Specializing Class Templates

How can I specialize a class template for specific types?

Class template specialization allows you to provide a custom implementation of a template for specific types.

This can be useful when you want to optimize for certain types or handle them differently. There are two types of specialization: full specialization and partial specialization.

Full Specialization

Full specialization is used when you want to provide a completely different implementation for a specific type. Here's an example:

#include <iostream>
#include <string>

// Primary template
template <typename T>
class Container {
 public:
  void Store(T value) {
    std::cout << "Storing generic value: "
      << value << '\n';
  }
};

// Full specialization for int
template <>
class Container<int> {
 public:
  void Store(int value) {
    std::cout << "Storing int value: "
      << value << '\n';
  }
};

// Full specialization for std::string
template <>
class Container<std::string> {
 public:
  void Store(const std::string& value) {
    std::cout << "Storing string value: "
      << value << '\n';
  }
};

int main() {
  Container<double> doubleContainer;
  Container<int> intContainer;
  Container<std::string> stringContainer;

  doubleContainer.Store(3.14);
  intContainer.Store(42);
  stringContainer.Store("Hello, World!");
}
Storing generic value: 3.14
Storing int value: 42
Storing string value: Hello, World!

In this example, we have a primary template and two full specializations for int and std::string.

Partial Specialization

Partial specialization is used when you want to specialize a template for a subset of possible types. It's particularly useful for templates with multiple parameters. Here's an example:

#include <iostream>
#include <vector>
#include <list>

// Primary template
template <typename T, typename Container>
class DataManager {
 public:
  void Process() {
    std::cout << "Processing generic data\n";
  }
};

// Partial specialization for vector containers
template <typename T>
class DataManager<T, std::vector<T>> {
 public:
  void Process() {
    std::cout << "Processing vector data\n";
  }
};

// Partial specialization for pointers
template <typename T>
class DataManager<T*, std::vector<T*>> {
 public:
  void Process() {
    std::cout << "Processing vector of pointers\n";
  }
};

int main() {
  DataManager<int, std::vector<int>>
    vectorManager;
  DataManager<int*, std::vector<int*>>
    pointerVectorManager;
  DataManager<double, std::list<double>>
    listManager;

  vectorManager.Process();
  pointerVectorManager.Process();
  listManager.Process();
}
Processing vector data
Processing vector of pointers
Processing generic data

In this example, we have a primary template and two partial specializations: one for vectors and another for vectors of pointers.

Specialization allows you to write more efficient or appropriate code for specific types while maintaining a general implementation for others. It's a powerful feature of C++ templates that enables you to optimize performance and provide type-specific behavior when needed.

Class Templates

Learn how templates can be used to create multiple classes from a single blueprint

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Partial Specialization of Class Templates
Is it possible to partially specialize a class template?
Compile-Time Polymorphism with Templates
Can I use templates to implement compile-time polymorphism?
Creating Templates with Variable Number of Parameters
How can I create a template that works with an arbitrary number of type parameters?
Default Values for Non-Type Template Parameters
Is it possible to have default values for non-type template parameters?
Constraining Template Arguments
How can I create a template that only accepts certain types of arguments?
Typename vs Class in Template Declarations
What's the difference between typename and class in template parameter declarations?
Creating Class Templates with Multiple Types
How can I create a class template that works with both primitive types and user-defined types?
Best Practices for Naming Template Parameters
What are the best practices for naming template parameters?
Using Template Parameters in Constructors
Can I use template parameters in the constructor of a class template?
Template Methods in Non-Template Classes
How do I create a template method within a non-template class?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant