Default arguments - cppreference.com

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-seqdeclarator=initializer(1) attr(optional)decl-specifier-seqabstract-declarator(optional)=initializer(2) Default arguments are used in place of the missing trailing arguments in a function call:

voidpoint(intx=3,inty=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 from the same scope:

intx(int=1,int);// Error: only the trailing parameters can have default arguments// (assuming there's no previous declaration of “x”)voidf(intn,intk=1);voidf(intn=0,intk);// OK: the default argument of “k” is provided by// the previous declaration in the same scopevoidg(int,int=7);voidh(){voidg(int=1,int);// Error: not the same scope}...unless the parameter was expanded from a parameter pack:

template<class...T>structC{voidf(intn=0,T...);};C<int>c;// OK; instantiates declaration void C::f(int n = 0, int)or be a function parameter pack:

template<class...T>voidh(inti=0,T...args);// OK(since C++11)The ellipsis is not a parameter, and so can follow a parameter with a default argument:

intg(intn=0,...);// OKDefault arguments are only allowed in the parameter lists of

function declarations

and

lambda-expressions

,(since C++11) 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 default arguments are a union of the default arguments provided in all visible declarations for the function. A redeclaration cannot introduce a default argument for a parameter for which a default argument 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.

voidf(int,int);// #1voidf(int,int=7);// #2 OK: adds a default argumentvoidh(){f(3);// #1 and #2 are in scope; makes a call to f(3,7)voidf(int=1,int);// Error: the default argument of the second// parameter is not acquired from outer scopes}voidm(){// new scope beginsvoidf(int,int);// inner scope declaration; has no default argument.f(4);// Error: not enough arguments to call f(int, int)voidf(int,int=6);f(4);// OK: calls f(4, 6);voidf(int,int=6);// Error: the second parameter already has a// default argument (even if the values are the same)}voidf(int=1,int);// #3 OK, adds a default argument to #2voidn(){// new scope beginsf();// #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 non-inline function is declared in the same namespace scope in different translation units, the corresponding default arguments must be the same if present (but some default arguments can be absent in some TU).

(since C++20)If a

friend

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

The

using-declarations

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

namespaceN{voidf(int,int=1);}usingN::f;voidg(){f(7);// calls f(7, 1);f();// error}namespaceN{voidf(int=2,int);}voidh(){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:

inta=1;intf(int);intg(intx=f(a));// lookup for f finds ::f, lookup for a finds ::a// the value of ::a, which is 1 at this point, is not usedvoidh(){a=2;// changes the value of ::a{inta=3;g();// calls f(2), then calls g() with the result}}For a

member function

of a non-

templated

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 default arguments would turn a member function into a default constructor or copy/move(since C++11) constructor/assignment operator (which makes the call ambiguous), the program is ill-formed. For member functions of templated classes, all default arguments must be provided in the initial declaration of the member function.

classC{voidf(inti=3);voidg(inti,intj=99);C(intarg);// non-default constructor};voidC::f(inti=3){}// error: default argument already// specified in class scopevoidC::g(inti=88,intj){}// OK: in this translation unit,// C::g can be called with no argumentC::C(intarg=1){}// Error: turns this into a default constructorThe 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).

structBase{virtualvoidf(inta=7);};structDerived:Base{voidf(inta)override;};voidm(){Derivedd;Base&b=d;b.f();// OK: calls Derived::f(7)d.f();// Error: no default argument}Local variables are not allowed in default arguments unless they are

not evaluated

:

voidf(){intn=1;externvoidg(intx=n);// error: local variable cannot be a default argumentexternvoidh(intx=sizeofn);// OK as of CWG 2082}The

this

pointer is not allowed in default arguments:

classA{voidf(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, or as the operand of

reflection operator

^^(since C++26):

intb;classX{inta;intmem1(inti=a);// error: non-static member cannot be usedintmem2(inti=b);// OK: lookup finds X::b, the static memberintmem3(intX::*i=&X::a);// OK: non-static member can be usedintmem4(inti=x.a);// OK: in a member access expressionconstevalintmem5(std::meta::infor=^^a);// OK since C++26: as a reflectionstaticXx;staticintb;};A default argument is evaluated each time the function is called with no argument for the corresponding parameter. Function parameters are not allowed in default arguments except if they are

not evaluated

. Note that parameters that appear earlier in the parameter list are in

scope

:

inta;intf(inta,intb=a);// Error: the parameter a used in a default argumentintg(inta,intb=sizeofa);// Error until resolving CWG 2082// OK after resolution: use in unevaluated context is OKThe default arguments are not part of the function type:

intf(int=0);voidh(){intj=f(1);intk=f();// calls f(0);}int(*p1)(int)=&f;int(*p2)()=&f;// Error: the type of f is int(int)Operator functions other than the

function call operator

and the

subscript operator

(since C++23) cannot have default arguments:

classC{intoperator++(inti=0);// ill-formedintoperator[](intj=0);// OK since C++23intoperator()(intk=0);// OK};Note

Spaces may be necessary to avoid a compound assignment token if the parameter name is absent (see

maximal munch

).

voidf1(int*=0);// Error, “*=” is unexpected herevoidg1(constint&=0);// Error, “&=” is unexpected herevoidf2(int*=0);// OKvoidg2(constint&=0);// OKvoidh(int&&=0);// OK even without spaces, “&&” is a token hereDefect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior

CWG 217

C++98 a default argument could be added to a non-
template member function of a class template prohibited

CWG 1344

C++98 default arguments added in the out-of-class definition of a
member function could change it to a special member function prohibited

CWG 1716

C++98 default arguments were evaluated each time the function
is called, even if the caller provided the arguments evaluated only if no
argument is provided for the
corresponding parameter

CWG 2082

C++98 default arguments were forbidden to use local variables
and preceding parameters in unevaluated context unevaluated context
use allowed

CWG 2233

C++11 parameters expanded from parameter packs could
not appear after parameters with default arguments allowed

CWG 2683

C++98 out-of-class definitions of the member functions of class
templates' nested classes could have default arguments prohibited