Skip to content

Latest commit

 

History

History
62 lines (41 loc) · 1.41 KB

File metadata and controls

62 lines (41 loc) · 1.41 KB

enum class

enum makes it possible to define integral values with meaningful names, without defining constants and giving them a separate type.

enum status {open, closed};

void foo(status s);

foo(open);
foo(closed);
foo(42); // does not compile

enum class

The downsides of enum is that it is "global", as it is not possible to reuse the identifier in other places. It also converts implicitely to other integral types:

enum status {open, closed};

bool closed = false; // does not compile
open + closed; // compiles

enum class

enum class shares many of the properties of an enum, but

  • introduces a new scope

  • does not convert implicitely to integral types

enum class status {open, closed};

void foo(status s);

foo(status::open);
foo(status::closed);
foo(42); // does not compile

bool closed = false; // compiles
status::open + status::closed; // does not compile

enum class

For both enum and enum class it is possible to cast an integral value to an enumeration type. But if the produced value is not within the range of the enumeration values, then the behaviour is undefined.