Class Templates

Specializing Class Templates

How can I specialize a class template for specific types?

Illustration representing computer hardware

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.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved