Evaluating C++ Default Arguments

When a function defined with default arguments is called with trailing arguments missing, the default expressions are evaluated. For example:

void f(int a, int b = 2, int c = 3); // declaration
// ...
int a = 1;
f(a);            // same as call f(a,2,3)
f(a,10);         // same as call f(a,10,3)
f(a,10,20);      // no default arguments

Default arguments are checked against the function declaration and evaluated when the function is called. The order of evaluation of default arguments is undefined. Default argument expressions cannot use formal arguments of a function. For example:

int f(int q = 3, int r = q); // error

The argument r cannot be initialized with the value of the argument q because the value of q may not be known when it is assigned to r. If the above function declaration is rewritten:

int q=5;
int f(int q = 3, int r = q); // error

the value of r in the function declaration still produces an error because the variable q defined outside of the function is hidden by the argument q declared for the function. Similarly:

typedef double D;
int f(int D, int z = D(5.3) ); // error

Here the type D is interpreted within the function declaration as the name of an integer. The type D is hidden by the argument D. The cast D(5.3) is therefore not interpreted as a cast because D is the name of the argument not a type.

In the following example, the nonstatic member a cannot be used as an initializer because a does not exist until an object of class X is constructed. You can use the static member b as an initializer because b is created independently of any objects of class X. You can declare the member b after its use as a default argument because the default values are not analyzed until after the final bracket } of the class declaration.

class X
{
   int a;
    f(int z = a) ; // error
    g(int z = b) ; // valid
    static int b;
};

You must put parentheses around default argument expressions that contain template references. In the following example:

class C {
   void f(int i = X<int,5>::y);
};

the compiler cannot tell that the < represents the start of a template argument list and not the less than operator because the default argument X <int,5>::y cannot be processed until the end of the class.

To avoid error messages, put parentheses around the expression containing the default argument:

class C {
   void f( int i = (X<int,5>::y) );
};


Default Arguments in C++ Functions
Restrictions on Default Arguments
Calling Functions and Passing Arguments


Constructors and Destructors Overview