Is C ++ standard for defining a variable using `A <int> :: template B <int> x;`?
The following code is accepted by gcc, vC ++, and 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? }; Is it standard for C ++ to define a variable using A<int>::template B<int> x; ?
Despite the fact that this is a non-normative note, I think that the answer can be given n3797 [temp.names] / 6
As with the
typenameprefix, thetemplateprefix is โโallowed in cases where this is not strictly necessary; that is, when the sub-name specifier or expression to the left of->or.independent of the template parameter, or usage is not displayed in the template area.
In the OP example, the template prefix is โโused outside the scope of the template, and the previous sub-name-specifier is independent. Therefore, the template prefix is โโnot required, but is allowed here.
[expr.prim.general] / 8
Qualified-ID:
inested-name-specifiertemplateopt unqualified-ID
Plus [temp.names] / 5
The name prefixed with the
templatekeyword must be the template identifier or the name must refer to the class template.
[temp.names] / 1 says that B<int> indeed a (simple) identifier pattern.