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; ?

+6
source share
1 answer

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 typename prefix, the template prefix 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-specifier template opt unqualified-ID

Plus [temp.names] / 5

The name prefixed with the template keyword 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.

+12
source

Source: https://habr.com/ru/post/971773/


All Articles