Skip to main content
A default parameter is a value specified in a function declaration that the compiler automatically injects into a function call when the caller omits the corresponding argument. It allows a function to be invoked with fewer arguments than it declares, effectively instructing the compiler to perform argument substitution at the call site during compile time.

Positional Rules (Right-to-Left)

Default arguments must be strictly trailing. If a parameter is assigned a default value, all subsequent parameters to its right in the lexical scope must also be assigned default values. The C++ standard mandates this because function arguments are resolved positionally from left to right.

Redeclaration Rules

According to C++ redeclaration rules ([dcl.fct.default]), a default argument cannot be redefined for a parameter in the same scope, even if the value is identical. Standard practice dictates placing the default argument in the forward declaration (typically in the header file) and omitting it in the function definition (the source file).
If a default parameter is redefined in the definition within the same scope, the compiler will emit an error. However, you can add default arguments to subsequent declarations in the same translation unit, provided they do not redefine existing defaults.

Evaluation Mechanics

Default arguments are evaluated at the call site, not at the declaration site. The expressions used for default parameters are bound at compile time but executed at runtime every time the function is called without that argument.

Interaction with Overload Resolution

Default parameters participate in overload resolution and can introduce ambiguity if an overloaded function signature matches the signature of a function utilizing default parameters.

Virtual Functions and Static Binding

When default parameters are used with virtual functions, the default value is statically bound based on the static type of the pointer or reference used to make the call, not the dynamic type of the object.
Tired of Poor C++ Skills? Fix That With Deep Grasping!Learn More