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

社会主义新天地 提交于 2019-12-07 06:15:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!