最烦恼的解析与合格的id - 或不? [英] Most vexing parse with a qualified-id - or not?
问题描述
请考虑:
struct Foo {
enum { bar };
explicit Foo(int){}
};
struct Baz { explicit Baz(Foo){} };
Baz b(Foo(Foo::bar)); // #1
行#1是最烦琐的解析,即使 Foo :: bar
是一个 qualified-id ,不能是有效的参数名称? Clang和GCC不同意;哪个编译器是正确的?
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?
推荐答案
Clang是对的。
令人惊讶的是,参数声明的语法允许限定符和非限定id ,因为它接受所有声明符 s:
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
并且声明器的语法允许合格 - 和非限定id 。对于函数参数名称的无 qualified-id 规则,无论更好还是更差,都是语义规则,即使可以很容易地为参数声明直接排除 qualified-id 。
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.
另请参阅 clang bug 4594 。
这篇关于最烦恼的解析与合格的id - 或不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!