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?
有帮助吗?

解决方案 2

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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top