Enum vs强类型枚举

Ras*_*yak 81 c++ enums c++11

我是C++编程的初学者.

今天我遇到了一个新主题:强类型enum.我对它进行了一些研究但到目前为止我无法找到为什么我们需要这个以及它的用途是什么?

例如,如果我们有:

enum xyz{a, b, c};
/*a = 0, b = 1, c = 2, (Typical C format)*/
Run Code Online (Sandbox Code Playgroud)

为什么我们需要写:

enum class xyz{a, b, c};
Run Code Online (Sandbox Code Playgroud)

我们在这里做什么?我最重要的疑问是如何使用它.你能提供一个小例子,这会让我理解.

jua*_*nza 110

好的,第一个例子:旧式枚举没有自己的范围:

enum Animals {Bear, Cat, Chicken};
enum Birds {Eagle, Duck, Chicken}; // error! Chicken has already been declared!

enum class Fruits { Apple, Pear, Orange };
enum class Colours { Blue, White, Orange }; // no problem!
Run Code Online (Sandbox Code Playgroud)

其次,它们隐式转换为整数类型,这可能导致奇怪的行为:

bool b = Bear && Duck; // what?
Run Code Online (Sandbox Code Playgroud)

最后,您可以指定C++ 11枚举的基础整数类型:

enum class Foo : char { A, B, C};
Run Code Online (Sandbox Code Playgroud)

以前,未指定基础类型,这可能会导致平台之间的兼容性问题.编辑已在评论中指出,您还可以在C++ 11中指定"旧样式"枚举的基础整数类型.

  • 另外:如果指定了基础类型,则可以对enum-s进行前向声明。对于C ++ 11,C ++ 98中的普通枚举,不允许使用。Microsoft编译器允许您进行枚举的前向声明,但这只是MS扩展名,不是标准的(例如,gcc不允许这样做),所以现在这样是合法的:enum ForwardDeclare:std :: uint8_t; (2认同)
  • @SSAnne不,隐式转换违背了强类型枚举的目的。定义一个模板函数来显式执行转换;使用 std::underlying_type<T>::type 提取类型。 (2认同)

Sin*_*all 16

这个IBM页面上有一篇关于枚举的好文章,它非常详细且写得很好.简而言之,以下是一些重要的观点:

范围内的枚举解决了常规枚举产生的大部分限制:完整的类型安全性,明确定义的底层类型,范围问题和前向声明.

  • 通过禁止将作用域枚举的所有隐式转换转换为其他类型,您可以获得类型安全性.
  • 您获得了一个新范围,并且枚举不再位于封闭范围内,从而避免了名称冲突.
  • Scoped枚举使您能够指定枚举的基础类型,对于作用域枚举,如果您选择不指定它,则默认为int.
  • 具有固定基础类型的任何枚举都可以向前声明.

  • 第三点和第四点不是特定于范围枚举的。您可以指定任何枚举的基础类型。 (2认同)

For*_*veR 11

enum class的确是类型enum class,而不是underlying_typeC-enums.

enum xyz { a, b, c};
enum class xyz_c { d, f, e };

void f(xyz x)
{
}

void f_c(xyz_c x)
{
}

// OK.
f(0);
// OK for C++03 and C++11.
f(a);
// OK with C++11.
f(xyz::a);
// ERROR.
f_c(0);
// OK.
f_c(xyz_c::d);
Run Code Online (Sandbox Code Playgroud)


Jna*_*ana 5

枚举类(“新枚举”,“强枚举”)解决了传统C ++枚举的三个问题:

  1. 常规enums隐式转换为int,当某人不希望枚举充当整数时会导致错误。
  2. 常规 enums上将其枚举器导出到周围的范围,导致名称冲突。
  3. enum无法指定an的基础类型,从而导致混乱,兼容性问题,并使前向声明变得不可能。

enum class (“强枚举”)具有强类型和范围:

enum Alert { green, yellow, orange, red }; // traditional enum

enum class Color { red, blue };   // scoped and strongly typed enum
                                  // no export of enumerator names into enclosing scope
                                  // no implicit conversion to int
enum class TrafficLight { red, yellow, green };

Alert a = 7;              // error (as ever in C++)
Color c = 7;              // error: no int->Color conversion

int a2 = red;             // ok: Alert->int conversion
int a3 = Alert::red;      // error in C++98; ok in C++11
int a4 = blue;            // error: blue not in scope
int a5 = Color::blue;     // error: not Color->int conversion

Color a6 = Color::blue;   // ok
Run Code Online (Sandbox Code Playgroud)

如图所示,传统枚举像往常一样工作,但是您现在可以选择使用枚举的名称进行限定。

新的枚举是“枚举类”,因为它们将传统枚举的各个方面(名称值)与类的各个方面(作用域成员和无转换)结合在一起。

能够指定基础类型可以简化互操作性并保证枚举的大小:

enum class Color : char { red, blue };  // compact representation

enum class TrafficLight { red, yellow, green };  // by default, the underlying type is int

enum E { E1 = 1, E2 = 2, Ebig = 0xFFFFFFF0U };   // how big is an E?
                                                 // (whatever the old rules say;
                                                 // i.e. "implementation defined")

enum EE : unsigned long { EE1 = 1, EE2 = 2, EEbig = 0xFFFFFFF0U };   // now we can be specific
Run Code Online (Sandbox Code Playgroud)

它还允许枚举的前向声明:

enum class Color_code : char;     // (forward) declaration
void foobar(Color_code* p);       // use of forward declaration
// ...
enum class Color_code : char { red, yellow, green, blue }; // definition
Run Code Online (Sandbox Code Playgroud)

基础类型必须是有符号或无符号整数类型之一;默认是int

在标准库中,enum类用于:

  1. 映射系统特定的错误代码<system_error>enum class errc ;
  2. 指针安全指标<memory>enum class pointer_safety { relaxed, preferred, strict };
  3. I / O流错误:在<iosfwd>enum class io_errc { stream = 1 };
  4. 异步通信错误处理<future>enum class future_errc { broken_promise, future_already_retrieved, promise_already_satisfied };

其中一些具有运算符,例如==defined。