Вопрос

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