问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/28955859/most-vexing-parse-with-a-qualified-id-or-not