C++:函数重新声明是一种未定义的行为



代码:

#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;
}

输出(在g++5.1上测试(:

x:0
x:1

我的问题:

  1. int f(int x = 1);是声明还是定义
  2. 这样的函数重新声明是一种未定义的行为吗

来自dcl.fct.default中的§8.3.6:

  1. 对于非模板函数,可以在相同的范围。不同作用域中的声明具有完全不同的默认参数集

未定义的行为。你看到的是强制性的。

相关内容

最新更新