Introduction to C++ | Templates

C++ Templates

C++ Templates are a powerful feature that allows you to write generic functions and classes. Templates enable the creation of functions, classes, and structures that can operate on any data type. This makes your code reusable and type-independent.

What are Templates?

A template is a blueprint or formula for creating a generic class or function. It allows you to define the data type of the object or function when it is used.

Syntax of Function Templates

Function Template Syntax


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

Example of Function Template

Here’s an example where a function template is used to add two numbers of any type:

Code Example


        #include <iostream>
        using namespace std;
        
        // Function template to add two numbers of any type
        template 
        T add(T a, T b) {
            return a + b;
        }
        
        int main() {
            cout << "Sum of integers: " << add(5, 10) << endl;
            cout << "Sum of doubles: " << add(3.14, 2.72) << endl;
        
            return 0;
        }
                    

Output

Sum of integers: 15
Sum of doubles: 5.86

Syntax of Class Templates

Class Template Syntax


        template 
        class Box {
        private:
            T value;
        
        public:
            Box(T val) : value(val) {}
        
            T getValue() {
                return value;
            }
        };
                    

Example of Class Template

Here’s an example where a class template is used to create a box that stores any type of value:

Code Example


        #include <iostream>
        using namespace std;
        
        // Class template to store a value of any type
        template 
        class Box {
        private:
            T value;
        
        public:
            Box(T val) : value(val) {}
        
            T getValue() {
                return value;
            }
        };
        
        int main() {
            Box<int> intBox(10);
            Box<double> doubleBox(5.75);
        
            cout << "Integer Value: " << intBox.getValue() << endl;
            cout << "Double Value: " << doubleBox.getValue() << endl;
        
            return 0;
        }
                    

Output

Integer Value: 10
Double Value: 5.75

Key Points About Templates

Template Specialization

Template specialization allows you to provide a specific implementation for a template when it is used with a particular type.

Code Example: Template Specialization


        #include <iostream>
        using namespace std;
        
        // General template
        template 
        T multiply(T a, T b) {
            return a * b;
        }
        
        // Template specialization for type 'string'
        template <>
        string multiply(string a, string b) {
            return a + b; // Concatenate strings instead of multiplying
        }
        
        int main() {
            cout << "Multiplying integers: " << multiply(3, 4) << endl;
            cout << "Multiplying strings: " << multiply(string("Hello "), string("World!")) << endl;
        
            return 0;
        }
                    

Output

Multiplying integers: 12
Multiplying strings: Hello World!

Advantages of Using Templates

Pro Tip:

💡 Pro Tip

Template specialization can be used to customize the behavior of templates for certain data types, allowing you to provide more specific implementations where necessary.