[C/C++11]_[初级]_[使用enumerations类型]

场景

  1. 枚举类型在C++里用的最多就是声明某种数值类型,之后用switch来分别处理这些类型.
  2. C++11 定义了一种 enumeration(枚举类型),它区别于C的 enumerators(枚举器),这种类型可以定义枚举常量的类型为数值类型,比如char,不能定义非数值类型的枚举常量有点遗憾. 还可以给枚举常量增加使用范围, 也就是scoped enumerations.
  3. 范围枚举不能通过=来直接和数值类型相互转化, 这就增加了安全性.

参考

enumeration declaration
Enumerations

说明

语法结构

enum-key attr(optional) identifier(optional) enum-base(optional) { enumerator-list(optional) }  (1)
enum-key attr(optional) identifier enum-base(optional) ;    (2) (since C++11)

特性

  1. 其中 unscoped enumerations 和 scoped enumerations 的区别就是 scoped 的enum-key 是 enum struct 或者 enum class.

  2. unscoped enumerations 即可以直接脱离枚举 identifier 使用的.

  3. scoped enumerations 必须在 identifier 范围内通过::使用,类似命名空间.这种枚举类型不能直接转换为 int,这样就增加了一种编译时检查机制,使代码更可靠. 如果非要相互转换, 可以使用static_cast.

例子

// test_enum.cpp : 定义控制台应用程序的入口点。
    //

    #include <iostream>
    #include <stdint.h>
    #include <string>

    // enum that takes 16 bits
    enum smallenum: int16_t
    {
        a,
        b,
        c
    };


    // color may be red (value 0), yellow (value 1), green (value 20), or blue (value 21)
    enum color
    {
        red,
        yellow,
        green = 20,
        blue
    };

    // altitude may be altitude::high or altitude::low
    enum class altitude: char
    {
         high='h',
         low='l', // C++11 allows the extra comma
    };

    // the constant d is 0, the constant e is 1, the constant f is 3
    enum
    {
        d,
        e,
        f = e + 2
    };


    // typedef const char* CCHARP;
    // enum EnumStr: CCHARP
    // {
    // kEnumStrA="hello",
    // kEnumStrB="world",
    // };

    //enumeration types (both scoped and unscoped) can have overloaded operators
    std::ostream& operator<<(std::ostream& os, color c)
    {
        switch(c)
        {
            case red   : os << "red";    break;
            case yellow: os << "yellow"; break;
            case green : os << "green";  break;
            case blue  : os << "blue";   break;
            default    : os.setstate(std::ios_base::failbit);
        }
        return os;
    }

    std::ostream& operator<<(std::ostream& os, altitude al)
    {
        return os << static_cast<char>(al);
    }

    struct A
    {
        // 注意,这里必须指定int类型.
        enum {value = 14,};
        enum : int{value2 = 15,};
    };

    int main()
    {
        //enum class Handle : uint32_t { Invalid = 0 };

        // compiler error.
        // cannot convert 'int' to 'main()::Handle' in initialization
        //Handle h { 42 }; // OK

        color col = red;
        //altitude a {'l'};
        altitude a;
        a = altitude::low;
        switch(a)
        {
        case altitude::high:
            break;
        case altitude::low:
            break;
        }

        // int 和 scope enum 之间的转换.
        int col_int = static_cast<int> (col);
        color col_enum = static_cast<color>(col_int);

        std::cout << "col = " << col << '\n'
                  << "col_int = "   << col_int   << '\n'
                  << "col_enum = "   << col_enum   << '\n'
                  << "A::value = "   << A::value   << '\n'
                  << "A::value2 = "   << A::value2   << '\n'
                  << "a = "   << a   << '\n'
                  << "f = "   << f   << '\n';
    }

输出

col = red
col_int = 0
col_enum = red
A::value = 14
A::value2 = 15
a = l
f = 3

你可能感兴趣的:(enum,枚举类型,C++11,scoped,范围枚举)