http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57164

             Bug #: 57164
           Summary: enumerator value -1 is too large for underlying type
                    ‘unsigned int’
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: vvnic.ho...@gmail.com


Having an enumerator with a negative value in an enumeration with an unsigned
underlying type produces a compilation error.

enum A : unsigned {
    B = -1
};

nicholas@ubuntu:~$ g++ test.cpp -std=c++11
test.cpp:2:7: error: enumerator value -1 is too large for underlying type
‘unsigned int’
  B = -1
       ^

Same error if any other unsigned type is used, or if a scoped enumeration is
used.

As per §7.2.5: "If the underlying type is fixed, the type of each enumerator
prior to the closing brace is the underlying type and the constant-expression
in the enumerator-definition shall be a _converted constant expression_ of the
underlying type (5.19)" (emphasis added)

As per §5.19.3: "A converted constant expression of type T is an expression,
implicitly converted to a prvalue of type T, where the converted expression is
a core constant expression and the implicit conversion sequence contains only
user-defined conversions, lvalue-to-rvalue conversions (4.1), integral
promotions (4.5), and _integral conversions_ (4.7) other than narrowing
conversions (8.5.4)." (emphasis added)

As per §4.7.2 "If the destination type is unsigned, the resulting value is the
least unsigned integer congruent to the source integer"

In other words, the above snippet should be equivalent to:

enum A : unsigned {
    B = 4294967295
};

Reply via email to