C++ Programing Language
Chapter 9: Templates in C++
Introduction:
9.1 Function Templates:
Function templates allow us to define generic functions that can operate on multiple types. A function template serves as a blueprint for generating specific functions based on the types passed as template arguments. This enables us to write a single function definition that can be used with different data types.
9.1.1 Creating a Function Template:
To create a function template, we use the `template` keyword followed by the template parameter list, which specifies one or more generic types or non-type parameters. The basic syntax for creating a function template is as follows:
Code
```c++
template <typename T>
return_type function_name(T parameter) {
// Function body
// Code to operate on parameter of type T
}
```
For example, let's create a function template that swaps two values of any type:
code
```c++
template <typename T>
void swapValues(T& a, T& b) {
T temp = a;
a = b;
b = temp;
}
```
In this example, `T` is the template parameter representing the generic type. The `swapValues` function can now be used with different types, such as `int`, `double`, or custom types.
9.1.2 Using a Function Template:
To use a function template, we can simply invoke the function with the desired types, and the compiler will generate the appropriate function from the template. The compiler performs template argument deduction to determine the specific types based on the function arguments.
For example, let's use the `swapValues` function template to swap two integers:
code
```c++
int a = 5;
int b = 10;
swapValues(a, b);
```
In this example, the compiler generates a specific version of the `swapValues` function for integers and performs the swap.
9.1.3 Function Template Specialization:
Function template specialization allows us to provide a specialized implementation for a specific type. It allows us to override the generic behavior of a template for specific cases. Specialized versions are selected by the compiler based on the provided types.
For example, let's specialize the `swapValues` function template for strings:
Code
```c++
template <>
void swapValues<std::string>(std::string& a, std::string& b) {
std::string temp = a;
a = "Special " + b;
b = "Special " + temp;
}
```
In this example, we provide a specialized implementation of `swapValues` specifically for `std::string` types. This implementation performs a custom swap for strings.
9.2 Class Templates:
Class templates allow us to define generic classes that can work with multiple data types. Similar to function templates, class templates serve as blueprints for generating specific classes based on template arguments.
9.2.1 Creating a Class Template:
To create a class template, we use the `template` keyword followed by the template parameter list, which specifies one or more generic types or non-type parameters. The basic syntax for creating a class template is as follows:
Code
```c++
template <typename T>
class ClassName {
// Member variables (attributes) and member functions (methods)
// Code to operate on members of type T
};
```
For example, let's create a class template named `Stack` that represents a generic stack data structure:
Code
```c++
template <typename T>
class Stack {
private:
std::vector<T> elements;
public:
void push(const T& element) {
elements.push_back(element);
}
void pop() {
elements.pop_back();
}
T top() const {
return elements.back();
}
bool empty() const {
return elements.empty();
}
};
```
In this example, `T` is the template parameter representing the generic type. The `Stack` class can now be instantiated with different types, such as `int`, `double`, or custom types.
9.2.2 Using a Class Template:
To use a class template, we instantiate the class with the desired types. The compiler generates a specific class from the template based on the provided types.
For example, let's create a stack of integers using the `Stack` class template:
Code
```c++
Stack<int> intStack;
intStack.push(5);
intStack.push(10);
intStack.pop();
int topElement = intStack.top();
```
In this example, the `intStack` object is of type `Stack<int>`, representing a stack of integers. We can push and pop integers and retrieve the top element.
9.2.3 Class Template Specialization:
Similar to function templates, class templates can also be specialized for specific types. Class template specialization allows us to provide a specialized implementation for a specific type.
For example
, let's specialize the `Stack` class template for strings:
Code
```c++
template <>
class Stack<std::string> {
private:
std::vector<std::string> elements;
public:
void push(const std::string& element) {
elements.push_back("Special " + element);
}
void pop() {
elements.pop_back();
}
std::string top() const {
return elements.back();
}
bool empty() const {
return elements.empty();
}
};
```
In this example, we provide a specialized implementation of the `Stack` class specifically for `std::string` types. This specialized version performs custom operations on strings.
Conclusion:
In Chapter 9, we explored the concept of templates in C++. We learned how to create and use function templates and class templates to write generic code that can work with multiple types. We also discussed function template specialization and class template specialization, allowing us to provide specialized implementations for specific types. Templates are a powerful feature in C++ that enable code reuse, flexibility, and maintainability. By understanding and applying templates in your C++ programs, you can write highly generic and reusable code that adapts to different data types and scenarios.
1 Comments
This comment has been removed by the author.
ReplyDelete