Where is the normative rule that restricts what the declaration can be in a explicit specialization?

Viewed 173

On the grammar of explicit specialization

explicit-specialization:

template < > declaration

declaration:

  • block-declaration
  • nodeclspec-function-declaration
  • ...
  • explicit-instantiation
  • ...

The declaration could be anything that is a valid declaration on its grammar. Consider this example

template<class T>
struct A{
  template<class Y>
  struct B{};
};

template<>
template struct A<int>::B<int>;  //declaration: explicit-instantiation

Both GCC and Clang cannot recognize the syntax. Where is the rule that restricts the declaration cannot be an explicit instantiation?

1 Answers

As you mention, the grammar for an explicit specialization requires a declaration after template<>. However, just because some syntax satisfies the grammar production rules, it's not necessarily valid code.

In the case of explicit specializations, the entities that can be explicitly specialized with this syntax are listed in temp.expl.spec#1

An explicit specialization of any of the following:

(1.1) function template

(1.2) class template

...

can be declared by a declaration introduced by template<> ...

The syntax template struct A<int>::B<int>; is an explicit instantiation, which is not listed here, since it's not an entity that can be explicitly specialized.

Related