Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most vexing parse with a qualified-id - or not?

Consider:

struct Foo {
     enum { bar };
     explicit Foo(int){}
};

struct Baz { explicit Baz(Foo){} };

Baz b(Foo(Foo::bar)); // #1

Is line #1 the most vexing parse, even though Foo::bar is a qualified-id and can't possibly be a valid parameter name? Clang and GCC disagree; which compiler is correct?

like image 516
T.C. Avatar asked Mar 10 '15 03:03

T.C.


1 Answers

Clang is right.

Somewhat surprisingly, the grammar for parameter-declaration permits both qualified- and unqualified-ids, because it accepts all declarators:

parameter-declaration:
    attribute-specifier-seq_opt decl-specifier-seq declarator
    attribute-specifier-seq_opt decl-specifier-seq declarator = initializer-clause
    attribute-specifier-seq_opt decl-specifier-seq abstract-declarator_opt
    attribute-specifier-seq_opt decl-specifier-seq abstract-declarator_opt = initializer-clause

and the grammar for a declarator permits both qualified- and unqualified-ids. The "no qualified-id for function parameter names" rule, for better or worse, is a semantic rule, even though it is easily possible to write a grammar for parameter-declaration that excludes qualified-ids directly.

Just like the situation in this question, the disambiguation rule is purely syntatic, and since

Baz b(Foo(Foo::bar));

can syntatically be parsed as a function declaration, it is so parsed, even though the disambiguation in this case results in something that can never compile.

See also clang bug 4594.

like image 168
T.C. Avatar answered Oct 18 '22 03:10

T.C.