Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 654
Alexander Oh Avatar asked Dec 21 '22 19:12

Alexander Oh


2 Answers

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));

like image 89
David Schwartz Avatar answered Dec 24 '22 01:12

David Schwartz


The C++ FQA is a pile of garbage. Ignore it.

As for foo x(bar()), it is as ambiguous as foo x(), except the type of x also takes an argument, which is a function. Add a pointer here and you'll see what I mean.

foo (*x)();
foo (*x)(bar(*)());

Even the idea of allowing this is beyond silly, but nobody can change it now.

(I have a sneaking suspicion that this grammar is a tad off, but I'm sure someone will correct me).

like image 23
Puppy Avatar answered Dec 24 '22 01:12

Puppy