[dcl.fct.default] (original) (raw)
9 Declarations [dcl]
9.3 Declarators [dcl.decl]
9.3.4 Meaning of declarators [dcl.meaning]
9.3.4.7 Default arguments [dcl.fct.default]
[Note 1:
Default arguments will be used in calls where trailing arguments are missing ([expr.call]).
— _end note_]
[Example 1:
The declarationvoid point(int = 3, int = 4);declares a function that can be called with zero, one, or two arguments of typeint.
It can be called in any of these ways:point(1,2); point(1); point();
The last two calls are equivalent topoint(1,4)andpoint(3,4), respectively.
— _end example_]
A default argument shall not be specified for a template parameter pack or a function parameter pack.
For non-template functions, default arguments can be added in later declarations of a function that inhabit the same scope.
Declarations that inhabit different scopes have completely distinct sets of default arguments.
That is, declarations in inner scopes do not acquire default arguments from declarations in outer scopes, and vice versa.
In a given function declaration, each parameter subsequent to a parameter with a default argument shall have a default argument supplied in this or a previous declaration, unless the parameter was expanded from a parameter pack, or shall be a function parameter pack.
[Note 2:
A default argument cannot be redefined by a later declaration (not even to the same value) ([basic.def.odr]).
— _end note_]
[Example 2: void g(int = 0, ...); void f(int, int);void f(int, int = 7);void h() { f(3); void f(int = 1, int); } void m() { void f(int, int); f(4); void f(int, int = 5); f(4); void f(int, int = 5); } void n() { f(6); } template<class ... T> struct C { void f(int n = 0, T...);}; C<int> c; — _end example_]
For a given inline function defined in different translation units, the accumulated sets of default arguments at the end of the translation units shall be the same; no diagnostic is required.
If a friend declaration D specifies a default argument expression, that declaration shall be a definition and there shall be no other declaration of the function or function template which is reachable from D or from which D is reachable.
The default argument has the same semantic constraints as the initializer in a declaration of a variable of the parameter type, using the copy-initialization semantics ([dcl.init]).
The names in the default argument are looked up, and the semantic constraints are checked, at the point where the default argument appears, except that an immediate invocation ([expr.const]) that is a potentially-evaluated subexpression ([intro.execution]) of the initializer-clause in a parameter-declaration is neither evaluated nor checked for whether it is a constant expression at that point.
Name lookup and checking of semantic constraints for default arguments of templated functions are performed as described in [temp.inst].
[Example 3:
In the following code,gwill be called with the valuef(2):int a = 1;int f(int);int g(int x = f(a)); void h() { a = 2;{ int a = 3; g(); } }
— _end example_]
[Note 3:
A default argument is a complete-class context ([class.mem]).
Access checking applies to names in default arguments as described in [class.access].
— _end note_]
Except for member functions of templated classes, the default arguments in a member function definition that appears outside of the class definition are added to the set of default arguments provided by the member function declaration in the class definition; the program is ill-formed if a default constructor ([class.default.ctor]), copy or move constructor ([class.copy.ctor]), or copy or move assignment operator ([class.copy.assign]) is so declared.
Default arguments for a member function of a templated class shall be specified on the initial declaration of the member function within the templated class.
[Example 4: class C { void f(int i = 3);void g(int i, int j = 99);};void C::f(int i = 3) {} void C::g(int i = 88, int j) {} — _end example_]
[Note 4:
A local variable cannot be odr-used ([basic.def.odr]) in a default argument.
— _end note_]
[Example 5: void f() { int i;extern void g(int x = i); extern void h(int x = sizeof(i)); } — _end example_]
[Note 5:
The keywordthiscannot appear in a default argument of a member function; see [expr.prim.this].
[Example 6: class A { void f(A* p = this) { } }; — _end example_]
— _end note_]
A default argument is evaluated each time the function is called with no argument for the corresponding parameter.
A parameter shall not appear as a potentially-evaluated expression in a default argument.
[Note 6:
Parameters of a function declared before a default argument are in scope and can hide namespace and class member names.
— _end note_]
[Example 7: int a;int f(int a, int b = a); typedef int I;int g(float I, int b = I(2)); int h(int a, int b = sizeof(a)); — _end example_]
A non-static member shall not appear in a default argument unless it appears as the id-expression of a class member access expression ([expr.ref]) or unless it is used to form a pointer to member ([expr.unary.op]).
[Example 8:
The declaration ofX::mem1()in the following example is ill-formed because no object is supplied for the non-static memberX::aused as an initializer.
int b;class X { int a;int mem1(int i = a); int mem2(int i = b); static int b;};
The declaration ofX::mem2()is meaningful, however, since no object is needed to access the static memberX::b.
Classes, objects, and members are described in [class].
— _end example_]
A default argument is not part of the type of a function.
[Example 9: int f(int = 0);void h() { int j = f(1);int k = f(); } int (*p1)(int) = &f;int (*p2)() = &f; — _end example_]
When an overload set contains a declaration of a function that inhabits a scope S, any default argument associated with any reachable declaration that inhabits Sis available to the call.
[Note 7:
The candidate might have been found through a using-declaratorfrom which the declaration that provides the default argument is not reachable.
— _end note_]
A virtual function call ([class.virtual]) uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object.
An overriding function in a derived class does not acquire default arguments from the function it overrides.
[Example 10: struct A { virtual void f(int a = 7);};struct B : public A { void f(int a);};void m() { B* pb = new B; A* pa = pb; pa->f(); pb->f(); } — _end example_]