13

I am observing the following behavior under Visual Studio 2013 (Debug/Win32 compilation). Consider the following c++ code:

#include <iostream>
#include <climits>

int main(int argc, char *argv[])
{
  enum { V = (unsigned long long)ULLONG_MAX } E;
  std::cout << sizeof E << std::endl;

  enum : unsigned long long { W = (unsigned long long)ULLONG_MAX } F;
  std::cout << sizeof F << std::endl;

  return 0;
}

After compilation this leads to:

$ ./enum.exe
4
8

If I understand the c++ standard correctly (Standard C++ 7.2/5), this is an invalid c++ behavior. In this case, I should not be required to define the underlying type explicitly, since the value of an enumerator cannot fit in an int or unsigned int.

So:

  1. Is this is a well known limitation of Visual Studio 2013 (maybe other versions are affected) ?
  2. Is there a way to force the compiler to use a proper underlying type for a c++98 style enum ? Or am I required to switch to c++11 notation with fixed-type ?

Update: as suggested I reported a problem at:

malat
  • 12,152
  • 13
  • 89
  • 158

1 Answers1

1

The reference says the following (important parts bolded):

Declares an unscoped enumeration type whose underlying type is not fixed (in this case, the underlying type is an implementation-defined integral type that can represent all enumerator values; this type is not larger than int unless the value of an enumerator cannot fit in an int or unsigned int. If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0).

and

Values of unscoped enumeration type are implicitly-convertible to integral types. If the underlying type is not fixed, the value is convertible to the first type from the following list able to hold their entire value range: int, unsigned int, long, unsigned long, long long, or unsigned long long. If the underlying type is fixed, the values can be converted to their promoted underlying type.

Taken together, it is clear that it is a bug with (probably introduced at some point). What makes matters worse is that it is sort of silent about this.

darune
  • 10,480
  • 2
  • 24
  • 62