A Class Template Specialization

Get an overview of how a class template specialization should name a type.

Require a nested type

Just like for the nested types, we have to use the keyword typename along with the specialized class template that is expected to be a valid specialization:

template<typename T>
concept TypeRequirement = requires {
    typename Other<T>;
};

In this case, the concept TypeRequirement requires that the class template Other can be instantiated with T. Let’s see how it works.

Declare a class template

We declare a class template Other and make a requirement on the template parameter by making sure that Other cannot be instantiated with a vector of int values.

template <typename T>
requires (!std::same_as<T, std::vector<int>>) // parentheses are mandatory!
struct Other{};

Now, the line TypeRequirement auto myVec = std::vector<int>{1, 2, 3}; fails. Compile the following code to see the error.

Get hands-on with 1200+ tech skills courses.