C ++: re-declaring a function a undefined?

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

Exit (tested on g ++ 5.1):

 x:0 x:1 

My question is:

  • Is int f(int x = 1); declaration or definition?
  • Is re-declaring a function like undefined behavior?
+7
c ++
source share
1 answer

From Β§8.3.6 to dcl.fct.default :

  1. For functions other than templates, default arguments can be added in later function declarations to the same extent. Ads in different areas have completely different default argument sets.

Not undefined behavior. What you see is required.

+14
source share

All Articles