c++11系列:enumeration

2017-05-31  本文已影响0人  再见小浣熊

枚举类型

从C++11开始,存在了两种枚举类型Unscoped enumerationScoped enumeration

Unscoped enumeration是C++98风格的枚举,也是平时使用的最多的,enum Direction { East, South, West, North }

Scoped enumeration是C++11引入的新的枚举类型,也是更推荐使用的枚举。完整的定义如下enum struct|class name : type { enumerator = constexpr , enumerator = constexpr , ... }
定义的方式和Unscoped enumeration大体一致,只有两个部分有些差异:

  1. struct | class: 只要使用了struct或者class关键字,就表示定义了Scoped enumeration,而这两个关键字本身是等价的,随便使用哪个都没有区别,根据惯例的话更偏向使用class

  2. type:其实C++11以后,两种类型的枚举都可以指定type。这个类型是可以缺省的,这样默认使用的就是int类型。如果不符合自己的使用场景,也可以自己指定类型,但该类型属于integral type,如charintuint8_t等。

Scoped enums的优势

  1. 减少命名空间的污染
    在包含Unscoped enum的作用域内,定义的任何变量不可以和枚举符重名
    enum Color {
        red, green, blue
    };

    enum class Animal {
        cat, dog, fish
    };

    auto red = "red"; // error! redefinition of 'red' as different kind of symbo
    auto cat = "cat"; // OK
  1. 强类型
    Unscoped enum可以隐式地转化为整型,意味着枚举符可以当作整型,甚至float来使用,在某些情况下会引起难以察觉的bug,这也是C++11的期望解决的
    enum Color {
        red, green, blue
    };

    enum class Animal {
        cat, dog, fish
    };

    int a = red; // OK
    int b = Animal::cat; // Error, cannot initialize a variable of type 'int' with an rvalue of type 'Animal'
    Animal c = Animal::cat; // OK
    
    // 当然,如果你非要转化成int也是可以的,虽然不推荐这样做
    // 如果遇到这样的情况,更推荐使用Unscoped enum
    int d = static_cast<int>(Animal::cat); 

参考资料

上一篇 下一篇

猜你喜欢

热点阅读