Skip to content

Commit 2aee7f4

Browse files
committed
feat: migrate cpp/language/functions/default_arguments
1 parent ac877e0 commit 2aee7f4

File tree

1 file changed

+374
-0
lines changed

1 file changed

+374
-0
lines changed
Lines changed: 374 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,374 @@
1+
---
2+
title: Default arguments
3+
sidebar:
4+
order: 3
5+
cppdoc:
6+
keys: ["cpp.lang.functions.default_arguments"]
7+
---
8+
9+
import { Decl, DeclDoc } from "@components/decl-doc";
10+
import { DR, DRList } from "@components/defect-report";
11+
import { Revision, RevisionBlock } from "@components/revision";
12+
import Behavior from "@components/Behavior.astro";
13+
import DocLink from "@components/DocLink.astro";
14+
15+
<DeclDoc>
16+
<Decl slot="decl">
17+
<RevisionBlock until="C++11" noborder>
18+
```cpp cxx-mark
19+
/*$s:decl-specifier-seq*/ /*$s:declarator*/
20+
= /*$s:initializer*/
21+
```
22+
</RevisionBlock>
23+
</Decl>
24+
<Decl slot="decl">
25+
<RevisionBlock since="C++11" noborder>
26+
```cpp cxx-mark
27+
/*$s:attr*//*$opt*/
28+
/*$s:decl-specifier-seq*/ /*$s:declarator*/
29+
= /*$s:initializer*/
30+
```
31+
</RevisionBlock>
32+
</Decl>
33+
<Decl slot="decl">
34+
<RevisionBlock until="C++11" noborder>
35+
```cpp cxx-mark
36+
/*$s:decl-specifier-seq*/ /*$s:abstract-declarator*//*$opt*/
37+
= /*$s:initializer*/
38+
```
39+
</RevisionBlock>
40+
</Decl>
41+
<Decl slot="decl">
42+
<RevisionBlock since="C++11" noborder>
43+
```cpp cxx-mark
44+
/*$s:attr*//*$opt*/
45+
/*$s:decl-specifier-seq*/ /*$s:abstract-declarator*//*$opt*/
46+
= /*$s:initializer*/
47+
```
48+
</RevisionBlock>
49+
</Decl>
50+
51+
Allows a function to be called without providing one or more trailing arguments.
52+
53+
Indicated by using the syntax above for a parameter in the `parameter-list` of a <DocLink src="/cpp/language/functions/function">function declaration</DocLink>.
54+
</DeclDoc>
55+
56+
Default arguments are used in place of the missing trailing arguments in a function call:
57+
58+
```cpp
59+
void point(int x = 3, int y = 4);
60+
61+
point(1, 2); // calls point(1, 2)
62+
point(1); // calls point(1, 4)
63+
point(); // calls point(3, 4)
64+
```
65+
66+
In a function declaration, after a parameter with a default argument, all subsequent parameters must:
67+
68+
- have a default argument supplied in this or a previous declaration from the same scope<Revision since="C++11"> unless the parameter was expanded from a parameter pack</Revision>:
69+
70+
```cpp
71+
int x(int = 1, int); // Error: only the trailing parameters can have default arguments
72+
// (assuming there's no previous declaration of “x”)
73+
74+
void f(int n, int k = 1);
75+
void f(int n = 0, int k); // OK: the default argument of “k” is provided by
76+
// the previous declaration in the same scope
77+
78+
void g(int, int = 7);
79+
80+
void h() {
81+
void g(int = 1, int); // Error: not the same scope
82+
}
83+
```
84+
85+
<RevisionBlock since="C++11" vertical>
86+
```cpp
87+
template<class... T>
88+
struct C { void f(int n = 0, T...); };
89+
90+
C<int> c; // OK; instantiates declaration void C::f(int n = 0, int)
91+
```
92+
</RevisionBlock>
93+
94+
<RevisionBlock since="C++11" vertical>
95+
- or be a function parameter pack:
96+
97+
```cpp
98+
template<class... T>
99+
void h(int i = 0, T... args); // OK
100+
```
101+
</RevisionBlock>
102+
103+
The ellipsis is not a parameter, and so can follow a parameter with a default argument:
104+
105+
```cpp
106+
int g(int n = 0, ...); // OK
107+
```
108+
109+
Default arguments are only allowed in the parameter lists of <DocLink src="/cpp/language/functions/function">function declarations</DocLink><Revision since="C++11"> and <DocLink src="/cpp/language/functions/lambda">lambda-expressions</DocLink>,</Revision> and are not allowed in the declarations of pointers to functions, references to functions, or in <DocLink src="/cpp/language/declarations/typedef">typedef</DocLink> declarations. Template parameter lists use similar syntax for their <DocLink src="/cpp/language/templates/template_parameters" section="default-template-arguments">default template arguments</DocLink>.
110+
111+
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.
112+
113+
```cpp
114+
void f(int, int); // #1
115+
void f(int, int = 7); // #2 OK: adds a default argument
116+
117+
void h() {
118+
f(3); // #1 and #2 are in scope; makes a call to f(3,7)
119+
void f(int = 1, int); // Error: the default argument of the second
120+
// parameter is not acquired from outer scopes
121+
}
122+
123+
void m() { // new scope begins
124+
void f(int, int); // inner scope declaration; has no default argument.
125+
f(4); // Error: not enough arguments to call f(int, int)
126+
void f(int, int = 6);
127+
f(4); // OK: calls f(4, 6);
128+
void f(int, int = 6); // Error: the second parameter already has a
129+
// default argument (even if the values are the same)
130+
}
131+
132+
void f(int = 1, int); // #3 OK, adds a default argument to #2
133+
134+
void n() { // new scope begins
135+
f(); // #1, #2, and #3 are in scope: calls f(1, 7);
136+
}
137+
```
138+
139+
If an <DocLink src="/cpp/language/declarations/inline">inline</DocLink> function is declared in different translation units, the accumulated sets of default arguments must be the same at the end of each translation unit.
140+
141+
<RevisionBlock since="C++20">
142+
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).
143+
</RevisionBlock>
144+
145+
If a <DocLink src="/cpp/language/classes/friend">friend</DocLink> 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.
146+
147+
The <DocLink src="/cpp/language/declarations/namespace" section="using-declarations">using-declarations</DocLink> 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:
148+
149+
```cpp
150+
namespace N {
151+
void f(int, int = 1);
152+
}
153+
154+
using N::f;
155+
156+
void g() {
157+
f(7); // calls f(7, 1);
158+
f(); // error
159+
}
160+
161+
namespace N {
162+
void f(int = 2, int);
163+
}
164+
165+
void h() {
166+
f(); // calls f(2, 1);
167+
}
168+
```
169+
170+
The names used in the default arguments are looked up, checked for <DocLink src="/cpp/language/classes/access">accessibility</DocLink>, and bound at the point of declaration, but are executed at the point of the function call:
171+
172+
```cpp
173+
int a = 1;
174+
175+
int f(int);
176+
177+
int g(int x = f(a)); // lookup for f finds ::f, lookup for a finds ::a
178+
// the value of ::a, which is 1 at this point, is not used
179+
180+
void h() {
181+
a = 2; // changes the value of ::a
182+
{
183+
int a = 3;
184+
g(); // calls f(2), then calls g() with the result
185+
}
186+
}
187+
```
188+
189+
For a <DocLink src="/cpp/language/classes/member_functions">member function</DocLink> of a non-<DocLink src="/cpp/language/templates" section="templated-entity">templated</DocLink> 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<Revision since="C++11">/move</Revision> constructor/assignment operator (which makes the call ambiguous), the program is <Behavior kind="ill-formed">ill-formed</Behavior>. For member functions of templated classes, all default arguments must be provided in the initial declaration of the member function.
190+
191+
```cpp
192+
class C {
193+
void f(int i = 3);
194+
void g(int i, int j = 99);
195+
C(int arg); // non-default constructor
196+
};
197+
198+
void C::f(int i = 3) {} // error: default argument already
199+
// specified in class scope
200+
201+
void C::g(int i = 88, int j) {} // OK: in this translation unit,
202+
// C::g can be called with no argument
203+
204+
C::C(int arg = 1) {} // Error: turns this into a default constructor
205+
```
206+
207+
The overriders of <DocLink src="/cpp/language/classes/virtual">virtual</DocLink> 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](http://www.gotw.ca/publications/mill18.htm) pattern).
208+
209+
```cpp
210+
struct Base {
211+
virtual void f(int a = 7);
212+
};
213+
214+
struct Derived : Base {
215+
void f(int a) override;
216+
};
217+
218+
void m() {
219+
Derived d;
220+
Base& b = d;
221+
b.f(); // OK: calls Derived::f(7)
222+
d.f(); // Error: no default argument
223+
}
224+
```
225+
226+
Local variables are not allowed in default arguments unless they are <DocLink src="/cpp/language/expressions" section="potentially-evaluated-expressions">not evaluated</DocLink>:
227+
228+
```cpp
229+
void f() {
230+
int n = 1;
231+
extern void g(int x = n); // error: local variable cannot be a default argument
232+
extern void h(int x = sizeof n); // OK as of CWG 2082
233+
}
234+
```
235+
236+
The <DocLink src="/cpp/language/classes/this">this</DocLink> pointer is not allowed in default arguments:
237+
238+
```cpp
239+
class A {
240+
void f(A* p = this) {} // error: this is not allowed
241+
};
242+
```
243+
244+
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:
245+
246+
```cpp
247+
int b;
248+
249+
class X {
250+
int a;
251+
int mem1(int i = a); // error: non-static member cannot be used
252+
int mem2(int i = b); // OK: lookup finds X::b, the static member
253+
int mem3(int X::* i = &X::a); // OK: non-static member can be used
254+
int mem4(int i = x.a); // OK: in a member access expression
255+
256+
static X x;
257+
static int b;
258+
};
259+
```
260+
261+
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 <DocLink src="/cpp/language/expressions" section="potentially-evaluated-expressions">not evaluated</DocLink>. Note that parameters that appear earlier in the parameter list are in <DocLink src="/cpp/language/basic_concepts/scope">scope</DocLink>:
262+
263+
```cpp
264+
int a;
265+
266+
int f(int a, int b = a); // Error: the parameter a used in a default argument
267+
268+
int g(int a, int b = sizeof a); // Error until resolving CWG 2082
269+
// OK after resolution: use in unevaluated context is OK
270+
```
271+
272+
The default arguments are not part of the function type:
273+
274+
```cpp
275+
int f(int = 0);
276+
277+
void h() {
278+
int j = f(1);
279+
int k = f(); // calls f(0);
280+
}
281+
282+
int (*p1)(int) = &f;
283+
int (*p2)() = &f; // Error: the type of f is int(int)
284+
```
285+
286+
Operator functions other than the <DocLink src="/cpp/language/expressions/operator_other" section="built-in-function-call-operator">function call operator</DocLink><Revision since="C++23"> and the <DocLink src="/cpp/language/expressions/operator_member_access" section="built-in-subscript-operator">subscript operator</DocLink></Revision> cannot have default arguments:
287+
288+
```cpp
289+
class C {
290+
int operator++(int i = 0); // ill-formed
291+
int operator[](int j = 0); // OK since C++23
292+
int operator()(int k = 0); // OK
293+
};
294+
```
295+
296+
<RevisionBlock since="C++23" vertical>
297+
<DocLink src="/cpp/language/functions/function" section="explicit-object-parameter">Explicit object parameters</DocLink> cannot have default arguments:
298+
299+
```cpp
300+
struct S { void f(this const S& = S{}); }; // ill-formed
301+
```
302+
</RevisionBlock>
303+
304+
## Notes
305+
306+
Spaces may be necessary to avoid a compound assignment token if the parameter name is absent (see <DocLink src="/cpp/language/basic_concepts/translation_phases" section="maximal-munch">maximal munch</DocLink>).
307+
308+
```cpp
309+
void f1(int*=0); // Error, “*=” is unexpected here
310+
void g1(const int&=0); // Error, “&=” is unexpected here
311+
void f2(int* = 0); // OK
312+
void g2(const int& = 0); // OK
313+
void h(int&&=0); // OK even without spaces, “&&” is a token here
314+
```
315+
316+
## Defect reports
317+
318+
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
319+
320+
<DRList>
321+
<DR kind="cwg" id={217} std="C++98">
322+
<Fragment slot="behavior-published">
323+
a default argument could be added to a non-template member function of a class template
324+
</Fragment>
325+
<Fragment slot="correct-behavior">
326+
prohibited
327+
</Fragment>
328+
</DR>
329+
330+
<DR kind="cwg" id={1344} std="C++98">
331+
<Fragment slot="behavior-published">
332+
default arguments added in the out-of-class definition of a member function could change it to a special member function
333+
</Fragment>
334+
<Fragment slot="correct-behavior">
335+
prohibited
336+
</Fragment>
337+
</DR>
338+
339+
<DR kind="cwg" id={1716} std="C++98">
340+
<Fragment slot="behavior-published">
341+
default arguments were evaluated each time the function is called, even if the caller provided the arguments
342+
</Fragment>
343+
<Fragment slot="correct-behavior">
344+
evaluated only if no argument is provided for the corresponding parameter
345+
</Fragment>
346+
</DR>
347+
348+
<DR kind="cwg" id={2082} std="C++98">
349+
<Fragment slot="behavior-published">
350+
default arguments were forbidden to use local variables and preceding parameters in unevaluated context
351+
</Fragment>
352+
<Fragment slot="correct-behavior">
353+
unevaluated context use allowed
354+
</Fragment>
355+
</DR>
356+
357+
<DR kind="cwg" id={2233} std="C++11">
358+
<Fragment slot="behavior-published">
359+
parameters expanded from parameter packs could not appear after parameters with default arguments
360+
</Fragment>
361+
<Fragment slot="correct-behavior">
362+
allowed
363+
</Fragment>
364+
</DR>
365+
366+
<DR kind="cwg" id={2683} std="C++98">
367+
<Fragment slot="behavior-published">
368+
out-of-class definitions of the member functions of class templates' nested classes could have default arguments
369+
</Fragment>
370+
<Fragment slot="correct-behavior">
371+
prohibited
372+
</Fragment>
373+
</DR>
374+
</DRList>

0 commit comments

Comments
 (0)