Among the many things Stack Overflow has taught me is what is known as the "most vexing parse", which is classically demonstrated with a line such as
A a(B()); //declares a function While this, for most, intuitively appears to be the declaration of an object a of type A, taking a temporary B object as a constructor parameter, it's actually a declaration of a function a returning an A, taking a pointer to a function which returns B and itself takes no parameters. Similarly the line
A a(); //declares a function also falls under the same category, since instead of an object, it declares a function. Now, in the first case, the usual workaround for this issue is to add an extra set of brackets/parenthesis around the B(), as the compiler will then interpret it as the declaration of an object
A a((B())); //declares an object However, in the second case, doing the same leads to a compile error
A a(()); //compile error My question is, why? Yes I'm very well aware that the correct 'workaround' is to change it to A a;, but I'm curious to know what it is that the extra () does for the compiler in the first example which then doesn't work when reapplying it in the second example. Is the A a((B())); workaround a specific exception written into the standard?
(B())is just a C++ expression, nothing more. It's not any kind of exception. The only difference that it makes is that there's no way it can be possibly parsed as a type, and so it's not.A a();is not of the same category. For the compiler, there is never any different way to parse it: An initializer at that place never consists of empty parentheses, so this is always a function declaration.A a();is not an example of the most vexing parse. It is simply a function declaration, just like it is in C.A a;" is wrong. That won't give you initialization of a POD type. To get intitialization writeA a{};.