c++ - What makes enum -> int a better conversion than enum -> unsigned? -


in following code, overload f(int) chosen instead of f(unsigned). tested clang 3.0 , gcc 4.8.

enum e { };  e f(int); int f(unsigned);  e e = f(e(0)); 

my reading of standard lead me think enum -> int , enum -> unsigned identical standard conversion sequences both contain integral-conversion.

[conv.integral] rvalue of enumeration type can converted rvalue of integer type.

according [over.best.ics], rank of standard conversion sequence containing integral conversion 'conversion'.

[over.ics.rank] 2 implicit conversion sequences of same form indistinguishable conversion sequences unless 1 of following rules apply: [...]

none of rules mentioned seem apply when comparing 2 standard conversion sequences.

what missing?

c++11:

[conv.prom]/3

a prvalue of unscoped enumeration type underlying type not fixed (7.2) can converted prvalue of first of following types can represent values of enumeration (i.e., values in range b min b max described in 7.2): int, unsigned int, long int, unsigned long int, long long int, or unsigned long long int.

(emphasis mine)

then, [over.ics.rank]/4:

standard conversion sequences ordered ranks: exact match better conversion promotion, better conversion conversion.

so, overload resolution on expression f(e(0)), overload e f(int); requires integral promotion (from e int, via [conv.prom]), has higher rank integral conversion required int f(unsigned); (from e unsigned via [conv.integral]).


for c++03, argumentation same, though first quote different: [conv.prom]/2

an rvalue of type wchar_t (3.9.1) or enumeration type (7.2) can converted rvalue of first of following types can represent values of underlying type: int, unsigned int, long, or unsigned long.

[over.ics.rank]/4 same.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -