I've never run into this before in C++ but it's odd that it still compiles but doesn't do what I expected. Can someone tell me what it does do? Please see the code, more info follows.
#include <iostream>
using namespace std;
class Test{
public:
Test();
};
Test::Test(){ cout << "ctor" << endl; }
int main(void){
Test t(); // this compiles but doesn't call the constructor
return(0);
}
It will compile, but if I try to use "t" it won't. I was only dependent on constructor functionality, and my code didn't work as expected. The solution is to lose the parenthesis "Test t();" to "Test t;". My question is what is going on in the "Test t();" example, and what does the compiler think is happening that it lets it compile.
This is the Most Vexing Parse. Basically, according to the C++ parsing rules, what you have there isn't an object of type Test
named t
, but rather a function declaration for a function t
which takes zero arguments and returns a Test
.
Incidentally, clang++ actually recognizes this situation and emits a warning, telling you that this probably isn't doing what you want.
This is a common problem that is aptly named as the most vexing parse. Your line Test t();
can be interpreted in one of two ways.
t
which is of type Test
t()
, which returns a Test
value and takes no argumentsThe C++ standard unfortunately requires the compiler to consider the second alternative, which is quite a vexing parse.
The easiest way to fix that parse is to get rid of the parenthesis and simply declare your variable as such :
Test t; // Will call the default constructor
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