Question

I have read docs about Explicit Specialization of Class Templates and Partial Specialization of Class Templates, but don't understand what kind of specialization is used in this example (msdn links are used only due to my current environment, the question is more or less theoretical). I need the name used in c++ standard and/or links for documentation or reference to c++ standard paragraphs. The problem I'm trying to solve is quite complex to ask directly, but I have an idea how to use a similar aproach to the one used in this sample.

template<class T>
struct is_vector {
    static bool const value = false;
};

template<class T>
struct is_vector<std::vector<T>> {
    static bool const value = true;
}; 
Was it helpful?

Solution

This defines a (primary) class template is_vector<T>, and then partially specialises it for T = std::vector<U>.

The general rule is fairly simple:

Primary template:

template <something here> class someName /*no angle barckets here */ { ... }

Partial specialisation:

template <something here> class someName<otherThing here> { ... }

Explicit specialisation:

template <> class someName<something here> { ... }

There is no short piece of the standard to cite, but you can refer to the subchapter C++11[temp.class.spec]. There is nothing in that chapter that would restrict partial specialisations to pointers and references. Note that the MSDN link you gave does not limit its scope to them either; it says "such as" before the examples, which does not mean there are no other possibilities.

OTHER TIPS

It is a partial specialization. Your MSDN link describes two types of partial specialization, and this is the second :

  1. A template has multiple types and only some of them need to be specialized. The result is a template parameterized on the remaining types.
  2. A template has only one type, but a specialization is needed for pointer, reference, pointer to member, or function pointer types. The specialization itself is still a template on the type pointed to or referenced.

Why is it partial and not explicit? Because the specialized template still doesn't have all of its type parameters fully specified. The specialized version will be selected for a vector of any type T. You could further specialize it to deal with std::vector < int > - that would be an explicit specialization.

Furthermore - your MSDN link for explicit specialization says "Use partial specialization ... when you want to specialize behavior for an entire set of types, such as all pointer types, reference types, or array types." The use of 'such as' makes it clear that the list is not exhaustive. The set of types matching vector < T > is another "entire set of types".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top