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 C++ standard-compliant to define a variable using A<int>::template B<int> x;
?
Even though it is a non-normative note, I think an answer can be given by n3797[temp.names]/6
As is the case with the
typename
prefix, thetemplate
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.
In the OP's example, the prefix template
is used outside the scope of a template, and the preceding nested-name-specifier is not dependent. Therefore, the prefix template
is not required, but allowed here.
[expr.prim.general]/8
qualified-id:
nested-name-specifiertemplate
opt unqualified-id
Plus [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 says that B<int>
is indeed a (simple-)template-id.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With