Search code examples
c++compiler-theorymost-vexing-parse

Why do I need double parentheses  in constructor calls like: foo x( (bar()) );


Possible Duplicate:
Why is it an error to use an empty set of brackets to call a constructor with no arguments?

I have seen the C++ FQA entries about nested constructor calls and bracing and always wondered how C++ parsers resolve two and why it isn't possible for parsers to resolve it.

So I get why foo xxx(); is ambiguous. but what makes then foo x(bar()); ambiguous, as it is clearly no forward-declaration. (i.e.: there should be a grammar that can successfully detect this).

Could someone explain the limitations and ambiguity in that part of the C++ grammar?


Solution

  • foo x(bar());
    

    This could be either:

    1) A declaration for a variable called x whose value is a default-constructed bar. This is the same as foo x = bar();.

    2) A declaration for a function called x that returns foo and takes a single parameter -- a function that returns a bar and takes no parameters. This is the same as foo x(bar (void));