The following code will compile and is deterministic according to cppquiz.org (Question #30)
#include <iostream>
struct X {
X() { std::cout << "X"; }
};
int main() { X x(); }
The output of the program is nothing, as
X x();
is a function declaration.
But still I wonder why this compiles though this declaration is never defined anywhere?
Since x()
is never called, there's nothing to link so no error from linker that it's not defined. It's only declared as a function taking no arguments and returning an X
: X x();
.
X x();
itself a declaration (prototype), not a function call. If a function call is made prior to seeing its declaration then it would not compile.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With