[英]type-dependent nested-name-specifier in elaborated-type-specifier
I never saw a nested-name-specifier in an elaborated-type-specifier before this question , and at first glance I thought it wasn't even covered by the grammar. 在这个问题之前,我从未在详细说明类型说明符中看到过嵌套名称说明符,乍一看我认为它甚至没有被语法覆盖。 Now I see that since C++98 up to the present, it's translated as a special case without the typename-specifier construct.
现在我看到,从C ++ 98到现在,它被翻译为没有typename-specifier结构的特殊情况。 C++11 7.1.6.3/2 (7.1.5.3/2 in C++98):
C ++ 11 7.1.6.3/2(C ++ 98中的7.1.5.3/2):
3.4.4 describes how name lookup proceeds for the identifier in an elaborated-type-specifier .
3.4.4描述了如何在详细类型说明 符中对标识符进行名称查找。 If the identifier resolves to a class-name or enum-name , the elaborated-type-specifier introduces it into the declaration the same way a simple-type-specifier introduces its type-name .
如果标识符解析为类名或枚举名 ,则elaborated-type-specifier将它引入声明,就像simple-type-specifier引入其类型名一样 。
Therefore it would seem that although you can form a qualified elaborated-type-specifier, you need to take care that it's never type-dependent. 因此,虽然您可以形成一个合格的详细类型说明符,但您需要注意它从不依赖于类型。 Name resolution per 3.4.4 at template definition time will never find a class-name because dependent names are presumed to be objects unless prefixed with the
typename
keyword, which isn't grammatically allowed in this context. 在模板定义时,每3.4.4的名称解析将永远不会找到类名,因为除非前缀为
typename
关键字,否则依赖名称将被假定为对象,在此上下文中不允许使用语法。
Is this an accurate assessment of the meaning of the standard, and the design intent of the language? 这是对标准含义和语言设计意图的准确评估吗?
Expanding from the comments: 从评论中扩展:
Take this program as an example: 以此程序为例:
template <typename T>
struct S1 { struct I { }; };
template <typename T>
struct S2 { typedef struct S1<T>::I I; }; // okay
template <typename T>
struct S3 { typedef struct S2<T>::I I; }; // okay at definition time
// usually error at instantiation time
template <>
struct S2<short> : S1<short> { };
int main() {
S1<int>::I a;
S2<int>::I &b = a; // okay
S3<int>::I &c = b; // error
S1<short>::I d;
S2<short>::I &e = d; // okay
S3<short>::I &f = e; // okay
}
At template definition time, both S2
and S3
are okay: 14.6p5 lists the exceptions where typename
is not required. 在模板定义时,
S2
和S3
都可以:14.6p5列出了不需要typename
的异常。 Essentially: where the use is unambiguous because the name can never be anything other than a type, typename
is not required. 基本上:在使用明确的地方,因为名称永远不能是类型以外的任何名称,因此不需要
typename
。 This includes several cases where typename
is not syntactically allowed, so where requiring typename
would mean there is no way to write the program. 这包括几种在语法上不允许使用
typename
情况,因此需要typename
意味着无法编写程序。 Both typename struct S<T>::I
and struct typename S<T>::I
are hard errors, and struct S<T>::I
is unambiguous. typename struct S<T>::I
和struct typename S<T>::I
都是硬错误, struct S<T>::I
是明确的。
At template instantiation time, the rules of 3.4.4 are clearer: both struct S1<int>::I
and struct S2<int>::I
can then be found, where it becomes apparent that S2<int>::I
is a typedef, so struct S2<int>::I
is an error. 在模板实例化时,3.4.4的规则更清楚:
struct S1<int>::I
和struct S2<int>::I
然后可以找到,其中很明显S2<int>::I
是一个typedef,所以struct S2<int>::I
是一个错误。 At the same time, at that point, S2<short>::I
is seen not to be a typedef, but a struct, so struct S2<short>::I
is allowed. 同时,在那时,
S2<short>::I
被认为不是typedef,而是一个struct,所以struct S2<short>::I
是允许的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.