Why is the C++ call print(42u) ambiguous when only print(int), print(long) and print(double) are declared?
answer
- unsigned promotes to nothing
- three conversions of equal rank
- no distance between types
- better on one, worse on none
basics
~20 s42u has type unsigned int, which promotes to nothing. Reaching int, long or double is a Conversion-rank step in every case, and equal ranks do not compare, so no overload is better and the call is ambiguous.
solid answer
~50 s`42u` is an `unsigned int`. Integral promotion lifts only integer types of lower rank than `int`, plus `bool`, the character types and unscoped enumerations, so `unsigned int` has no promotion; `unsigned` to `int` and `unsigned` to `long` are integral conversions and `unsigned` to `double` is a floating-integral conversion. All three are **Conversion** rank, and the standard has no tie-breaker for "which integer type is wider" or "which conversion loses no value", so the three sequences are indistinguishable and the call is ill-formed. `print(5LL)` fails the same way. For several arguments the rule is: a function wins only if it is **not worse for any argument and better for at least one**; `h(int, double)` and `h(double, int)` called as `h(1, 1)` each win one argument, so that call is ambiguous too. Fix it with an exact overload (`print(unsigned)`), or a `static_cast` at the call site.
code
plaintext · 12 linesamb.cpp:2:13: error: call to 'print' is ambiguous
2 | int main(){ print(42u); print(5LL); }
| ^~~~~
amb.cpp:1:6: note: candidate function
1 | void print(int); void print(long); void print(double); void print(const char*);
| ^
amb.cpp:1:23: note: candidate function
1 | void print(int); void print(long); void print(double); void print(const char*);
| ^
amb.cpp:1:41: note: candidate function
1 | void print(int); void print(long); void print(double); void print(const char*);
| ^go deeper
Know that an overloaded call can fail to compile as ambiguous, and that a cast or an extra overload resolves it.
Explain why unsigned int has no promotion, why equal ranks tie, and the not-worse-anywhere, better-somewhere rule for several arguments.
Diagnose an ambiguity from the compiler's candidate list, choose between fixing the overload set and casting at call sites, and spot by-value versus by-reference overload pairs.
Set a rule for numeric overload sets in shared libraries, such as covering every fixed-width type or using one template, so callers never meet ambiguity.
## Where the ambiguity comes from Overload resolution picks the **best viable function**: a function `F1` is better than `F2` if, for every argument, the conversion `F1` needs is **not worse** than the one `F2` needs, and for at least one argument it is **strictly better**. If no single viable function is better than all the others, the call is ambiguous and the program is ill-formed. The comparison of individual conversions is coarse. Standard conversions are sorted into three ranks, and two conversions of the same rank are **indistinguishable** unless one of a short list of special rules applies. | Rank | Contents | |---|---| | Exact Match | identity, lvalue transformations, qualification adjustments | | Promotion | small integers to `int` or `unsigned int`; `float` to `double` | | Conversion | all other integral, floating, floating-integral, pointer and boolean conversions | ## Applying it to print(42u) The literal `42u` has type `unsigned int`. Walk the three candidates: 1. `print(int)`: `unsigned int` to `int` is an **integral conversion**. It is not a promotion: integral promotion applies to integer types whose rank is lower than `int`'s, to `bool`, to the character types and to unscoped enumerations, and `unsigned int` is none of these. 2. `print(long)`: `unsigned int` to `long` is also an **integral conversion**. On a platform where `long` is 64 bits it can represent every value, but value preservation is not part of the ranking. 3. `print(double)`: a **floating-integral conversion**. Three conversions of the same rank, none covered by a special rule, so none of the three functions is better than the others and clang reports `call to 'print' is ambiguous`, listing all three candidates. `print(5LL)` fails identically, since `long long` to `int`, `long`, or `double` are all conversions. ## The tie-breakers that do exist The standard does refine same-rank comparisons in a few places, and it is worth knowing them so you do not invent others: - A conversion that does **not** turn a pointer into `bool` beats one that does. - Conversions between a derived class pointer and its bases prefer the **most derived** target. - For reference bindings, binding to a **less cv-qualified** type wins, and an rvalue binding to an rvalue reference beats binding it to an lvalue reference. - Since C++23, conversions between floating-point types of **equal conversion rank** beat conversions to other arithmetic types (relevant for `std::float32_t` and friends). There is no rule that prefers a wider integer, a signed-to-unsigned match, or a conversion that is "lossless". ## Ambiguity with several arguments The "not worse anywhere, better somewhere" rule produces ambiguities that surprise people: - `h(int, double)` and `h(double, int)` called as `h(1, 1)`: each overload has an exact match on one argument and a conversion on the other. Neither is better on every argument, so the call is ambiguous. - `g(const int&)` and `g(int)` called with an `int` lvalue: both bindings are exact matches and no rule separates them, so clang rejects the call. Overloading on "by value versus by const reference" is almost always a mistake. ## Where it shows up in real code The unsigned case is not academic. On 64-bit Linux and macOS, `std::size_t` is `unsigned long`, so `print(v.size())` against `print(int)`, `print(long)` and `print(double)` is ambiguous for exactly the same reason: `unsigned long` has no promotion, and all three routes are conversions. The same is true of `std::uint64_t` and other fixed-width aliases whose underlying type the set does not name. A numeric overload set that looks complete for `int`, `long` and `double` usually covers fewer argument types than its author thinks. ## Fixing an ambiguous call Pick the fix by who owns the problem: - **The library author** adds the overload callers actually need (`print(unsigned)`, `print(long long)`), so every common argument type has an exact match. This is the durable fix. - **A caller** writes `print(static_cast<long>(value))`, which makes the argument an exact match for one overload. It works, but every call site must repeat it. - A **function template** catch-all would also accept every type, but changes the design; template overloading and constraints are covered with templates. A useful habit is to compile a quick table of calls, as in the code example, whenever an overload set grows: the compiler's ambiguity report is precise and names every tied candidate.
- Would adding print(unsigned) fix every unsigned call?It fixes `unsigned int` arguments, which become exact matches. It does not fix `unsigned long` or `unsigned long long`, which still convert to all four overloads at Conversion rank. Each distinct argument type that callers pass needs an exact or promoting route, or the set stays ambiguous for it.
- Why is print(short_value) not ambiguous when print(42u) is?`short` has a lower integer rank than `int`, so `short` to `int` is an integral **promotion**, which beats the conversions to `long` and `double`. `unsigned int` has the same rank as `int`, so it has no promotion and every route is a conversion.
- unsigned int promotes to long because long can hold every value
- The compiler picks the overload whose type is closest in width
- A lossless conversion ranks above a narrowing one
- With two arguments, the overload with more exact matches always wins
- Overloading f(int) and f(const int&) lets lvalues pick the reference