Class Template
Class Template
Class Template
Prerequisites
1. What is a Template
A template allows you to write generic, reusable code. Instead of fixing a type, you parameterize it:
1
template<typename T>
A function template defines a generic function that works with different types.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
template<typename T>
T add(T a, T b)
{
return a + b;
}
int main()
{
std::cout << add(2, 3); // int
std::cout << add(2.5, 3.1); // double
}
- Type is deduced automatically
- Simpler to use
- Used for operations / algorithms
✔️ Multiple Types
1
2
3
4
5
template<typename T, typename U>
auto add(T a, U b)
{
return a + b;
}
Limitation
Cannot do partial specialization
2. What is a Class Template
A class template defines a generic class.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
template<typename T>
class Box
{
public:
T value;
Box(T v) : value(v) {}
void print()
{
std::cout << value << "\n";
}
};
int main()
{
Box<int> b1(10);
Box<std::string> b2("hello");
b1.print();
b2.print();
}
- Type must be explicitly specified
- Used for data structures / containers
- Supports partial specialization
Partial Specialization (Class Only)
1
2
3
4
5
6
template<typename T>
class Box<T*>
{
public:
T* value;
};
👉 Applies to pointer types only
| Feature | Function Template | Class Template |
|---|---|---|
| Purpose | Behavior (functions) | Data structure |
| Type Deduction | ✅ Automatic | ❌ Explicit |
| Partial Specialization | ❌ Not allowed | ✅ Allowed |
| Overloading | ✅ Yes | ❌ No |
| Use Case | Algorithms | Containers |
2-1. Performance Perspective
Both are compile-time constructs
Benefits:
- zero runtime overhead
- no virtual dispatch
- aggressive inlining possible
2-2. Modern C++
function template + auto for simple logic
1
2
3
4
auto add(auto a, auto b)
{
return a + b;
}
if constexpr
1
2
3
4
5
6
7
8
template<typename T>
void print(T x)
{
if (std::is_pointer<T>::value)
std::cout << *x;
else
std::cout << x;
}
Both case are compliled.
1
2
3
4
5
6
7
8
template<typename T>
void print(T x)
{
if constexpr (std::is_pointer_v<T>)
std::cout << *x;
else
std::cout << x;
}
If the condition is false, the code is automatically deleted.
concepts (C++20)
Before:
1
2
3
template<typename T>
typename std::enable_if<std::is_integral<T>::value>::type
func(T x) {}
After C++20
1
2
3
4
5
6
7
#include <concepts>
template<std::integral T>
void func(T x)
{
std::cout << x;
}
1
2
3
4
5
6
7
8
9
10
template<typename T>
concept Addable = requires(T a, T b)
{
a + b;
};
template<Addable T>
T add(T a, T b) {
return a + b;
}
Only allow to be possible “+” operation
This post is licensed under CC BY 4.0 by the author.