-
Template Functions:
- Function templates allow you to write generic functions that can work with different data types.
- The template parameters are specified using angle brackets (
<>
) when defining and calling the function. - Example:
template <typename T> T add(T a, T b) { return a + b; }
-
Template Classes:
- Class templates allow you to create generic classes that can work with different data types.
- The template parameters are specified using angle brackets (
<>
) when defining and creating instances of the class. - Example:
template <typename T> class Stack { public: void push(const T& item) { container.push_back(item); } // ... private: std::vector<T> container; };
-
Template Parameters:
- Template parameters can be of different types:
- Type parameters (using
typename
orclass
) - Non-type parameters (e.g., integral types, pointers, or references)
- Type parameters (using
- Example:
template <typename T, int N> class FixedArray { public: T data[N]; // ... };
- Template parameters can be of different types:
-
Default Template Arguments:
- You can provide default values for template parameters, similar to function parameters.
- This allows you to create more flexible and reusable templates.
- Example:
template <typename T, typename Allocator = std::allocator<T>> class Vector { public: // ... };
-
Template Specialization:
- Allows you to provide a specific implementation of a template for a particular set of template arguments.
- This can be used to optimize or customize the behavior of a template for specific data types.
- Example:
template <typename T> struct MyStruct { T data; // ... }; template <> struct MyStruct<int> { int data; // Specialized implementation for int };
-
Variadic Templates:
- Introduced in C++11, variadic templates allow you to create functions and class templates that can accept an arbitrary number of type arguments.
- This feature enables the creation of powerful generic programming constructs, such as
std::tuple
andstd::make_unique
. - Example:
template <typename... Args> void print(Args... args) { ((std::cout << args << " "), ...) << std::endl; }
-
Template Aliases (using Declarations):
- Introduced in C++11, template aliases allow you to create a new name for an existing template.
- This can be useful for improving code readability and reducing duplication.
- Example:
template <typename T, std::size_t N> using Array = std::array<T, N>;
-
Trailing Return Type:
- Introduced in C++11, the trailing return type syntax allows you to define the return type of a function template after the parameter list.
- This can be particularly useful for complex return types or when using
auto
as the return type. - Example:
template <typename T, typename U> auto add(T a, U b) -> decltype(a + b) { return a + b; }
-
Fold Expressions:
- Introduced in C++17 (but related to C++11 variadic templates), fold expressions provide a concise way to perform operations on the elements of a parameter pack.
- Fold expressions can be used to implement various generic algorithms and utility functions.
- Example:
template <typename... Args> auto sum(Args... args) { return (... + args); }
-
Template Argument Deduction for Class Templates:
- Introduced in C++17 (but related to C++11 features), this feature allows for automatic deduction of template arguments when creating instances of class templates.
- This can help reduce code duplication and improve readability.
- Example:
template <typename T, typename U> class Pair { public: Pair(T t, U u) : first(t), second(u) {} T first; U second; }; Pair pair = Pair(42, "hello"); // Deduced as Pair<int, const char*>
These are the key C++11 template features that allow you to write flexible, reusable, and powerful C++ code. By understanding and applying these concepts, you can create robust and efficient generic programming solutions.