Topic: enums and conversion question
Author: raviyer@nt.com
Date: 1996/03/16 Raw View
Hi,
I have the following piece of code,
void goo(int);
void goo(unsigned int);
enum Enum {E1,E2}
void foo ()
{
Enum e = E1;
goo(e); // Is this call ambiguos?
}
The DWP in 4.5 (integral promotion) point 2 says that
"An rvalue of type wchar_t or an enumeration type can be converted to an
rvalue of the _first_ of the following types that can represent all the values
of the source type: int, unsigned int, long, unsigned long."
Does this mean that in the above example the call to goo(e) should be resolved
to goo(int)? or in other words is the promotion enum -> int better than
enum -> unsigned int in this case?
thanks in advance
--
|#include <stddisclaimers.h> // My own views |
|Ravikant Iyer. aka - ravi - raviyer@nt.com |
|Nortel. Santa Clara CA. Off: 408-565-7430 |
| ESN-655-7430. |
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/03/18 Raw View
raviyer@nt.com writes:
>void goo(int);
>void goo(unsigned int);
>enum Enum {E1,E2}
>void foo ()
>{
> Enum e = E1;
> goo(e); // Is this call ambiguos?
>}
>The DWP in 4.5 (integral promotion) point 2 says that
>"An rvalue of type wchar_t or an enumeration type can be converted to an
>rvalue of the _first_ of the following types that can represent all the values
>of the source type: int, unsigned int, long, unsigned long."
>Does this mean that in the above example the call to goo(e) should be resolved
>to goo(int)? or in other words is the promotion enum -> int better than
>enum -> unsigned int in this case?
Yes, although your "other words" are not quite correct.
If the range of an enum type can be represented by type int, values
of that type undergo promotion to int, and not to any other type.
Thus, calling goo(int) requires only a promotion to int (since the
values 0 and 1 can always be represented by type int), while calling
goo(unsigned int) requires a standard conversion. A promotion is
preferred over a conversion, so the call is not ambiguous.
--
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]