In C++, templates are a powerful feature that allow you to write generic code that
can work with different types of data. A template is a blueprint for a function or
a class that can work with different types of data, without the need to write 
multiple versions of the same code.

Here's an example of a simple template function that calculates the maximum value 
between two input values:

Copy code
template <typename T>
T max(T a, T b) {
    return (a > b) ? a : b;
}

int main() {
    int a = 3, b = 5;
    double c = 3.14, d = 2.718;
    cout << max(a, b) << endl;
    cout << max(c, d) << endl;
    return 0;
}
In this example, the template <typename T> syntax is used to define a template
function called max(), which takes two arguments of the same type T. The type
T is a placeholder for the actual type that will be used when the function is 
called. In this example, the max() function is called twice, once with two int 
values, and once with two double values. The cout statements will output "5" and "3.
14" respectively.

Here's an example of a simple template class that can store one value of any type:

Copy code
template <typename T>
class Box {
public:
    T value;
    Box(T val) : value(val) {}
};

int main() {
    Box<int> intBox(5);
    Box<double> doubleBox(3.14);
    cout << intBox.value << endl;
    cout << doubleBox.value << endl;
    return 0;
}
In this example, the template <typename T> syntax is used to define a template
class called Box, which has a single member variable value of type T. The class
has a constructor which takes a variable of type T and assigns it to value. In 
this example, the Box class is instantiated twice, once with an int value and once 
with a double value, and the cout statements will output "5" and "3.14"
respectively.

It's important to note that you can use any valid C++ type for the template 
parameter, such as fundamental types, user-defined types, and even other template
types.

Also, you can use template specialization to handle specific types in a different
way and you can use template overloading to use different templates with the same
function name.

You can also use template classes, where you define a class with a template
parameter, and the class can work with multiple types of data. Here is an example
of a simple template class in C++:

Copy code
template <typename T>
class MyVector {
    T* data;
    int size;
public:
    MyVector(int s) : size(s) {
        data = new T[s];
    }
    ~MyVector() {
        delete[] data;
    }
    // Other member functions
};
This class defines a simple dynamic array (vector) that can work with any type
of data. You can create an object of this class with different types, like this:

Copy code
int main() {
    MyVector<int> v1(5);
    MyVector<string> v2(7);
    MyVector<double> v3(10);
    return 0;
}
Please note that C++ templates are a powerful feature, but they can also make
your code more complex, so use them with caution.

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: