tmp/tmpwcewbr8c/{from.md → to.md}
RENAMED
|
@@ -2,11 +2,13 @@
|
|
| 2 |
|
| 3 |
If an *initializer-clause* is specified in a *parameter-declaration*
|
| 4 |
this *initializer-clause* is used as a default argument. Default
|
| 5 |
arguments will be used in calls where trailing arguments are missing.
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
``` cpp
|
| 10 |
void point(int = 3, int = 4);
|
| 11 |
```
|
| 12 |
|
|
@@ -18,17 +20,20 @@ point(1,2); point(1); point();
|
|
| 18 |
```
|
| 19 |
|
| 20 |
The last two calls are equivalent to `point(1,4)` and `point(3,4)`,
|
| 21 |
respectively.
|
| 22 |
|
|
|
|
|
|
|
| 23 |
A default argument shall be specified only in the
|
| 24 |
-
*parameter-declaration-clause* of a function declaration or
|
| 25 |
-
*template-parameter* ([[temp.param]]); in
|
| 26 |
-
*initializer-clause* shall be an
|
| 27 |
-
argument shall not be specified for a
|
| 28 |
-
in a *parameter-declaration-clause*,
|
| 29 |
-
*declarator* or *abstract-declarator* of a
|
|
|
|
| 30 |
|
| 31 |
For non-template functions, default arguments can be added in later
|
| 32 |
declarations of a function in the same scope. Declarations in different
|
| 33 |
scopes have completely distinct sets of default arguments. That is,
|
| 34 |
declarations in inner scopes do not acquire default arguments from
|
|
@@ -37,33 +42,35 @@ declaration, each parameter subsequent to a parameter with a default
|
|
| 37 |
argument shall have a default argument supplied in this or a previous
|
| 38 |
declaration or shall be a function parameter pack. A default argument
|
| 39 |
shall not be redefined by a later declaration (not even to the same
|
| 40 |
value).
|
| 41 |
|
|
|
|
|
|
|
| 42 |
``` cpp
|
| 43 |
void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow
|
| 44 |
// a parameter with a default argument
|
| 45 |
void f(int, int);
|
| 46 |
void f(int, int = 7);
|
| 47 |
void h() {
|
| 48 |
f(3); // OK, calls f(3, 7)
|
| 49 |
-
void f(int = 1, int); // error: does not use default
|
| 50 |
-
// from surrounding scope
|
| 51 |
}
|
| 52 |
void m() {
|
| 53 |
void f(int, int); // has no defaults
|
| 54 |
f(4); // error: wrong number of arguments
|
| 55 |
void f(int, int = 5); // OK
|
| 56 |
f(4); // OK, calls f(4, 5);
|
| 57 |
-
void f(int, int = 5); // error: cannot redefine, even to
|
| 58 |
-
// same value
|
| 59 |
}
|
| 60 |
void n() {
|
| 61 |
f(6); // OK, calls f(6, 7)
|
| 62 |
}
|
| 63 |
```
|
| 64 |
|
|
|
|
|
|
|
| 65 |
For a given inline function defined in different translation units, the
|
| 66 |
accumulated sets of default arguments at the end of the translation
|
| 67 |
units shall be the same; see [[basic.def.odr]]. If a friend declaration
|
| 68 |
specifies a default argument expression, that declaration shall be a
|
| 69 |
definition and shall be the only declaration of the function or function
|
|
@@ -74,12 +81,15 @@ initializer in a declaration of a variable of the parameter type, using
|
|
| 74 |
the copy-initialization semantics ([[dcl.init]]). The names in the
|
| 75 |
default argument are bound, and the semantic constraints are checked, at
|
| 76 |
the point where the default argument appears. Name lookup and checking
|
| 77 |
of semantic constraints for default arguments in function templates and
|
| 78 |
in member functions of class templates are performed as described in
|
| 79 |
-
[[temp.inst]].
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
``` cpp
|
| 83 |
int a = 1;
|
| 84 |
int f(int);
|
| 85 |
int g(int x = f(a)); // default argument: f(::a)
|
|
@@ -91,13 +101,16 @@ void h() {
|
|
| 91 |
g(); // g(f(::a))
|
| 92 |
}
|
| 93 |
}
|
| 94 |
```
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
Except for member functions of class templates, the default arguments in
|
| 101 |
a member function definition that appears outside of the class
|
| 102 |
definition are added to the set of default arguments provided by the
|
| 103 |
member function declaration in the class definition; the program is
|
|
@@ -105,80 +118,105 @@ ill-formed if a default constructor ([[class.ctor]]), copy or move
|
|
| 105 |
constructor, or copy or move assignment operator ([[class.copy]]) is so
|
| 106 |
declared. Default arguments for a member function of a class template
|
| 107 |
shall be specified on the initial declaration of the member function
|
| 108 |
within the class template.
|
| 109 |
|
|
|
|
|
|
|
| 110 |
``` cpp
|
| 111 |
class C {
|
| 112 |
void f(int i = 3);
|
| 113 |
void g(int i, int j = 99);
|
| 114 |
};
|
| 115 |
|
| 116 |
-
void C::f(int i = 3) {
|
| 117 |
-
}
|
| 118 |
-
void C::g(int i = 88, int j) { // in this translation unit,
|
| 119 |
-
} // C::g can be called with no argument
|
| 120 |
```
|
| 121 |
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
``` cpp
|
| 125 |
void f() {
|
| 126 |
int i;
|
| 127 |
extern void g(int x = i); // error
|
|
|
|
| 128 |
// ...
|
| 129 |
}
|
| 130 |
```
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
``` cpp
|
| 136 |
class A {
|
| 137 |
void f(A* p = this) { } // error
|
| 138 |
};
|
| 139 |
```
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
A default argument is evaluated each time the function is called with no
|
| 142 |
-
argument for the corresponding parameter.
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
| 147 |
|
| 148 |
``` cpp
|
| 149 |
int a;
|
| 150 |
-
int f(int a, int b = a); // error: parameter a
|
| 151 |
-
// used as default argument
|
| 152 |
typedef int I;
|
| 153 |
int g(float I, int b = I(2)); // error: parameter I found
|
| 154 |
-
int h(int a, int b = sizeof(a)); //
|
| 155 |
-
// in default argument
|
| 156 |
```
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
``` cpp
|
| 166 |
int b;
|
| 167 |
class X {
|
| 168 |
int a;
|
| 169 |
-
int mem1(int i = a);
|
| 170 |
-
// used as default argument
|
| 171 |
int mem2(int i = b); // OK; use X::b
|
| 172 |
static int b;
|
| 173 |
};
|
| 174 |
```
|
| 175 |
|
| 176 |
The declaration of `X::mem2()` is meaningful, however, since no object
|
| 177 |
is needed to access the static member `X::b`. Classes, objects, and
|
| 178 |
-
members are described in Clause [[class]].
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
``` cpp
|
| 182 |
int f(int = 0);
|
| 183 |
|
| 184 |
void h() {
|
|
@@ -188,10 +226,12 @@ void h() {
|
|
| 188 |
|
| 189 |
int (*p1)(int) = &f;
|
| 190 |
int (*p2)() = &f; // error: type mismatch
|
| 191 |
```
|
| 192 |
|
|
|
|
|
|
|
| 193 |
When a declaration of a function is introduced by way of a
|
| 194 |
*using-declaration* ([[namespace.udecl]]), any default argument
|
| 195 |
information associated with the declaration is made known as well. If
|
| 196 |
the function is redeclared thereafter in the namespace with additional
|
| 197 |
default arguments, the additional arguments are also known at any point
|
|
@@ -201,10 +241,12 @@ A virtual function call ([[class.virtual]]) uses the default arguments
|
|
| 201 |
in the declaration of the virtual function determined by the static type
|
| 202 |
of the pointer or reference denoting the object. An overriding function
|
| 203 |
in a derived class does not acquire default arguments from the function
|
| 204 |
it overrides.
|
| 205 |
|
|
|
|
|
|
|
| 206 |
``` cpp
|
| 207 |
struct A {
|
| 208 |
virtual void f(int a = 7);
|
| 209 |
};
|
| 210 |
struct B : public A {
|
|
@@ -216,5 +258,7 @@ void m() {
|
|
| 216 |
pa->f(); // OK, calls pa->B::f(7)
|
| 217 |
pb->f(); // error: wrong number of arguments for B::f()
|
| 218 |
}
|
| 219 |
```
|
| 220 |
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
If an *initializer-clause* is specified in a *parameter-declaration*
|
| 4 |
this *initializer-clause* is used as a default argument. Default
|
| 5 |
arguments will be used in calls where trailing arguments are missing.
|
| 6 |
|
| 7 |
+
[*Example 1*:
|
| 8 |
+
|
| 9 |
+
The declaration
|
| 10 |
|
| 11 |
``` cpp
|
| 12 |
void point(int = 3, int = 4);
|
| 13 |
```
|
| 14 |
|
|
|
|
| 20 |
```
|
| 21 |
|
| 22 |
The last two calls are equivalent to `point(1,4)` and `point(3,4)`,
|
| 23 |
respectively.
|
| 24 |
|
| 25 |
+
— *end example*]
|
| 26 |
+
|
| 27 |
A default argument shall be specified only in the
|
| 28 |
+
*parameter-declaration-clause* of a function declaration or
|
| 29 |
+
*lambda-declarator* or in a *template-parameter* ([[temp.param]]); in
|
| 30 |
+
the latter case, the *initializer-clause* shall be an
|
| 31 |
+
*assignment-expression*. A default argument shall not be specified for a
|
| 32 |
+
parameter pack. If it is specified in a *parameter-declaration-clause*,
|
| 33 |
+
it shall not occur within a *declarator* or *abstract-declarator* of a
|
| 34 |
+
*parameter-declaration*.[^10]
|
| 35 |
|
| 36 |
For non-template functions, default arguments can be added in later
|
| 37 |
declarations of a function in the same scope. Declarations in different
|
| 38 |
scopes have completely distinct sets of default arguments. That is,
|
| 39 |
declarations in inner scopes do not acquire default arguments from
|
|
|
|
| 42 |
argument shall have a default argument supplied in this or a previous
|
| 43 |
declaration or shall be a function parameter pack. A default argument
|
| 44 |
shall not be redefined by a later declaration (not even to the same
|
| 45 |
value).
|
| 46 |
|
| 47 |
+
[*Example 2*:
|
| 48 |
+
|
| 49 |
``` cpp
|
| 50 |
void g(int = 0, ...); // OK, ellipsis is not a parameter so it can follow
|
| 51 |
// a parameter with a default argument
|
| 52 |
void f(int, int);
|
| 53 |
void f(int, int = 7);
|
| 54 |
void h() {
|
| 55 |
f(3); // OK, calls f(3, 7)
|
| 56 |
+
void f(int = 1, int); // error: does not use default from surrounding scope
|
|
|
|
| 57 |
}
|
| 58 |
void m() {
|
| 59 |
void f(int, int); // has no defaults
|
| 60 |
f(4); // error: wrong number of arguments
|
| 61 |
void f(int, int = 5); // OK
|
| 62 |
f(4); // OK, calls f(4, 5);
|
| 63 |
+
void f(int, int = 5); // error: cannot redefine, even to same value
|
|
|
|
| 64 |
}
|
| 65 |
void n() {
|
| 66 |
f(6); // OK, calls f(6, 7)
|
| 67 |
}
|
| 68 |
```
|
| 69 |
|
| 70 |
+
— *end example*]
|
| 71 |
+
|
| 72 |
For a given inline function defined in different translation units, the
|
| 73 |
accumulated sets of default arguments at the end of the translation
|
| 74 |
units shall be the same; see [[basic.def.odr]]. If a friend declaration
|
| 75 |
specifies a default argument expression, that declaration shall be a
|
| 76 |
definition and shall be the only declaration of the function or function
|
|
|
|
| 81 |
the copy-initialization semantics ([[dcl.init]]). The names in the
|
| 82 |
default argument are bound, and the semantic constraints are checked, at
|
| 83 |
the point where the default argument appears. Name lookup and checking
|
| 84 |
of semantic constraints for default arguments in function templates and
|
| 85 |
in member functions of class templates are performed as described in
|
| 86 |
+
[[temp.inst]].
|
| 87 |
+
|
| 88 |
+
[*Example 3*:
|
| 89 |
+
|
| 90 |
+
In the following code, `g` will be called with the value `f(2)`:
|
| 91 |
|
| 92 |
``` cpp
|
| 93 |
int a = 1;
|
| 94 |
int f(int);
|
| 95 |
int g(int x = f(a)); // default argument: f(::a)
|
|
|
|
| 101 |
g(); // g(f(::a))
|
| 102 |
}
|
| 103 |
}
|
| 104 |
```
|
| 105 |
|
| 106 |
+
— *end example*]
|
| 107 |
+
|
| 108 |
+
[*Note 1*: In member function declarations, names in default arguments
|
| 109 |
+
are looked up as described in [[basic.lookup.unqual]]. Access checking
|
| 110 |
+
applies to names in default arguments as described in Clause
|
| 111 |
+
[[class.access]]. — *end note*]
|
| 112 |
|
| 113 |
Except for member functions of class templates, the default arguments in
|
| 114 |
a member function definition that appears outside of the class
|
| 115 |
definition are added to the set of default arguments provided by the
|
| 116 |
member function declaration in the class definition; the program is
|
|
|
|
| 118 |
constructor, or copy or move assignment operator ([[class.copy]]) is so
|
| 119 |
declared. Default arguments for a member function of a class template
|
| 120 |
shall be specified on the initial declaration of the member function
|
| 121 |
within the class template.
|
| 122 |
|
| 123 |
+
[*Example 4*:
|
| 124 |
+
|
| 125 |
``` cpp
|
| 126 |
class C {
|
| 127 |
void f(int i = 3);
|
| 128 |
void g(int i, int j = 99);
|
| 129 |
};
|
| 130 |
|
| 131 |
+
void C::f(int i = 3) {} // error: default argument already specified in class scope
|
| 132 |
+
void C::g(int i = 88, int j) {} // in this translation unit, C::g can be called with no argument
|
|
|
|
|
|
|
| 133 |
```
|
| 134 |
|
| 135 |
+
— *end example*]
|
| 136 |
+
|
| 137 |
+
A local variable shall not appear as a potentially-evaluated expression
|
| 138 |
+
in a default argument.
|
| 139 |
+
|
| 140 |
+
[*Example 5*:
|
| 141 |
|
| 142 |
``` cpp
|
| 143 |
void f() {
|
| 144 |
int i;
|
| 145 |
extern void g(int x = i); // error
|
| 146 |
+
extern void h(int x = sizeof(i)); // OK
|
| 147 |
// ...
|
| 148 |
}
|
| 149 |
```
|
| 150 |
|
| 151 |
+
— *end example*]
|
| 152 |
+
|
| 153 |
+
[*Note 2*:
|
| 154 |
+
|
| 155 |
+
The keyword `this` may not appear in a default argument of a member
|
| 156 |
+
function; see [[expr.prim.this]].
|
| 157 |
+
|
| 158 |
+
[*Example 6*:
|
| 159 |
|
| 160 |
``` cpp
|
| 161 |
class A {
|
| 162 |
void f(A* p = this) { } // error
|
| 163 |
};
|
| 164 |
```
|
| 165 |
|
| 166 |
+
— *end example*]
|
| 167 |
+
|
| 168 |
+
— *end note*]
|
| 169 |
+
|
| 170 |
A default argument is evaluated each time the function is called with no
|
| 171 |
+
argument for the corresponding parameter. A parameter shall not appear
|
| 172 |
+
as a potentially-evaluated expression in a default argument. Parameters
|
| 173 |
+
of a function declared before a default argument are in scope and can
|
| 174 |
+
hide namespace and class member names.
|
| 175 |
+
|
| 176 |
+
[*Example 7*:
|
| 177 |
|
| 178 |
``` cpp
|
| 179 |
int a;
|
| 180 |
+
int f(int a, int b = a); // error: parameter a used as default argument
|
|
|
|
| 181 |
typedef int I;
|
| 182 |
int g(float I, int b = I(2)); // error: parameter I found
|
| 183 |
+
int h(int a, int b = sizeof(a)); // OK, unevaluated operand
|
|
|
|
| 184 |
```
|
| 185 |
|
| 186 |
+
— *end example*]
|
| 187 |
+
|
| 188 |
+
A non-static member shall not appear in a default argument unless it
|
| 189 |
+
appears as the *id-expression* of a class member access expression (
|
| 190 |
+
[[expr.ref]]) or unless it is used to form a pointer to member (
|
| 191 |
+
[[expr.unary.op]]).
|
| 192 |
+
|
| 193 |
+
[*Example 8*:
|
| 194 |
+
|
| 195 |
+
The declaration of `X::mem1()` in the following example is ill-formed
|
| 196 |
+
because no object is supplied for the non-static member `X::a` used as
|
| 197 |
+
an initializer.
|
| 198 |
|
| 199 |
``` cpp
|
| 200 |
int b;
|
| 201 |
class X {
|
| 202 |
int a;
|
| 203 |
+
int mem1(int i = a); // error: non-static member a used as default argument
|
|
|
|
| 204 |
int mem2(int i = b); // OK; use X::b
|
| 205 |
static int b;
|
| 206 |
};
|
| 207 |
```
|
| 208 |
|
| 209 |
The declaration of `X::mem2()` is meaningful, however, since no object
|
| 210 |
is needed to access the static member `X::b`. Classes, objects, and
|
| 211 |
+
members are described in Clause [[class]].
|
| 212 |
+
|
| 213 |
+
— *end example*]
|
| 214 |
+
|
| 215 |
+
A default argument is not part of the type of a function.
|
| 216 |
+
|
| 217 |
+
[*Example 9*:
|
| 218 |
|
| 219 |
``` cpp
|
| 220 |
int f(int = 0);
|
| 221 |
|
| 222 |
void h() {
|
|
|
|
| 226 |
|
| 227 |
int (*p1)(int) = &f;
|
| 228 |
int (*p2)() = &f; // error: type mismatch
|
| 229 |
```
|
| 230 |
|
| 231 |
+
— *end example*]
|
| 232 |
+
|
| 233 |
When a declaration of a function is introduced by way of a
|
| 234 |
*using-declaration* ([[namespace.udecl]]), any default argument
|
| 235 |
information associated with the declaration is made known as well. If
|
| 236 |
the function is redeclared thereafter in the namespace with additional
|
| 237 |
default arguments, the additional arguments are also known at any point
|
|
|
|
| 241 |
in the declaration of the virtual function determined by the static type
|
| 242 |
of the pointer or reference denoting the object. An overriding function
|
| 243 |
in a derived class does not acquire default arguments from the function
|
| 244 |
it overrides.
|
| 245 |
|
| 246 |
+
[*Example 10*:
|
| 247 |
+
|
| 248 |
``` cpp
|
| 249 |
struct A {
|
| 250 |
virtual void f(int a = 7);
|
| 251 |
};
|
| 252 |
struct B : public A {
|
|
|
|
| 258 |
pa->f(); // OK, calls pa->B::f(7)
|
| 259 |
pb->f(); // error: wrong number of arguments for B::f()
|
| 260 |
}
|
| 261 |
```
|
| 262 |
|
| 263 |
+
— *end example*]
|
| 264 |
+
|