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:
T
which can be any type.T
, except that it can be assigned (data = newValue
) and streamed to std::cout
.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.
Learn how templates can be used to create multiple classes from a single blueprint