🌟 Join our Telegram group for exclusive updates! Join Now Get Involved

C++ Templates

C++ Templates

C++ Templates

Function Templates:

A function template allows you to define a generic function that can work with various types. Here's a simple example of a function template for swapping two values:

#include <iostream>

// Function template for swapping values
template <typename T>
void swapValues(T &a, T &b) {
    T temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 5, y = 10;
    std::cout << "Before swapping: x = " << x << ", y = " << y << std::endl;

    // Call the function template with int types
    swapValues(x, y);

    std::cout << "After swapping: x = " << x << ", y = " << y << std::endl;

    double a = 3.14, b = 6.28;
    std::cout << "Before swapping: a = " << a << ", b = " << b << std::endl;

    // Call the function template with double types
    swapValues(a, b);

    std::cout << "After swapping: a = " << a << ", b = " << b << std::endl;

    return 0;
}

Class Templates:

Class templates allow you to define generic classes with member functions and member variables that operate on template parameters. Here's an example of a simple class template for a generic pair:

#include <iostream>

// Class template for a generic pair
template <typename T1, typename T2>
class Pair {
public:
    T1 first;
    T2 second;

    // Constructor
    Pair(const T1 &f, const T2 &s) : first(f), second(s) {}

    // Member function to display the pair
    void displayPair() {
        std::cout << "(" << first << ", " << second << ")" << std::endl;
    }
};

int main() {
    // Create an instance of Pair with int and double types
    Pair<int, double> myPair(5, 3.14);

    // Display the pair
    myPair.displayPair();

    return 0;
}

Template Specialization:

Template specialization allows you to provide specific implementations for certain template parameter values. For example, you might want to provide a specialized implementation for a specific data type. Here's a brief example:

#include <iostream>

// Generic template
template <typename T>
void printType() {
    std::cout << "Generic type" << std::endl;
}

// Template specialization for int
template <>
void printType<int>() {
    std::cout << "Type is int" << std::endl;
}

int main() {
    printType<double>();  // Calls the generic template
    printType<int>();     // Calls the specialized template for int

    return 0;
}

In this example, the printType function template has a specialization for the int type.

Templates provide a flexible way to write generic code in C++. They are widely used in the standard library and are an essential part of modern C++ programming. Understanding how to use templates can lead to more reusable and generic code.

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing BYTEFOXD9, you agreed to use cookies in agreement with the BYTEFOXD9's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.