Namespaces
Variants
Views
Actions

Default arguments

From cppreference.com
< cpp‎ | language

Allows a function to be called without providing one or more trailing arguments.

Indicated by using the following syntax for a parameter in the parameter-list of a function declaration.

attr(optional) decl-specifier-seq declarator = initializer (1)
attr(optional) decl-specifier-seq abstract-declarator(optional) = initializer (2)

Default arguments are used in place of the missing trailing arguments in a function call:

void point(int x = 3, int y = 4);
 
point(1,2); // calls point(1,2)
point(1);   // calls point(1,4)
point();    // calls point(3,4)

In a function declaration, after a parameter with a default argument, all subsequent parameters must

  • have a default argument supplied in this or a previous declaration; or
  • be a function parameter pack. (since C++11)

The ellipsis is not a parameter, and so can follow a parameter with a default argument.

int f(int = 1, int); // error, assuming there's no previous declaration of f
int g(int n = 0, ...); // ok
template<class...T> void h(int i = 0, T... args); // ok

Default arguments are only allowed in the parameter lists of function declarations and lambda-expressions, (since C++14) and are not allowed in the declarations of pointers to functions, references to functions, or in typedef declarations. Template parameter lists use similar syntax for their default template arguments.

For non-template functions, default arguments can be added to a function that was already declared if the function is redeclared in the same scope. At the point of a function call, the defaults are a union of the defaults provided in all visible declarations for the function. A redeclaration cannot introduce a default for an argument for which a default is already visible (even if the value is the same). A re-declaration in an inner scope does not acquire the default arguments from outer scopes.

void f(int, int);     // #1 
void f(int, int = 7); // #2 OK: adds a default
 
void h() {
    f(3); // #1 and #2 are in scope; makes a call to f(3,7)
    void f(int =1, int); // Error: inner scope declarations don't acquire defaults
}
 
void m() { // new scope begins
    void f(int, int); // inner scope declaration; has no defaults.
    f(4); // Error: not enough arguments to call f(int, int)
    void f(int, int = 6); 
    f(4); // OK: calls f(4,6);
    void f(int, int = 6); // Error: cannot redeclare a default in the same scope
}
 
void f(int = 1, int); // #3 OK, adds a default to #2
 
void n() { // new scope begins 
    f(); // #1, #2, and #3 are in scope: calls f(1, 7);
}

If an inline function is declared in different translation units, the accumulated sets of default arguments must be the same at the end of each translation unit.

If a friend declaration specifies a default, it must be a friend function definition, and no other declarations of this function are allowed in the translation unit.

The using-declaration carries over the set of known default arguments, and if more arguments are added later to the function's namespace, those defaults are also visible anywhere the using-declaration is visible

namespace N {
    void f(int, int = 1);
}
using N::f;
void g() {
    f(7); // calls f(7, 1);
    f();  // error
}
namespace N {
    void f(int = 2, int);
}
void h() {
    f();  // calls f(2, 1);
}

The names used in the default arguments are looked up, checked for accessibility, and bound at the point of declaration, but are executed at the point of the function call:

int a = 1;
int f(int);
int g(int x = f(a)); // lookup for f finds ::f, lookup for a finds ::a
                     // the value of ::a, which is 1 at this point, is not used
void h()
{
  a = 2;  // changes the value of ::a
  {
     int a = 3;
     g();       // calls f(2), then calls g() with the result
  }
}

For a member function of a non-template class, the default arguments are allowed on the out-of-class definition, and are combined with the default arguments provided by the declaration inside the class body. If these out-of-class defaults would turn a member function into a default, copy, or move constructor the program is ill-formed. For member functions of class templates, all defaults must be provided in the initial declaration of the member function.

class C {
    void f(int i = 3);
    void g(int i, int j = 99);
    C(int arg); // non-default constructor
};
void C::f(int i = 3) {         // error: default argument already
}                              // specified in class scope
void C::g(int i = 88, int j) { // OK: in this translation unit,
}                              // C::g can be called with no argument
C::C(int arg = 1) {   // Error: turns this into a default constructor
}

The overriders of virtual functions do not acquire the default arguments from the base class declarations, and when the virtual function call is made, the default arguments are decided based on the static type of the object (note: this can be avoided with non-virtual interface pattern).

struct Base {
    virtual void f(int a = 7);
};
struct Derived : Base {
    void f(int a) override;
};
void m() {
    Derived d;
    Base& b = d;
    b.f(); // OK: calls Derived::f(7) 
    d.f(); // Error: no default 
}

Local variables are not allowed in default arguments:

void f() 
{
    int n = 1;
    extern void g(int x = n); // error: local variable cannot be a default
}

The this pointer is not allowed in default arguments:

class A {
  void f(A* p = this) { } // error: this is not allowed
};

Non-static class members are not allowed in default arguments (even if they are not evaluated), except when used to form a pointer-to-member or in a member access expression.

int b;
class X {
  int a;
  int mem1(int i = a); // error: non-static member cannot be used
  int mem2(int i = b); // OK: lookup finds X::b, the static member
  static int b;
};

Function parameters are not allowed in default arguments (even if they are not evaluated). Note that parameters that appear earlier in the parameter list are in scope:

int a;
int f(int a, int b = a); // Error: the parameter a used in a default argument
int g(int a, int b = sizeof(a)); // Error: parameter a is used in a default argument

The default arguments are not part of the function type

int f(int = 0);
void h() {
  int j = f(1);
  int k = f();  // calls f(0);
}
int (*p1)(int) = &f;
int (*p2)()    = &f; //Error: the type of f is int(int)


If a lambda-expression appears in a default argument, it cannot explicitly or implicitly capture anything.

void f2() {
    int i = 1;
    void g1(int = ([i]{ return i; })()); // error: captures something
    void g2(int = ([i]{ return 0; })()); // error: captures something
    void g3(int = ([=]{ return i; })()); // error: captures something
    void g4(int = ([=]{ return 0; })());       // OK: capture-less
    void g5(int = ([]{ return sizeof i; })()); // OK: capture-less
}