Reputation: 7650
Code:
#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(tested on g++ 5.1):
x:0
x:1
My question:
int f(int x = 1);
a declaration or definition?Upvotes: 7
Views: 155
Reputation: 69902
From §8.3.6 in dcl.fct.default :
- 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.
Not undefined behaviour. What you're seeing is mandated.
Upvotes: 14