문제

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