使用"A:<int>:template B x;"定义变量是否C++符合标准<int>?
Is it C++ standard-compliant to define a variable using `A<int>::template B<int> x;`?
下面的代码被gcc、vc++和clang接受。
template<class T>
struct A
{
template<class U>
struct B
{};
};
int main()
{
A<int>::B<int> y; // OK as expected
A<int>::template B<int> x; // Also OK! Is this standard-compliant?
};
使用A<int>::template B<int> x;
定义变量是否符合c++标准?
尽管这是一个非规范性的注释,我认为可以通过n3797[temp.names]/6给出答案
与
typename
前缀的情况一样,template
前缀在不是严格必要的情况下是允许的;即,当嵌套名称说明符或->
或.
左侧的表达式不依赖于模板参数,或使用不在模板范围内时。
在OP的示例中,前缀template
在模板作用域之外使用,前面的嵌套名称说明符不是依赖的。因此,前缀template
不是必需的,但在这里是允许的。
[expr.prim.general]/8
限定标识:
,,,,nested-name-specifiertemplate
opt unqualified-id
+ [temp.names]/5
以关键字
template
为前缀的名称应该是模板id,或者该名称应该引用一个类模板。
[temp.names]/1说B<int>
确实是一个(simple-)模板id。
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 'short int'持有的值溢出,但"自动"不会溢出?
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 调用'begin(int [n])'没有匹配函数
- 没有显式声明的int[]中的foreach
- 在c++中访问int到类对象的映射时出错
- 为什么我无法更改"set<set>"循环中的值<int>
- EASTL矢量<向量<int>>连续的
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- 'structstd::对<int,int>'没有名为'push_back'