【发布时间】:2014-08-25 22:45:56
【问题描述】:
以下代码被 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++标准?
【问题讨论】:
-
B 是依赖于模板的类型模板,所以第二种语法完全正确。
-
您能否让我参考指定此用法的标准页面?谢谢。
-
@peppe 不是重复的,因为这里不需要使用
template。 -
@MikeSeymour 哦。因此,只有
A<W>::template B<int>在具有模板参数W的范围内才会出现这种情况? -
@Quentin:确实;当名称的解释取决于未知的模板参数时,在依赖的上下文中需要它。在这里,不需要它,因为模板已经专门化了,我们确切地知道
B在那个专门化中是什么。 (对不起,我有短暂的第二个想法,并删除了我的评论,说它不依赖于这里)。
标签: c++ templates c++11 compiler-construction portability