Skip to main content

Command Palette

Search for a command to run...

Exploring Templates in C++

Updated
2 min read
Exploring Templates in C++

Templates are a powerful feature of C++ that allows for generic programming. They enable the creation of reusable code that can work with different data types without the need for duplication or typecasting. In this article, we will discuss the basics of templates, including function templates and class templates.

Function templates in C++

Function templates are used to create generic functions that can work with different data types. To create a function template, use the 'template' keyword followed by angle brackets (<>) containing the template parameter(s).

template <typename T>
T add(T a, T b) {
    return a + b;
}

You can then call the function with various data types:

int main() {
    int i1 = 3, i2 = 5;
    double d1 = 3.5, d2 = 5.2;

    std::cout << add(i1, i2) << endl; // Calls the 'add' function for integers
    std::cout << add(d1, d2) << endl; // Calls the 'add' function for doubles
}

Class templates

Class templates allow you to create generic classes that can work with different data types. To create a class template, use the 'template' keyword followed by angle brackets (<>) containing the template parameter(s), and then define the class.

template <typename T>
class Pair {
public:
    T first, second;

    Pair(T a, T b) : first(a), second(b) {}
};

To create an instance of a class template, specify the data type within angle brackets (<>) after the class name.

int main() {
    Pair<int> p1(1, 2);          // Creates an instance of 'Pair' for integers
    Pair<double> p2(3.5, 4.2);   // Creates an instance of 'Pair' for doubles
}

Template specialization

Template specialization allows you to define custom behavior for a specific data type. Use the 'template' keyword followed by angle brackets (<>) containing the specific data type.

template <class T>
void fun(T a)
{
   std::cout << "The main template fun(): "
        << a << std::endl;
}

template<>
void fun(int a)
{
    std::cout << "Specialized Template for int type: "
         << a << std::endl;
}

int main()
{
    fun<char>('a');
    fun<int>(10);
    fun<float>(10.14);
}

Conclusion

Templates in C++ provide a powerful tool for generic programming, enabling the creation of reusable functions and classes that can work with various data types. By understanding and utilizing templates, you can write more efficient and maintainable code. In future articles, we will dive deeper into templates, exploring topics such as template specialization, variadic templates, and template metaprogramming.

More from this blog

DarleaNews

32 posts

I want to teach people what I learn over time, learning things myself during the process.