默认参数
允许在不提供一个或多个尾随参数的情况下调用函数。
通过在函数声明的参数列表中为参数使用以下语法来指示。
attr (可选) decl-specifier-seq declarator = initializer |
(1) | ||||||||
attr (可选) decl-specifier-seq abstract-declarator (可选) = initializer |
(2) | ||||||||
默认参数用于替换函数调用中缺失的尾随参数
void point(int x = 3, int y = 4); point(1, 2); // calls point(1, 2) point(1); // calls point(1, 4) point(); // calls point(3, 4)
在函数声明中,在带有默认参数的参数之后,所有后续参数都必须
- 在此或同一范围内的先前声明中提供默认参数
int x(int = 1, int); // Error: only the trailing parameters can have default arguments // (assuming there's no previous declaration of “x”) void f(int n, int k = 1); void f(int n = 0, int k); // OK: the default argument of “k” is provided by // the previous declaration in the same scope void g(int, int = 7); void h() { void g(int = 1, int); // Error: not the same scope }
template<class... T> struct C { void f(int n = 0, T...); }; C<int> c; // OK; instantiates declaration void C::f(int n = 0, int)
template<class... T> void h(int i = 0, T... args); // OK |
(C++11 起) |
省略号不是参数,因此可以跟随带有默认参数的参数
int g(int n = 0, ...); // OK
默认参数只允许在函数声明和lambda表达式的参数列表(C++11 起)中,并且不允许在函数指针、函数引用或typedef声明中。模板参数列表为其默认模板参数使用类似的语法。
对于非模板函数,如果函数在同一作用域内被重新声明,则可以将默认参数添加到已声明的函数。在函数调用点,默认参数是该函数所有可见声明中提供的默认参数的并集。重新声明不能为已可见默认参数的参数引入新的默认参数(即使值相同)。内部作用域的重新声明不会继承外部作用域的默认参数。
void f(int, int); // #1 void f(int, int = 7); // #2 OK: adds a default argument void h() { f(3); // #1 and #2 are in scope; makes a call to f(3,7) void f(int = 1, int); // Error: the default argument of the second // parameter is not acquired from outer scopes } void m() { // new scope begins void f(int, int); // inner scope declaration; has no default argument. f(4); // Error: not enough arguments to call f(int, int) void f(int, int = 6); f(4); // OK: calls f(4, 6); void f(int, int = 6); // Error: the second parameter already has a // default argument (even if the values are the same) } void f(int = 1, int); // #3 OK, adds a default argument to #2 void n() { // new scope begins f(); // #1, #2, and #3 are in scope: calls f(1, 7); }
如果内联函数在不同的翻译单元中声明,则在每个翻译单元结束时,默认参数的累积集合必须相同。
如果非内联函数在不同的翻译单元中以相同的命名空间范围声明,则如果存在对应的默认参数,它们必须相同(但某些默认参数可以在某些翻译单元中缺失)。 |
(C++20 起) |
如果友元声明指定了默认参数,则它必须是友元函数定义,并且在此函数在翻译单元中不允许有其他声明。
using-声明会继承已知的默认参数集,并且如果稍后向函数的命名空间添加了更多默认参数,则这些默认参数也会在使用声明可见的任何地方可见
namespace N { void f(int, int = 1); } using N::f; void g() { f(7); // calls f(7, 1); f(); // error } namespace N { void f(int = 2, int); } void h() { f(); // calls f(2, 1); }
默认参数中使用的名称在声明点进行查找、访问性检查和绑定,但在函数调用点执行
int a = 1; int f(int); int g(int x = f(a)); // lookup for f finds ::f, lookup for a finds ::a // the value of ::a, which is 1 at this point, is not used void h() { a = 2; // changes the value of ::a { int a = 3; g(); // calls f(2), then calls g() with the result } }
对于非模板化类的成员函数,默认参数允许在类外定义上,并与类体内部声明提供的默认参数结合。如果这些类外默认参数将成员函数变为默认构造函数或复制/移动(C++11 起)构造函数/赋值运算符(这会导致调用歧义),则程序格式不正确。对于模板化类的成员函数,所有默认参数必须在成员函数的初始声明中提供。
class C { void f(int i = 3); void g(int i, int j = 99); C(int arg); // non-default constructor }; void C::f(int i = 3) {} // error: default argument already // specified in class scope void C::g(int i = 88, int j) {} // OK: in this translation unit, // C::g can be called with no argument C::C(int arg = 1) {} // Error: turns this into a default constructor
虚函数的重写器不会从基类声明中获取默认参数,并且在进行虚函数调用时,默认参数是根据对象的静态类型决定的(注意:这可以通过非虚接口模式来避免)。
struct Base { virtual void f(int a = 7); }; struct Derived : Base { void f(int a) override; }; void m() { Derived d; Base& b = d; b.f(); // OK: calls Derived::f(7) d.f(); // Error: no default argument }
局部变量不允许在默认参数中,除非它们未被求值
void f() { int n = 1; extern void g(int x = n); // error: local variable cannot be a default argument extern void h(int x = sizeof n); // OK as of CWG 2082 }
this 指针不允许在默认参数中
class A { void f(A* p = this) {} // error: this is not allowed };
非静态类成员不允许在默认参数中(即使它们未被求值),除非用于形成指向成员的指针或在成员访问表达式中
int b; class X { int a; int mem1(int i = a); // error: non-static member cannot be used int mem2(int i = b); // OK: lookup finds X::b, the static member int mem3(int X::* i = &X::a); // OK: non-static member can be used int mem4(int i = x.a); // OK: in a member access expression static X x; static int b; };
每次函数调用时,如果对应的参数没有提供参数,则会求值默认参数。函数参数不允许在默认参数中,除非它们未被求值。请注意,参数列表中较早出现的参数在作用域内
int a; int f(int a, int b = a); // Error: the parameter a used in a default argument int g(int a, int b = sizeof a); // Error until resolving CWG 2082 // OK after resolution: use in unevaluated context is OK
默认参数不属于函数类型
int f(int = 0); void h() { int j = f(1); int k = f(); // calls f(0); } int (*p1)(int) = &f; int (*p2)() = &f; // Error: the type of f is int(int)
除了函数调用运算符和下标运算符(C++23 起)之外,运算符函数不能有默认参数
class C { int operator++(int i = 0); // ill-formed int operator[](int j = 0); // OK since C++23 int operator()(int k = 0); // OK };
显式对象参数不能有默认参数 struct S { void f(this const S& = S{}); }; // ill-formed |
(C++23 起) |
[编辑] 注意
如果参数名缺失,可能需要空格以避免复合赋值记号(参见最大贪吃)。
void f1(int*=0); // Error, “*=” is unexpected here void g1(const int&=0); // Error, “&=” is unexpected here void f2(int* = 0); // OK void g2(const int& = 0); // OK void h(int&&=0); // OK even without spaces, “&&” is a token here
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
CWG 217 | C++98 | 可以向非 模板类模板的成员函数添加默认参数 |
已禁止 |
CWG 1344 | C++98 | 在成员函数的类外定义中添加的默认参数 可能会将其更改为特殊成员函数 |
已禁止 |
CWG 1716 | C++98 | 默认参数在每次函数调用时都会被求值, 即使调用者提供了参数 |
仅在未为 对应参数提供 参数时才求值 |
CWG 2082 | C++98 | 默认参数禁止在未求值上下文中 使用局部变量和前面的参数 |
未求值上下文 允许使用 |
CWG 2233 | C++11 | 从参数包展开的参数不能 出现在带有默认参数的参数之后 |
允许 |
CWG 2683 | C++98 | 类模板嵌套类的成员函数的类外定义 可以有默认参数 |
已禁止 |