C++ Generics / Templates
A while ago I discovered generics in Java. Today, I’m going to explore how to do the same with C++.
Generics in C++ are known as templates. We use the keyword template
to tell the compiler that we are about to define one:
1
2
template <typename T>
class Hello {};
In the example above, you can also see that typename
is used to define the type. You might also see the keyword class
used interchangeably (There are some scenarios where they are not interchangeable, but I’m not going to cover those in this article):
1
2
template <class T>
class Hello {};