c++0x: initializer lists and ambiguity

Olivier JG olivier.jg at gmail.com
Mon Aug 15 08:03:23 UTC 2011


On 08/11/2011 09:20 PM, Milian Wolff wrote:

You may have already figured this one out but...
> <SNIP>
> 
> So this should be possible to be parsed as a simple-declaration. But
> this actually is supposed to be a function-definition followed by an
> empty declaration:
> 
> "void foo() {}" -> function-definition ";" -> empty-declaration
> 
> Looking at http://www.nongnu.org/hcb/#declaration you will see that
> the list first mentions block-declaration and hence
> simple-declaration and function- definition only afterwards. Should
> this list be parsed from the end to resolve this issue?

I'm not sure what order that list in, but I'm fairly certain that it's
got nothing to do with the order in which you should parse.
In this case, it's true that it doesn't specifically disallow the
optional braced-init-list after parameters-and-qualifiers (which could
be considered ambiguous).
It does say this though:
"
7.7b If the decl-specifier-seq contains no typedef specifier, the
declaration is called a function declaration if the type associated with
the name is a function type (8.3.5) and an object declaration otherwise.
7.8a Syntactic components beyond those found in the general form of
declaration are added to a function declaration to make a
function-definition.
"

IOW, it's pretty clearly a function-definition. Also, I don't think that
list's order is a parsing guide.

On another note, though on that site you linked, elements may be listed
as optional, the standard gives specific guidance on just in what
circumstances any given element is or isn't optional (though again,
strangely absent in this particular case unfortunately).

> Or is "6.8 Ambiguite resolution" of the spec applying here:
>> There is an ambiguity in the grammar involving
>> expression-statements and declarations: An expression- statement
>> with a function-style explicit type conversion (5.2.3) as its
>> leftmost subexpression can be indis- tinguishable from a
>> declaration where the first declarator starts with a (. In those
>> cases the statement is a declaration. [ Note: To disambiguate, the
>> whole statement might have to be examined to determine if it is an 
>> expression-statement or a declaration. This disambiguates many
>> examples.

Unrelated to your problem, this is the
http://en.wikipedia.org/wiki/Most_vexing_parse

-Olivier JG




More information about the KDevelop-devel mailing list