Defination of class template

In C++, a class template is a blueprint for creating generic classes. It allows you to define a class without specifying the exact data types it will work with. Instead, you use placeholders, typically represented by type parameters, to represent the data types. When you create an instance of the class using the template, you can specify the actual data types you want to use, and the compiler will generate a specific version of the class for those data types.

The syntax for declaring a class template in C++ is as follows:

template <typename T>
class ClassName {
    // Class members and methods using the type parameter T
};

Here’s a breakdown of the key components:

  1. template <typename T>: This is the template declaration that introduces the type parameter T. You can also use the keyword class instead of typename. The T here is just a placeholder for the actual data type.
  2. class ClassName: This is the name of the class you are defining. You can replace ClassName with any valid C++ identifier.
  3. T: The type parameter T can be used throughout the class definition to represent the unspecified data type.
  4. Class Members and Methods: Within the class, you can use T as if it were a regular data type. You can declare member variables, member functions, constructors, and other elements using T to create a generic class that will work with different data types.

Here’s an example of a class template in C++:

#include <iostream>

// Class template for a generic container
template <typename T>
class Container {
private:
    T data;
public:
    Container(T value) : data(value) {}
    T getData() const { return data; }
};

int main() {
    // Creating objects of the class template with different data types
    Container<int> intContainer(42);
    Container<double> doubleContainer(3.14);
    Container<std::string> stringContainer("Hello, world!");

    // Accessing data stored in the containers
    std::cout << "Integer container: " << intContainer.getData() << std::endl;
    std::cout << "Double container: " << doubleContainer.getData() << std::endl;
    std::cout << "String container: " << stringContainer.getData() << std::endl;

    return 0;
}

In this example, we define a class template Container that can store a generic type T. The class has a private member data of type T, and a constructor that takes a value of type T as a parameter to initialize the data member. Additionally, there’s a member function getData() that returns the stored data.

In the main() function, we create three instances of the Container class with different data types (int, double, and std::string) and demonstrate how to access the data stored in each container. The class template allows us to create multiple containers, each holding a different data type, using the same template code.

Class Template and Non-Type Template argument

In C++, a non-type template argument is a constant expression that can be used to parameterize a template. Unlike type template arguments, which represent types, non-type template arguments can be used to pass values, such as integers, floating-point numbers, pointers, or references, as template parameters.

Non-type template arguments have some limitations and requirements:

  1. They must be constant expressions: Non-type template arguments must be computable at compile-time and should be constants or expressions that can be evaluated during compilation. This ensures that the template can be instantiated with known values.
  2. They can be integral or enumeration types: Non-type template arguments can be of integral types (e.g., int, unsigned int, char) or enumeration types.
  3. They can be pointers or references: Non-type template arguments can also be pointers or references, but these must refer to external variables with static storage duration.

Here’s an example of using a non-type template argument in C++:

#include <iostream>

// Example of a template class with a non-type template argument
template <int N>
class Array {
public:
    int elements[N];

    Array() {
        for (int i = 0; i < N; ++i) {
            elements[i] = i;
        }
    }

    void print() {
        for (int i = 0; i < N; ++i) {
            std::cout << elements[i] << " ";
        }
        std::cout << std::endl;
    }
};

int main() {
    // Instantiate an Array template with N = 5
    Array<5> arr;
    arr.print(); // Output: 0 1 2 3 4

    // Instantiate another Array template with N = 10
    Array<10> arr2;
    arr2.print(); // Output: 0 1 2 3 4 5 6 7 8 9

    return 0;
}

In this example, the Array template class takes an integer non-type template argument N, which represents the size of the array. During compilation, the template will be instantiated with the specified values of N, and the resulting classes will have arrays of different sizes.


more related content on Object Oriented Programming

JOIN OUR NEWSLETTER
And get notified everytime we publish a new blog post.