Templates in C++

Templates are a powerful feature in C++ that enable generic programming, allowing for the creation of type-independent functions and classes. In this article, we will explore the concepts of templates in detail, with examples in C++.

Table of Contents

  1. Introduction to Templates
  2. Function Templates
  3. Class Templates
  4. Template Specialization
  5. Conclusion

1. Introduction to Templates

Templates allow the creation of generic functions and classes that can operate on different data types without the need for explicit type specification. They help reduce code duplication and improve code reusability.

2. Function Templates

Function templates are functions that can work with any data type, allowing for a single implementation to handle multiple types.

2.1. Creating a Function Template

To create a function template, use the template keyword followed by the template parameter(s) enclosed in angle brackets (<>).

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

2.2. Using a Function Template

The compiler automatically deduces the template argument types when a function template is called.

int main() {
int a = 5;
int b = 10;
cout << "Max of a and b: " << maximum(a, b) << endl; // Output: Max of a and b: 10

double c = 3.5;
double d = 2.1;
cout << "Max of c and d: " << maximum(c, d) << endl; // Output: Max of c and d: 3.5

return 0;
}

3. Class Templates

Class templates are classes that can work with any data type, allowing for a single implementation to handle multiple types.

3.1. Creating a Class Template

To create a class template, use the template keyword followed by the template parameter(s) enclosed in angle brackets (<>).

template <typename T>
class Box {
public:
Box(T value) : value_(value) {}

T getValue() const {
return value_;
}

private:
T value_;
};

3.2. Using a Class Template

When instantiating a class template, specify the template argument type(s) explicitly in angle brackets (<>).

int main() {
Box<int> intBox(10);
cout << "Value in intBox: " << intBox.getValue() << endl; // Output: Value in intBox: 10

Box<double> doubleBox(3.14);
cout << "Value in doubleBox: " << doubleBox.getValue() << endl; // Output: Value in doubleBox: 3.14

return 0;
}

4. Template Specialization

Template specialization allows for the customization of the behavior of function or class templates for specific data types.

4.1. Function Template Specialization

To create a specialization for a function template, use the template <> keyword followed by the function declaration, with the specialized type in place of the template parameter.

template <>
const char* maximum<const char*>(const char* a, const char* b) {return (strcmp(a, b) > 0) ? a : b;}

int main() {
const char* s1 = "Hello";
const char* s2 = "World";
cout << "Max of s1 and s2: " << maximum(s1, s2) << endl; // Output: Max of s1 and s2: World
return 0;
}

4.2. Class Template Specialization

To create a specialization for a class template, use the template <> keyword followed by the class declaration, with the specialized type in place of the template parameter.

template <typename T>
class Array {
public:
Array(size_t size) : size_(size), data_(new T[size_]) {}

~Array() {
delete[] data_;
}

T& operator[](size_t index) {
return data_[index];
}

size_t getSize() const {
return size_;
}

private:
size_t size_;
T* data_;
};

// Specialization for bool type
template <>
class Array<bool> {
public:
Array(size_t size) : size_(size), data_(new uint8_t[(size_ + 7) / 8]()) {}

~Array() {
delete[] data_;
}

bool operator[](size_t index) const {
return (data_[index / 8] & (1 << (index % 8))) != 0;
}

size_t getSize() const {
return size_;
}

private:
size_t size_;
uint8_t* data_;
};

int main() {
Array<bool> boolArray(10);
boolArray[5] = true;
cout << "Value at index 5: " << boolArray[5] << endl; // Output: Value at index 5: 1

return 0;
}

5. Conclusion

In this article, we’ve covered the fundamentals of templates in C++. We have explored function templates, class templates, and template specialization. Understanding these concepts is crucial for writing efficient, reusable, and type-independent code in C++.

Templates enable generic programming, allowing you to create functions and classes that can work with multiple data types, reducing code duplication and improving code reusability. Template specialization allows you to customize the behavior of function or class templates for specific data types, offering more control and flexibility in your code.