Reputation: 13407
I don't understand this code which should provide a different behavior in C and C++ ( Can code that is valid in both C and C++ produce different behavior when compiled in each language?)
#include <stdio.h>
struct f { };
int main() {
f();
}
int f() {
return printf("hello");
}
Why can I call f() in C++? Is it the default constructor (which I don't see by the way, is there another one "implicit"?)? In C++ that is not calling the f() function..
Upvotes: 1
Views: 390
Reputation: 21900
Every class has an implicit default constructor, unless you define other constructor. This definition of the class f
:
struct f { };
Is equivalent to:
struct f {
f() = default;
// same for copy constructors, move constructors, destructor, etc
};
So yes, inside main, you're value initializing(or default initializing, it's the same here), an object of type f
.
As of why it's not calling the function f
, well, inside main
there is no declaration nor definition of the function f
available. The only visible symbol named f
is the struct defined above.
Upvotes: 2
Reputation: 208323
In C++, the expression T()
where T
is a type is the creation of a temporary that is value-initialized. Note that this is different from a call to the constructor in general (in particular it is different for POD types).
Upvotes: 2