Why is initialization of enum class temporaries with arbitrary values allowed?

Peter Ruderman

I came across some code like the following in one the CppCon 2014 talks that confused the heck out of me. The audience accepted it without comment, so I presume that it's legal:

enum class Foo { Bar };

Foo const v1 = Foo(5);

The question is: why does this compile? I would expect compilation to fail and complain that we can't convert an int to a Foo. The slightly modified line below fails with the expected error:

Foo const v1(5);
user743382

Scoped enumeration types have an implicit underlying type of int, assuming no other underlying type is specified. All possible values of type int can be represented.

7.2p5:

[...] For a scoped enumeration type, the underlying type is int if it is not explicitly specified. In both of these cases, the underlying type is said to be fixed. [...]

7.2p8:

For an enumeration whose underlying type is fixed, the values of the enumeration are the values of the underlying type. [...]

And any integral value that can be represented by the enumeration can be explicitly converted to that enumeration type, as @Columbo had pointed out in his now-deleted answer:

5.2.9p10:

A value of integral or enumeration type can be explicitly converted to an enumeration type. The value is unchanged if the original value is within the range of the enumeration values (7.2). [...]

Since there is some confusion in the comments about what that means:

enum class Foo { Bar };

Foo const v1 = Foo(5);

is well-defined. Not undefined, not unspecified, not even implementation-defined. The parts of the standard I quote explain that:

  1. The underlying type of Foo is int, and that the underlying type is fixed.
  2. The values of Foo are the values of int.
  3. Since 5 is in the range of the enumeration values, the value is unchanged by the conversion.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the best way to store a reference to an arbitrary class inside an enum constant using the least memory on initialization as possible?

From Dev

Can I force initialization for all possible enum values of an enum class class/function template?

From Dev

What are the allowed types for an enum (class)?

From Dev

Why are arbitrary target expressions allowed in for-loops?

From Dev

Why is second initialization allowed in C++11

From Dev

Why I can not get .values() from Enum object class?

From Dev

Why is an extra comma not allowed in a parameter list when it is allowed in a brace initialization?

From Dev

Why is field referencing not allowed in an enum (or is this a compiler bug?)

From Dev

Dynamic initialization of enum class inside template struct

From Dev

Why null values are not allowed in ArrayDeque?

From Dev

Converting enum values to arbitrary integers when using Newtonsoft.Json

From Dev

Initialization of static/class lists/dictionaries in inner class(enum) in Python 3.5

From Dev

Documenting enum class values with doxygen

From Dev

enum values in a class - Qt Creator

From Java

Why is enum class preferred over plain enum?

From Dev

Why is variable sized object initialization not allowed using const

From Dev

Why auto_ptr initialization using the assignment syntax is not allowed

From Dev

Why auto_ptr initialization using the assignment syntax is not allowed

From Java

List-initialization of an array without temporaries - not working in GCC

From Dev

Why is usage of instanceof is not allowed, but Class.isInstance() is allowed in Generics?

From Dev

"Error in knn (...) no missing values are allowed", why?

From Dev

C++ enum class - initialization from underlying_type

From Dev

C++ enum class - initialization from underlying_type

From Dev

Why is class of T not allowed in a generic type?

From Dev

Why is a static local class not allowed in a method?

From Dev

Why is the semicolon not required but allowed at the end of a class definition?

From Dev

Why super keyword in generics is not allowed at class level

From Dev

Why super keyword in generics is not allowed at class level

From Dev

Why is a static local class not allowed in a method?

Related Related

  1. 1

    What is the best way to store a reference to an arbitrary class inside an enum constant using the least memory on initialization as possible?

  2. 2

    Can I force initialization for all possible enum values of an enum class class/function template?

  3. 3

    What are the allowed types for an enum (class)?

  4. 4

    Why are arbitrary target expressions allowed in for-loops?

  5. 5

    Why is second initialization allowed in C++11

  6. 6

    Why I can not get .values() from Enum object class?

  7. 7

    Why is an extra comma not allowed in a parameter list when it is allowed in a brace initialization?

  8. 8

    Why is field referencing not allowed in an enum (or is this a compiler bug?)

  9. 9

    Dynamic initialization of enum class inside template struct

  10. 10

    Why null values are not allowed in ArrayDeque?

  11. 11

    Converting enum values to arbitrary integers when using Newtonsoft.Json

  12. 12

    Initialization of static/class lists/dictionaries in inner class(enum) in Python 3.5

  13. 13

    Documenting enum class values with doxygen

  14. 14

    enum values in a class - Qt Creator

  15. 15

    Why is enum class preferred over plain enum?

  16. 16

    Why is variable sized object initialization not allowed using const

  17. 17

    Why auto_ptr initialization using the assignment syntax is not allowed

  18. 18

    Why auto_ptr initialization using the assignment syntax is not allowed

  19. 19

    List-initialization of an array without temporaries - not working in GCC

  20. 20

    Why is usage of instanceof is not allowed, but Class.isInstance() is allowed in Generics?

  21. 21

    "Error in knn (...) no missing values are allowed", why?

  22. 22

    C++ enum class - initialization from underlying_type

  23. 23

    C++ enum class - initialization from underlying_type

  24. 24

    Why is class of T not allowed in a generic type?

  25. 25

    Why is a static local class not allowed in a method?

  26. 26

    Why is the semicolon not required but allowed at the end of a class definition?

  27. 27

    Why super keyword in generics is not allowed at class level

  28. 28

    Why super keyword in generics is not allowed at class level

  29. 29

    Why is a static local class not allowed in a method?

HotTag

Archive