Topic: Defect Report: integral promotion of enumeration disregards fixed


Author: Alberto Ganesh Barbati <AlbertoBarbati@libero.it>
Date: Sun, 6 Jan 2008 14:19:04 CST
Raw View
===================================== MODERATOR'S COMMENT:

This is not formally a DR as it applies to a draft rather than an IS.


===================================== END OF MODERATOR'S COMMENT
Hi Everybody,

This is an excerpt of [conv.prom]/2 of N2461:

"An rvalue of an unscoped enumeration type (7.2) can be converted to an
rvalue of the first of the following types that can represent all the
values of the enumeration (i.e. the values in the range bmin to bmax as
described in 7.2: int, unsigned int, long int, unsigned long int, long
long int, or unsigned long long int."

This wording may have surprising behaviour in this case:

   enum Enum : long { value = 0; }

   void f(int);
   void f(long);

   int main()
   {
     f(value);  // which f() is called?
   }

Intuitively, as the programmer has explicitly expressed preference for
long as the underlying type, he/she might expect f(long) to be called.
However, if long and int happens to have the same size, then value is
promoted to int (as it the first type in the list that can represent all
values of Enum) and f(int) is called instead.

Please notice that according to [dcl.enum]/4 the underlying type of an
enumeration is always well defined for both the fixed and the non-fixed
case, so it makes sense to simply promote to the underlying type unless
such type would require promotion itself.

Proposed resolution:

Replace in [conv.prom]/2 all text after "An rvalue of an unscoped
enumeration type (7.2) ..." and up to the end of the paragraph, with:

"An rvalue of an unscoped enumeration type (7.2) is converted to an
rvalue of its underlying type if it is different from char16_t,
char32_t, wchar_t or has integer conversion rank greater than or equal
to int. Otherwise, it is converted to an rvalue of the first of the
following types that can represent all the values of its underlying
type: int, unsigned int, long int, unsigned long int, long long int, or
unsigned long long int."

(Notice that this wording needs no longer mention extended integer types
as special cases.)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]