Template class specialization with template class

644 Views Asked by At

Related questions:

Consider the following code:

  template <typename T>
  struct is_std_vector: std::false_type { };

  template<typename ValueType>
  struct is_std_vector<std::vector<ValueType>>: std::true_type { };

Why is such template class specialization syntax correct? The following seems more logical:

  template <typename T>
  struct is_std_vector: std::false_type { };

  template<> //--- because it is is_std_vector specialization
  template<typename ValueType>
  struct is_std_vector<std::vector<ValueType>>: std::true_type { };
1

There are 1 best solutions below

0
On BEST ANSWER

Class template partial specialization syntax closely mirrors function template syntax. Indeed, the rules for ordering class template partial specializations is based on function template partial ordering.

The way you would write a function taking a vector<T> is:

template <class T>
void is_std_vector(vector<T> ) { ... }

So the way you write a specialization on vector<T> is the same:

template <class T>
class is_std_vector<vector<T>> { ... };

Matching the specialization of is_std_vector would try to deduce T in vector<T> from some type argument A, so it makes a lot of sense that they're written the same way.

For full specializations, we use template <> as placeholder signal to make full specializations look similar to partial specializations. I'm not sure what purpose an extra template <> would serve in this particular case.