Specialization

Similar to function specialization, the explicit specialization of a class template defines a different implementation for a primary template when a specific type is passed as a template parameter. However, it is still a class template and you need to get the real code by instantiation.

For example, let's suppose that we have a struct X template that can store one element of any data type, and it has just one member function named increase(). But for the char type data, we want a different implementation of increase() and need to add a new member function called toUpperCase() to it. Therefore, we decide to declare a class template specialization for that type. We do this as follows:

  1. Declare a primary class template:
template ...

Get Expert C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.