skip to content

In C++, why does print(3.14) become ambiguous after you add print(double, int precision = 2) beside print(double)?

level: middleimportance: should knowfreq 45%

answer

  1. filled in at the call site
  2. not part of the function type
  3. both viable with one argument
  4. declare the default once

basics

~20 s

A default argument only lets the call omit a trailing argument; the compiler fills it in at the call site. With one argument both overloads are viable with the same exact match, so neither wins and the call is ambiguous.

solid answer

~50 s

Default arguments are not part of a function's type or signature: `void print(double, int = 2)` is still a two-parameter function, and the default just makes it **viable** for calls that pass one argument. Overload resolution only compares the conversions of the arguments actually written, so for `print(3.14)` both `print(double)` and `print(double, int)` need the same exact match on the one argument, and they are indistinguishable. Calls with two arguments are fine, because only the second overload accepts them. The fix is to keep **one** of the two shapes: either drop the one-parameter overload and rely on the default, or drop the default and keep two genuine overloads. Related rules: a default argument is evaluated at each call that omits it, it may be added in a later declaration in the same scope but never repeated or changed, and it belongs in the header declaration callers see.

code

cpp · 11 lines
cpp
#include <cstdio>

void print(double x)                    { std::printf("%g\n", x); }
void print(double x, int precision = 2) { std::printf("%.*f\n", precision, x); }

int main() {
    print(3.14159, 3);    // only the two-parameter overload is viable: prints 3.142
#ifdef AMBIGUOUS
    print(3.14159);       // error: call to 'print' is ambiguous
#endif
}

go deeper

for a junior

Recall how a default argument lets callers omit a trailing argument, and that the default goes on the declaration, not in both places.

for a middle

Explain why defaults are not part of the type, how they make a function viable for shorter calls, and why that ties with a genuine shorter overload.

for a senior

Show the production costs: defaults compiled into every caller, evaluated on each call, and ambiguities that surface only when a new caller writes the short form.

for a principal

Set a team rule on when an API uses a default argument versus an overload, with ABI and rebuild consequences of changing a default in a header in mind.

## What a default argument is A **default argument** is an initializer written on a parameter in a function declaration: `void print(double x, int precision = 2);`. When a call omits trailing arguments, the compiler supplies the defaults at the **call site**, so `print(1.5)` is compiled as `print(1.5, 2)`. The standard states plainly that a default argument **is not part of the type of a function**. The function's type is still `void(double, int)`, which shows up in three places: - A function pointer to it has two parameters: `void (*p)(double, int) = &print;` works, while `void (*q)(double) = &print;` does not, because the default does not travel with the pointer. - A default argument is evaluated **each time** the function is called without that argument, in the caller's context, so `int precision = current_precision()` calls `current_precision()` on every such call. - Callers only get the defaults they can see: the default lives in the declaration, normally the header, not in a definition hidden in a `.cpp` file. ## How defaults interact with overloading Overload resolution first builds the set of **viable** functions. A function with `n` parameters is viable for a call with fewer than `n` arguments only if every parameter after the last argument has a default. Then it ranks the conversions of the arguments that are **actually present**; the defaulted ones play no part. With both overloads declared: | Call | `print(double)` | `print(double, int = 2)` | Result | |---|---|---|---| | `print(3.14)` | viable, Exact Match | viable, Exact Match | ambiguous | | `print(3.14, 3)` | not viable | viable, Exact Match twice | `print(double, int)` | | `print(3)` | viable, Conversion | viable, Conversion | ambiguous | Every one-argument call ties, because both functions see exactly the same first argument. Clang reports `call to 'print' is ambiguous` and lists both candidates. Note that the error appears at the **call**, not at the declarations: the two overloads are legal to declare together, which is why the problem often surfaces only when a new caller writes the short form. ## Choosing between a default and an overload The two tools solve the same problem, so a function should use one or the other for a given parameter: - **A default argument** keeps one function body and one address. It is the right choice when the shorter call really means "the longer call with this value". - **A separate overload** is right when the short form does different work, needs a different implementation, or when the default must not be compiled into every caller (changing a default in a header changes the meaning of callers only after they are rebuilt). - Combining them for the same arity, as in the question, is always a trap. ## Redeclaration rules Default arguments also have rules of their own that interviewers like to probe: 1. A default may be **added** in a later declaration in the same scope: `void f(int, int); void f(int, int = 7);` is fine, and the two-argument function can then be called with one argument. 2. A default may **not be repeated or changed**, not even to the same value: writing `= 2` on both the header declaration and the definition is an error (clang: `redefinition of default argument`). 3. Every parameter after one with a default must also have a default, from this or an earlier declaration. 4. A declaration in an inner block scope starts with **no** defaults; it does not inherit them from the outer declaration. ## Defaults are compiled into the caller Because the compiler substitutes the default at each call site, the value lives in the **caller's** object code, not in the function: - Changing `= 2` to `= 3` in a header changes nothing for callers until they are recompiled; a shared library cannot change a default for already-built clients. - Two translation units that see different defaults for the same inline function break a rule the compiler is not required to diagnose. ## In practice - Put the default on the declaration in the header and nowhere else. - Keep defaults cheap and side-effect free, since they run on every call that omits them. - When an API needs both a short and a long form, decide once whether the short form is a default or an overload, and never ship both for the same number of arguments.

  • Why does writing the same default in the header and in the definition fail to compile?
    A default argument may be supplied once per parameter in a scope; a later declaration can add a missing default but never redefine one, even to the same value. Clang reports `redefinition of default argument`. Keep the default on the header declaration only.
  • Can you take a pointer to print(double, int = 2) with type void(*)(double)?
    No. The default is not part of the function type, so the function's type is `void(double, int)` and the pointer must have that type. Calls through the pointer must pass both arguments, because defaults are supplied only for calls that name the function directly.

  • A default argument creates a second overload with fewer parameters
  • Overload resolution prefers the function with fewer declared parameters
  • The default value is computed once, when the program starts
  • Repeating the same default in the definition is harmless
  • A function pointer keeps the default arguments of its target