码:
#include <iostream> using namespace std; int f(int x = 0) { cout << "x:" << x << endl; return 0; } int main() { f(); int f(int x = 1); f(); return 0; }
Output(在g 5.1上测试):
x:0 x:1
我的问题:
>是int f(int x = 1);声明或定义?
>函数重新声明是否像未定义的行为?
解决方法
从
dcl.fct.default的§8.3.6开始:
- For non-template functions,default arguments can be added in later declarations of a function in the
same scope. Declarations in different scopes have completely distinct sets of default arguments.
没有未定义的行为.你所看到的是强制性的.