Creating Class Templates with Multiple Types

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

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++.

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?
Best Practices for Naming Template Parameters
What are the best practices for naming template parameters?
Specializing Class Templates
How can I specialize a class template for specific types?
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