使用`A :: template B x;`来定义变量是否符合C标准?

前端之家收集整理的这篇文章主要介绍了使用`A :: template B x;`来定义变量是否符合C标准?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Where and why do I have to put the “template” and “typename” keywords?6个
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>定义变量是否符合C标准? X;?

解决方法

虽然这是一个非规范性的说明,但我认为答案可以由n3797 [temp.names] / 6给出.

As is the case with the typename prefix,the template prefix is allowed in cases where it is not strictly necessary; i.e.,when the nested-name-specifier or the expression on the left of the -> or . is not dependent on a template-parameter,or the use does not appear in the scope of a template.

在OP的示例中,前缀模板在模板范围之外使用,并且前面的嵌套名称说明符不依赖.因此,前缀模板不是必需的,但在此处允许.

[expr.prim.general] / 8

qualified-id:
nested-name-specifier templateopt unqualified-id

另外[temp.names] / 5

A name prefixed by the keyword template shall be a template-id or the name shall refer to a class template.

[temp.names] / 1表示B< int>确实是一个(简单的)模板ID.

原文链接:https://www.f2er.com/c/115140.html

猜你在找的C&C++相关文章