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? Thanks.