The Mask
The Mask

Reputation: 17457

Is enum members type still implementation-dependent?

I've read some articles about C++11 but none of them mentioned whether the implementation-dependent size of members of an enum keeps unchanged.

  1. Is that still the case?
  2. If yes, is it a good pratice to use enum class Token : int { ... }; to explictly force compile to make them of int type?

Upvotes: 1

Views: 158

Answers (2)

bames53
bames53

Reputation: 88225

If you don't specify a type for an unscoped enum then the type is implementation defined, and it may be any type capable of representing all the enumerator values (if there is no such type then the program is ill-formed). This is the same behavior as for all enums prior to C++11.

If you don't specify a type for a scoped enum then it has a fixed type of int. You can be explicit about the type if you want, but I don't think it's commonly considered a best practice.

Upvotes: 3

Shoe
Shoe

Reputation: 76300

As per §7.2/5 of the Standard:

Each enumeration also has an underlying type. The underlying type can be explicitly specified using enum-base; if not explicitly specified, the underlying type of a scoped enumeration type is int.

(emphasis mine).

On this matter (underlying type), the standard does not make any difference whatsoever between enum and enum class/enum struct.

Upvotes: 1

Related Questions