C++ Logo

std-discussion

Advanced search

Re: Implicit conversion sequence from literal 0

From: Phil <std-discussion_at_[hidden]>
Date: Sun, 8 Jan 2023 03:02:14 +0100
The example code favors the first overload because the 2nd argument (integer literal -> int -> Identity -> Exact Match) does not need to get narrowed to short like needed for the second overload (Integral conversions -> Conversion), i.e. it has a better rank.

If you explicitly cast the 2nd argument to short, the second overload gets selected because of identity match for 2nd parameter. There is of course no ambiguity because the first overload (with an int as second parameter) would require a integral promotion which has rank promotion, but the identity match with short is still better (exakt match).

If you change the 2nd parameter for the second overload from short to int/long, you will get ambiguity, because the first argument is always ambiguous since converting 0 to short as well as converting 0 to void* are conversions and have the same rank. It just depends on the second parameter.

If all shorts would be changed to ints, the second overload would be selected, since it had two times identity -> exact match (for both parameters). If the first overload changed its second parameter from int to short, there would be ambiguity again, since the compiler can not tell which conversion is "better": converting the first argument to void* or the second to short (both have rank: Conversion).

Therefore I do not see that this is an issue or related to "null-pointers". Or do I miss the point for this discussion completely?

Received on 2023-01-08 02:02:28