栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++11强类型枚举

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++11强类型枚举

大家枚举类型是不是都是这样实现的,这种枚举被称做弱枚举类型。

#include 

enum Color{
    RED,
    GREEN,
    BLUE,
};

void fun() {
    int color = RED;
    ::std::cout << color << ::std::endl;
}

​ 在弱枚举中,枚举类型是不限定作用域的(unscoped enumeration),可以不加命名空间随意使用,但不限定作用域的用法总是充满危险的。例如:

enum ColorB {
    RED.
};

​ 这样定义新的枚举ColorB,与其他弱枚举(Color)里面的RED与冲突。

​ 不仅如此,弱枚举类型默认可以被隐式转换为int类型,还存在这枚举类型与基本类型的比较。

void fun() {
    int color  = RED;
    int color2 = 1;
    if (color < color2) ::std::cout << color << " < " << color2 << ::std::endl;
}
//0 < 1

​ 这种状况很容易对整型和浮点型的隐式转换,造成乱用(本来你不相进行转换或比较)。或者是这样传递参数,有2个类A、B,都有一种id属性,当通过id获取A或者B对象的时候,会出现问题,例如:

class A {
public:
    uint64_t id() const;
};

class B {
public:
    uint64_t id() const;
};

::std::shared_ptr get_a_by_id(uint64_t id);
::std::shared_ptr get_b_by_id(uint64_t idA,uint64_t idB);

​ 当获取get_b_by_id的时候,把idB误当做idA传入,在运行的过程中,才会发现这个问题,当项目比较大的时候,debug会很复杂,不好定位问题。

struct UID {
    uint64_t id;

    constexpr explicit UID(uint64_t id) noexcept : id(id) {
    }
};

class A {
public:
    uint64_t id() const;
};

class B {
public:
    uint64_t id() const;
};

::std::shared_ptr get_a_by_id(UID id);

​ 定义一个UID的类,维护类型的隐式转换,传入数据的时候,指定类型。但是通常在函数开头判断id是否不为空,需要重载UID类的bool判断

struct UID {
    uint64_t id;

    constexpr explicit UID(uint64_t id) noexcept : id(id) {
    }

    constexpr explicit operator bool() const noexcept {
        return id != 0;
    }
};

::std::shared_ptr get_a_by_id(UID id) {
    if (!id) {
        return nullptr;
    }

    // ...
}

​ 需要打印log的时候,还需要重载<<运算符。

struct UID {
    uint64_t id;

    constexpr explicit UID(uint64_t id) noexcept : id(id) {
    }

    constexpr explicit operator bool() const noexcept {
        return id != 0;
    }

    friend ::std::ostream& operator<<(::std::ostream& os, UID id) {
        return os << id.id;
    }
};

class A {
public:
    uint64_t id() const;
};

class B {
public:
    uint64_t id() const;
};

::std::shared_ptr get_a_by_id(UID id) {
    if (!id) {
        return nullptr;
    }

    // ...

    ::std::cout << "get_a_by_id: " << id << ::std::endl;
}

定义一个强枚举,类型安全,不会因为隐式转换而造成歧义。

enum UID : uint64_t;

class A {
public:
    uint64_t id() const;
};

class B {
public:
    uint64_t id() const;
};

::std::shared_ptr get_a_by_id(UID id) {
    if (!id) {
        return nullptr;
    }

    // ...

    ::std::cout << "get_a_by_id: " << id << ::std::endl;
}

还可以利用static_cast(1)进行类型转换,int --> UID(强枚举),更加的清晰,增加可读性,减少歧义。

指定enum类型的好处是:

可以控制不同实验环境中使用的类型,我们可以确保在一种实现环境中编译通过的程序所产生的代码与其他实现环境中产生的代码一致。
——《C++ Primer》

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847636.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号