Class Templates

Creating Class Templates with Multiple Types

How can I create a class template that works with both primitive types and user-defined types?

Illustration representing computer hardware

Class templates in C++ are incredibly flexible and can work with both primitive types (like int, float, char) and user-defined types (like custom classes or structs) out of the box.

The key is to design your template in a way that doesn't make assumptions about the specific properties of the types it's used with.

Here's an example of a class template that can work with various types:

#include <iostream>
#include <string>

template <typename T>
class Container {
 private:
  T data;

 public:
  Container(T value)
    : data{value} {}

  void Display() const {
    std::cout << "Container holds: "
      << data << '\n';
  }

  T GetValue() const { return data; }

  void SetValue(T newValue) { data = newValue; }
};

struct Player {
  std::string name;
  int score;

  friend std::ostream& operator<<(
    std::ostream& os, const Player& p
  ) {
    return os << p.name << " (Score: "
      << p.score << ")";
  }
};

int main() {
  Container<int> intContainer{42};
  intContainer.Display();

  Container<std::string> stringContainer{
    "Hello, World!"
  };
  stringContainer.Display();

  Container<Player> playerContainer{
    {.name = "Alice", .score = 100}
  };
  playerContainer.Display();
}
Container holds: 42
Container holds: Hello, World!
Container holds: Alice (Score: 100)

In this example, our Container class template works with int, std::string, and our custom Player type. The key points to note are:

  1. We use a single type parameter T which can be any type.
  2. We don't make assumptions about what operations can be performed on T, except that it can be assigned (data = newValue) and streamed to std::cout.
  3. For user-defined types like Player, we define how it should be printed by overloading the << operator.

If you need to perform more specific operations on the contained type, you might need to use concepts (in C++20) or SFINAE techniques to constrain what types can be used with your template. This allows you to create more specialized templates that work with types that have certain properties or methods.

Remember, when working with templates, it's important to provide clear error messages when a type doesn't meet the requirements of your template. This can be achieved through static_assert or, preferably, concepts in modern C++.

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:

  • 125 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