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