Movatterモバイル変換


[0]ホーム

URL:


This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 119a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-12-20


685. Integral promotion of enumeration ignores fixed underlying type

Section:7.3.7  [conv.prom]    Status:CD2    Submitter:Alberto Ganesh Barbati    Date:6 January, 2008

[Voted into WP at July, 2009 meeting.]

According to 7.3.7 [conv.prom] paragraph 2,

An rvalue of an unscoped enumeration type (9.8.1 [dcl.enum]) can be converted to an rvalue of the first of thefollowing types that can represent all the values of theenumeration (i.e. the values in the rangebmintobmax as described in 9.8.1 [dcl.enum]):int,unsigned int,longint,unsigned long int,long long int, orunsigned long long int.

This wording may have surprising behavior in this case:

    enum E: long { e };    void f(int);    void f(long);    void g() {        f(e);    // Whichf is called?    }

Intuitively, as the programmer has explicitly expressedpreference forlong as the underlying type, he/she mightexpectf(long) to be called. However, iflongandint happen to have the same size, thene ispromoted toint (as it is the first type in the list thatcan represent all values ofE) andf(int) iscalled instead.

According to 9.8.1 [dcl.enum] the underlying type of anenumeration is always well-defined for both the fixed and thenon-fixed cases, so it makes sense simply to promote to theunderlying type unless such a type would itself require promotion.

Suggested resolution:

In 7.3.7 [conv.prom] paragraph 2, replace all the textfrom “An rvalue of an unscoped enumeration type” throughthe end of the paragraph with the following:

An rvalue of an unscoped enumeration type (9.8.1 [dcl.enum])is converted to an rvalue of its underlying type if it is differentfromchar16_t,char32_t,wchar_t, or hasinteger conversion rank greater than or equal toint.Otherwise, it is converted to an rvalue of the first of the followingtypes that can represent all the values of its underlying type:int,unsigned int,long int,unsigned long int,long long int, orunsigned long long int.

(Note that this wording no longer needs to mention extendedinteger types as special cases.)

Proposed resolution (August, 2008):

Move the following text from 7.3.7 [conv.prom] paragraph2 into a separate paragraph, making the indicated changes, and addthe following new paragraph after it:

An rvalue of an unscoped enumeration typewhose underlyingtype is not fixed (9.8.1 [dcl.enum]) can be convertedto an rvalue of the first of the following types that canrepresent all the values of the enumeration (i.e. the values inthe rangebmin tobmax asdescribed in 9.8.1 [dcl.enum]):int,unsigned int,long int,unsigned longint,long long int, orunsigned long longint. If none of the types in that list can represent allthe values of the enumeration, an rvalue of an unscopedenumeration type can be converted to an rvalue of the extendedinteger type with lowest integer conversion rank (7.3.15 [conv.bool]) greater than the rank oflong long inwhich all the values of the enumeration can be represented. Ifthere are two such extended types, the signed one is chosen.

An rvalue of an unscoped enumeration type whose underlyingtype is fixed (9.8.1 [dcl.enum]) can be converted to anrvalue of its underlying type. Moreover, if integral promotioncan be applied to its underlying type, an rvalue of an unscopedenumeration type whose underlying type is fixed can also beconverted to an rvalue of the promoted underlying type.




[8]ページ先頭

©2009-2026 Movatter.jp