内容简介:I know, it’s been a while since the last time I published something newbies-friendly on my blog. The main reason is that most of my readers are either experienced devs or from C background having modest C++ encounters. But while programming in C++ you need
I know, it’s been a while since the last time I published something newbies-friendly on my blog. The main reason is that most of my readers are either experienced devs or from C background having modest C++ encounters. But while programming in C++ you need a completely different mindset as both C and C++ belong to different programming paradigm. I always strive to show them a better way of doing things in C++. Anyway, I found the topic which is lengthy, reasonably complex (at least it was for me), newbies-friendly as well as energizing for experienced folks (if Modern C++ jargon, rules, and features added) i.e. C++ Template.
I will start with a simple class/function template and as we move along, it will increase the complexity. And also cover the advanced topics like the variadic template , nested template, CRTP, template vs fold-expression, etc. But, yes! we would not take deeper dive otherwise this would become a book rather than an article.
Note: I would recommend you to use cppinsights online tool wherever you feel confused. It helps you to see Template Instances, Template Argument Deduction, etc. It helps you to see code from the compiler's perspective.
Terminology/Jargon/Idiom You May Face
-
Template Instantiation
: It is a process of generating a concrete class/struct/union/function out of templated class/struct/union/function for a particular combination of template arguments. For example, if you use
vector<int>
andvector<char>
, it will create two different concrete classes during compilation. This process of creating concrete classes is known as Template Instantiation. - Template Instances : Outcome of Template Instantiation is Template Instances i.e. concrete classes.
- Explicit Template Instantiation : Usually template instantiation done at the time of object declaration. But you can also force the compiler to instantiate class/struct/union/function with a particular type without even creating the object. It may appear in the program anywhere after the template definition, and for a given argument-list. I will see this later in the article.
-
Template Argument vs Template Parameter
: In expression
template<typename T> void print(T a){ };
,T
is the parameter and when you callprint(5);
,5
which is of typeint
is a template argument. This is a trivial thing for some pips. But not for non-native English speakers or beginners. So, this ambiguity has to be clear.
C++ Template Types
Class Template
Java
x
1
template <typename T1, typename T2>
2
class pair {
3
public:
4
T1 first;
5
T2 second;
6
};
7
8
pair<int, char> p1;
9
pair<float, float> p2;
-
The basic idea of a class template is that the template parameter i.e.
T1
andT2
gets substituted by an appropriate deduced type at compile time. The result is that the same class can be reused for multiple types. - And the user has to specify which type they want to use when an object of the class is declared.
Function Template
Java
xxxxxxxxxx
1
1
template <typename T>
2
T min(T a, T b) {
3
return a < b ? a : b;
4
}
5
6
min<int>(4, 5); // Case 1
7
min<float>(4.1f, 5.1f); // Case 2
-
In both of the above case, the template arguments used to replace the types of the parameters i.e.
T
. -
One additional property of template functions (unlike class template till C++17) is that the compiler can infer the template parameters based on the parameters passed to the function. So, passing
<int>
and<float>
after the function name is redundant.
Union Template
-
Yes! a union can also be templatized. The standard library provides some utilities like
std::optional
,std::variant
, etc. which directly or indirectly uses templatized union.
Java
xxxxxxxxxx
1
1
template <typename T>
2
union test {
3
uint8_t ch[sizeof(T)];
4
T variable;
5
};
- As you can see above, templatized unions are also particularly useful to represent a type simultaneously as a byte array .
Variable Template
- Yes! This may a bit socking. But, you can templatize the variable also since C++14 .
Java
xxxxxxxxxx
1
1
template <class T>
2
constexpr T pi = T(3.1415926535897932385L); // variable template
3
4
cout << pi<float> << endl; // 3.14159
5
cout << pi<int> << endl; // 3
- Now, you might be wondering that what is the point of the templatizing variable. But, consider the following example:
Java
xxxxxxxxxx
1
10
1
template <uint32_t val>
2
constexpr auto fib = fib<val - 1> + fib<val - 2>;
3
4
template <>
5
constexpr auto fib<0> = 0;
6
7
template <>
8
constexpr auto fib<1> = 1;
9
10
cout << fib<10> << endl; // 55
- The above code gives you 10th Fibonacci term at compile-time, without even creating class or function.
C++ Template Argument
Overriding Template Argument Deduction
Java
xxxxxxxxxx
1
1
template <typename T>
2
T min(T a, T b) {
3
cout << typeid(T).name() << endl; // T will be deduce as `int`
4
return a < b ? a : b;
5
}
6
7
min<int>(5.5f, 6.6f); // Implicit conversion happens here
Default Template Arguments
Java
xxxxxxxxxx
1
1
template <class T, size_t N = 10>
2
struct array {
3
T arr[N];
4
};
5
6
array<int> arr;
- Just like in the case of the function arguments, template parameters can also have their default values.
- All template parameters with a default value have to be declared at the end of the template parameter list.
Template Argument Deduction
Function Template Argument Deduction
- Function template argument deduction is done by comparing the types of function arguments to function parameters, according to rules in the Standard . Which makes function templates far more usable than they would otherwise be. For example, given a function template like:
Java
xxxxxxxxxx
1
1
template <typename RanIt>
2
void sort(RanIt first, RanIt last){
3
// . . .
4
}
-
You can and should sort a
std::vector<int>
without explicitly specifying thatRanIt
isstd::vector<int>::iterator
. When the compiler seessort(v.begin(), v.end());
, it knows what the types ofv.begin()
andv.end()
are, so it can determine whatRanIt
should be.
Class Template Argument Deduction(CTAD)
- Until C++17, template classes could not apply type deduction in their initialization as template function do. For example
Java
xxxxxxxxxx
1
1
//...
2
pair p4{1, 'A'}; // Not OK until C++17: Can't deduce type in initialization
3
//...
- But from C++17, the compiler can deduce types in class/struct initialization and this to work, class/struct must have an appropriate constructor. But this limitation is also relaxed in C++20. So technically from C++20, you can construct the object with aggregate initialization and without specifying types explicitly .
-
Until C++17, the standard provided some
std::make_
utility functions to counter such situations as below.
Inferring Template Argument Through Function Template
-
You might have seen many functions like
std::make_pair()
,std::make_unique()
,std::make_share()
, etc. Which can typically and unsophistically implement as:
Java
xxxxxxxxxx
1
1
template <typename T1, typename T2>
2
pair<T1, T2> make_pair(T1andand t1, T2andand t2) {
3
return {forward<T1>(t1), forward<T2>(t2)};
4
}
- But have you ever wonder why these helper functions are there in the standard library? How does this even help?
Java
xxxxxxxxxx
1
1
pair<int, char> p1{1, 'A'}; // Rather using this
2
3
auto p2 = make_pair(1, 2); // Use this instead
4
auto p3 = make_pair<float>(1, 2.4f); // Or specify types explicitly
make_pair std::vector v{1,2,3,4};
Template Argument Forwarding
C++ Template Reference Collapsing Rules
-
Apart from accepting type and value in the template parameter. You can enable the template to accept both lvalue and rvalue references
. And to do this you need to adhere to the rules of reference collapsing as follows:
-
Tand and
becomesTand
-
Tand andand
becomeTand
-
Tandand and
becomesTand
-
Tandand andand
becomesTandand
-
template <typename T> void f(T andandt);
- In the above case, the real type depends on the context. For example:
Java
xxxxxxxxxx
1
1
int x = 0;
2
3
f(0); // deduces as rvalue reference i.e. f(intandand)
4
f(x); // deduces as lvalue reference i.e. f(intand)
-
In the case of
f(0);
,0
is rvalue of typeint
, henceT = intandand
, thusf(intandand andandt)
becomesf(intandand t)
. -
In the case of
f(x);
,x
is lvalue of typeint
, henceT = intand
, thusf(intand andandt)
becomesf(intand t)
.
Perfect Forwarding | Forwarding Reference | Universal Reference
-
To perfectly forward
t
to another function, one must usestd::forward
as:
Java
xxxxxxxxxx
1
1
template <typename T>
2
void func1(T andandt) {
3
func2(std::forward<T>(t)); // Forward appropriate lvalue or rvalue reference to another function
4
}
- Forwarding references can also be used with variadic templates:
Java
xxxxxxxxxx
1
1
template <typename... Args>
2
void func1(Argsandand... args) {
3
func2(std::forward<Args>(args)...);
4
}
Why Do We Need Forwarding Reference in First Place?
- Answer to this question lies in move semantics . Though, the short answer to this question is "To perform copy/move depending upon value category type".
C++ Template Category
Full Template Specialization
- Template has a facility to define implementation for specific instantiations of a template class/struct/union/function/method.
Function Template Specialization
Java
xxxxxxxxxx
1
1
template <typename T>
2
T sqrt(T t) { /* Some generic implementation */ }
3
4
template<>
5
int sqrt<int>(int i) { /* Highly optimized integer implementation */ }
-
In the above case, a user that writes
sqrt(4.0)
will get the generic implementation whereassqrt(4)
will get the specialized implementation.
Class Template Specialization
Java
xxxxxxxxxx
1
15
1
template <typename T> // Common case
2
struct Vector {
3
void print() {}
4
};
5
6
template <> // Special case
7
struct Vector<bool> {
8
void print_bool() {}
9
};
10
11
Vector<int> v1;
12
v1.print_bool(); // Not OK: Chose common case Vector<T>
13
v1.print() // OK
14
15
Vector<bool> v2; // OK : Chose special case Vector<bool>
Partial Template Specialization
Partial Class Template Specialization
- In contrast to a full template specialization, you can also specialize template partially with some of the arguments of the existing template fixed. Partial template specialization is only available for template class/structs/union:
Java
xxxxxxxxxx
1
25
1
template <typename T1, typename T2> // Common case
2
struct Pair {
3
T1 first;
4
T2 second;
5
6
void print_first() {}
7
};
8
9
template <typename T> // Partial specialization on first argument as int
10
struct Pair<int, T> {
11
void print() {}
12
};
13
14
// Use case 1 ----------------------------------------------------------
15
Pair<char, float> p1; // Chose common case
16
p1.print_first(); // OK
17
// p1.print(); // Not OK: p1 is common case and it doesn't have print() method
18
19
// Use case 2 ----------------------------------------------------------
20
Pair<int, float> p2; // Chose special case
21
p2.print(); // OK
22
// p2.print_first(); // Not OK: p2 is special case and it does not have print_first()
23
24
// Use case 3 ----------------------------------------------------------
25
// Pair<int> p3; // Not OK: Number of argument should be same as Primary template
Partial Function Template Specialization
- You cannot partially specialize in method/function . Function templates may only be fully specialized
Java
xxxxxxxxxx
1
19
1
template <typename T, typename U>
2
void foo(T t, U u) {
3
cout << "Common case" << endl;
4
}
5
6
// OK.
7
template <>
8
void foo<int, int>(int a1, int a2) {
9
cout << "Fully specialized case" << endl;
10
}
11
12
// Compilation error: partial function specialization is not allowed.
13
template <typename U>
14
void foo<string, U>(string t, U u) {
15
cout << "Partial specialized case" << endl;
16
}
17
18
foo(1, 2.1); // Common case
19
foo(1, 2); // Fully specialized case
Alternative To Partial Function Template Specialization
-
As I have mentioned earlier, partial specialization of function templates is not allowed. You can use SFINAE
with
std::enable_if
for workaround as follows:
Java
xxxxxxxxxx
1
14
1
template <typename T, typename std::enable_if_t<!std::is_pointer<T>::value> * = nullptr>
2
void func(T val) {
3
cout << "Value" << endl;
4
}
5
6
template <typename T, typename std::enable_if_t<std::is_pointer<T>::value> * = nullptr>
7
void func(T val) { // NOTE: function signature is NOT-MODIFIED
8
cout << "Pointer" << endl;
9
}
10
11
int a = 0;
12
func(a);
13
func(anda);
14
Non-Type Template Parameter
- As the name suggests, apart from types, you can also declare the template parameter as constant expressions like addresses, references , integrals, std::nullptr_t , enums, etc.
- Like all other template parameters, non-type template parameters can be explicitly specified, defaulted, or derived implicitly via Template Argument Deduction.
-
The
more specific use case of a non-type template is passing a plain array into a function without specifying its size explicitly
. A more relevant example of this is
std::begin
andstd::end
the specialization for array literal from the standard library:
Java
xxxxxxxxxx
1
1
template < class T,
2
size_t size> // Non Type Template
3
T* begin(T (andarr)[size]) { // Array size deduced implicitly
4
return arr;
5
}
6
7
int arr[] = {1,2,3,4};
8
begin(arr); // Do not have to pass size explicitly
- Non-type template parameters are one of the ways to achieve template recurrence and enable Template Meta-programming .
Nested Template: Template Template Parameter
- Sometimes we have to pass templated type into another templated type. And in such a case, you not only have to take care of the main template type but also a nested template type. Very simple template- template parameter examples is:
Java
xxxxxxxxxx
1
15
1
template<
2
template <typename> class C,
3
typename T
4
5
void print_container(C<T> andc) {
6
// . . .
7
}
8
9
template <typename T>
10
class My_Type {
11
// . . .
12
};
13
14
My_Type<int> t;
15
print_container(t);
Variadic Template
- It is often useful to define class/struct/union/function that accepts a variable number and type of arguments.
-
If you have already used C you'll know that
printf
function can accept any number of arguments. Such functions are entirely implemented through macros or ellipses operators . And because of that it has several disadvantages like type-safety , cannot accept references as arguments, etc.
Variadic Class Template
Implementing Unsophisticated Tuple Class(>=C++14)
-
Since C++11 standard library introduced
std::tuple
class that accepts variable data members at compile time using the variadic template. And to understand its working, we will build our own ADT same asstd::tuple
- The variadic template usually starts with the general (empty) definition, that also serves as the base-case for recursion termination in the later specialization:
Java
xxxxxxxxxx
1
1
template <typename... T>
2
struct Tuple { };
-
This already allows us to define an empty structure i.e.
Tuple<> object;
, albeit that isn't very useful yet. Next comes the recursive case specialization:
Java
xxxxxxxxxx
1
16
1
template<
2
typename T,
3
typename... Rest
4
5
struct Tuple<T, Rest...> {
6
T first;
7
Tuple<Rest...> rest;
8
9
Tuple(const Tand f, const Restand ... r)
10
: first(f)
11
, rest(r...) {
12
}
13
};
14
15
Tuple<bool> t1(false); // Case 1
16
Tuple<int, char, string> t2(1, 'a', "ABC"); // Case 2
How Does Variadic Class Template Works?
To understand variadic class template, consider use case 2 above i.e. Tuple<int, char, string> t2(1, 'a', "ABC");
-
The declaration first matches against the specialization, yielding a structure with
int first;
andTuple<char, string> rest;
data members. -
The rest definition again matches with specialization, yielding a structure with
char first;
andTuple<string> rest;
data members. -
The rest definition again matches this specialization, creating its own
string first;
andTuple<> rest;
members. - Finally, this last rest matches against the base-case definition, producing an empty structure.
You can visualize this as follows:
Java
xxxxxxxxxx
1
1
Tuple<int, char, string>
2
-> int first
3
-> Tuple<char, string> rest
4
-> char first
5
-> Tuple<string> rest
6
-> string first
7
-> Tuple<> rest
8
-> (empty)
I have written a separate article on Variadic Template C++: Implementing Unsophisticated Tuple , if you are interested more in the variadic temple.
Variadic Function Template
- As we have seen earlier, the variadic template starts with an empty definition i.e. base case for recursion.
void print() {}
- Then the recursive case specialization:
Java
xxxxxxxxxx
1
1
template<
2
typename First,
3
typename... Rest // Template parameter pack
4
5
void print(First first, Rest... rest) { // Function parameter pack
6
cout << first << endl;
7
print(rest...); // Parameter pack expansion
8
}
- This is now sufficient for us to use the print function with a variable number and type of arguments. For example:
print(500, 'a', "ABC");
-
You can further optimize the print function with forwarding reference,
if constexpr()
andsizeof()
operator as:
Java
xxxxxxxxxx
1
13
1
template<
2
typename First,
3
typename... Rest
4
5
void print(Firstandand first, Restandand... rest) {
6
if constexpr(sizeof...(rest) > 0) { // Size of parameter pack
7
cout << first << endl;
8
print(std::forward<Rest>(rest)...); // Forwarding reference
9
}
10
else {
11
cout << first << endl;
12
}
13
}
How Does Variadic Function Template Works?
-
As you can see we have called print with 3 arguments i.e.
print(500, 'a', "ABC");
-
At the time of compilation compiler instantiate 3 different print function as follows:
void print(int first, char __rest1, const char* __rest2) void print(char first, const char* __rest1) void print(const char* first)
-
The first print(i.e. accept 3 arguments) will be called which prints the first argument and line
print(rest…);
expand with second print(i.e. accept 2 arguments). This will go on till argument count reaches to zero. - That means in each call to print, the number of arguments is reduced by one and the rest of the arguments will be handled by a subsequent instance of print.
- Thus, the number of print instance after compilation is equal to the number of arguments, plus the base case instance of print. Hence, the variadic template also contributes to more code bloating .
- You can get this much better if you put the above example in cppinsights . And try to understand all the template instances.
Fold Expressions vs Variadic Template
- As we saw, from C++11, the variadic template is a great addition to C++ Template. But it has nuisance like you need a base case and recursive template implementation, etc.
- So, with C++17 standard introduced a new feature named as Fold Expression . Which you can use with parameter pack as follows:
Java
xxxxxxxxxx
1
1
template <typename... Args>
2
void print(Args andand... args) {
3
(void(cout << std::forward<Args>(args) << endl), ...);
4
}
- See, no cryptic boilerplate required. Isn't this solution looks neater?
- There are total of 3 types of folding: Unary fold, Binary fold, and Fold over a comma. Here we have done left folding over a comma. You can read more about Fold Expression here .
Misc
C++ Template 'typename' vs 'class'
-
typename
andclass
are interchangeable in most of the cases. -
A general convention is
typename
used with the concrete type(i.e. in turn, does not depend on further template parameter) whileclass
used with the dependent type. -
But there are cases where either
typename
orclass
has to be certain. For example
To Refer to Dependent Types
Java
xxxxxxxxxx
1
1
template<typename container>
2
class Example {
3
using t1 = typename container::value_type; // value_type depends on template argument of container
4
using t2 = std::vector<int>::value_type; // value_type is concrete type, so doesn't require typename
5
};
-
typename
is a must while referencing a nested type that depends on the template parameter.
To Specify Template Template Type
Java
xxxxxxxxxx
1
12
1
template<
2
template <typename, typename> class C, // `class` is must prior to C++17
3
typename T,
4
typename Allocator
5
6
void print_container(C<T, Allocator> container) {
7
for (const Tand v : container)
8
cout << v << endl;
9
}
10
11
vector<int> v;
12
print_container(v);
-
This is rectified in C++17, So now you can use
typename
also.
C++11: Template Type Alias
Java
xxxxxxxxxx
1
10
1
template<typename T>
2
using pointer = T*;
3
4
pointer<int> p = new int; // Equivalent to: int* p = new int;
5
6
7
template <typename T>
8
using v = vector<T>;
9
10
v<int> dynamic_arr; // Equivalent to: vector<int> dynamic_arr;
-
typedef
will also work fine, but would not encourage you to use it. As it isn't part of Modern C++ .
C++14/17: Template and Auto Keyword
-
Since C++14, you can use
auto
in function argument . It's kind of template shorthand as follows:
Java
xxxxxxxxxx
1
1
void print(auto andc) { /*. . .*/ }
2
3
// Equivalent to
4
5
template <typename T>
6
void print(T andc) { /*. . .*/ }
-
Although
auto
in function, return-type is supported from C++11 . But, you have to mention the trailing return type. Which is rectified in C++14 and now return type is automatically deduced by compiler. -
From C++17, you can also use
auto
in a non-type template (I will cover this in later part this article) parameters.
C++20: Template Lambda Expression
-
A generic lambda expression
is supported since C++14 which declare parameters as
auto
. But there was no way to change this template parameter and use real template arguments. For example:
Java
xxxxxxxxxx
1
1
template <typename T>
2
void f(std::vector<T>and vec) {
3
//. . .
4
}
-
How do you write the lambda for the above function which takes
std::vector
of typeT
? This was the limitation till C++17, but with C++20 it is possible templatized lambda as :
Java
xxxxxxxxxx
1
1
auto f = []<typename T>(std::vector<T>and vec) {
2
// . . .
3
};
4
5
std::vector<int> v;
6
f(v);
Explicit Template Instantiation
- An explicit instantiation creates and declares a concrete class/struct/union/function/variable from a template, without using it just yet.
- Generally, you have to implement the template in header files only. You can not put the implementation/definition of template methods in implementation files(i.e. CPP or .cc). If this seems new to you, then consider the following minimalist example:
value.hpp
Java
xxxxxxxxxx
1
1
#pragma once
2
3
template <typename T>
4
class value {
5
T val;
6
public:
7
T get_value();
8
};
value.cpp
Java
xxxxxxxxxx
1
1
#include "value.hpp"
2
3
template <typename T>
4
T value<T>::get_value() {
5
return val;
6
}
main.cpp
Java
xxxxxxxxxx
1
1
#include "value.hpp"
2
3
int main() {
4
value<int> v1{9};
5
cout << v1.get_value() << endl;
6
return 0;
7
}
- If you compile the above code you will get the following error:
Java
xxxxxxxxxx
1
1
/tmp/main-4b4bef.o: In function `main':
2
main.cpp:(.text+0x1e): undefined reference to `value<int>::get_value()'
3
clang: error: linker command failed with exit code 1 (use -v to see invocation)
4
compiler exit status 1
-
If you do explicit initialization i.e. add
template class value<int>;
line at the end ofvalue.cpp
. Then the compilation gets successful. -
The "template class" command causes the compiler to explicitly instantiate the template class. In the above case, the compiler will stencil out
value<int>
inside ofvalue.cpp
. - There are other solutions as well. Check out this StackOverflow link .
C++ Template Example Use Cases
Curiously Recurring Template Pattern
- CRTP widely employed for static polymorphism or code reusability without bearing the cost of a virtual dispatch mechanism . Consider the following code:
Java
xxxxxxxxxx
1
25
1
template <typename specific_animal>
2
struct animal {
3
void who() { implementation().who(); }
4
5
private:
6
specific_animal andimplementation() { return *static_cast<specific_animal *>(this); }
7
};
8
9
struct dog : animal<dog> {
10
void who() { cout << "dog" << endl; }
11
};
12
13
struct cat : animal<cat> {
14
void who() { cout << "cat" << endl; }
15
};
16
17
18
template <typename specific_animal>
19
void who_am_i(animal<specific_animal> *animal) {
20
animal->who();
21
}
22
23
24
who_am_i(new dog); // Prints `dog`
25
who_am_i(new cat); // Prints `cat`
- We have not used a virtual keyword and still achieved the functionality of polymorphism(more-specifically static polymorphism).
- I have written a separate article covering practical examples of Curiously Recurring Template Pattern(CRTP)[TODO].
Passing `std` Container as C++ Template Argument
- If you wanted to accept anything and figure it out later, you could write:
Java
xxxxxxxxxx
1
1
template <typename C>
2
void print_container(const C andcontainer) {
3
for (const auto andv : container)
4
cout << v << endl;
5
}
-
This naive way may fail if you pass anything other than standard container as other types may not have
begin
andend
iterator.
Passing std::vector to C++ Template Function
Naive Way to Capture the Container's Value Type
- But let say, you want to pass container and want to work with the container's storage type also. You can do:
Java
xxxxxxxxxx
1
1
template<
2
typename C,
3
typename T = typename C::value_type
4
5
void print_container(const C andcontainer) {
6
for (const T andv : container)
7
cout << v << endl;
8
}
- We can provide the second type parameter to our function that uses SFINAE to verify that the thing is a container.
-
All standard containers have a member type named
value_type
which is the type of the thing inside the container. We sniff for that type, and if no such type exists, then SFINAE kicks in, and that overload is removed from consideration.
Capturing Container's Value Type Explicitly
value_type std::vector
Java
xxxxxxxxxx
1
1
template<
2
class T,
3
class Allocator = std::allocator<T>
4
5
class vector;
-
And you can capture two template arguments of
std::vector
container explicitly as:
Java
xxxxxxxxxx
1
1
template<
2
template <typename, typename> class C,
3
typename T,
4
typename Allocator
5
6
void print_container(C<T, Allocator> container) {
7
for (const Tand v : container)
8
cout << v << endl;
9
}
- The above template pattern would be the same if you want pass container to class/struct/union.
Passing Any Container to C++ Template Function
- You see if you pass any other containers to the above solution. It won't work. So to make it generic we can use the variadic template :
Java
xxxxxxxxxx
1
14
1
template<
2
template <typename...> class C,
3
typename... Args
4
5
void print_container(C<Args...> container) {
6
for (const auto andv : container)
7
cout << v << endl;
8
}
9
10
vector<int> v{1, 2, 3, 4}; // takes total 2 template type argument
11
print_container(v);
12
13
set<int> s{1, 2, 3, 4}; // takes total 3 template type argument
14
print_container(s);
Passing Container-of-Container/2D-std::vector as C++ Template Argument
- This is the case of the nested template i.e. template-template parameter. And there are the following solutions:
Explicit and Complex Solution
Java
xxxxxxxxxx
1
11
1
template<
2
template <typename, typename> class C1,
3
template <typename, typename> class C2,
4
typename Alloc_C1, typename Alloc_C2,
5
typename T
6
7
void print_container(const C1<C2<T, Alloc_C2>, Alloc_C1> andcontainer) {
8
for (const C2<T, Alloc_C2> andcontainer_in : container)
9
for (const T andv : container_in)
10
cout << v << endl;
11
}
- I know this is ugly but seems more explicit.
Neat Solution
Java
xxxxxxxxxx
1
10
1
template<
2
typename T1,
3
typename T2 = typename T1::value_type,
4
typename T3 = typename T2::value_type
5
6
void print_container(const T1 andcontainer) {
7
for (const T2 ande : container)
8
for (const T3 andx : e)
9
cout << x << endl;
10
}
- As seen earlier including SFINAE .
Generic Solution: Using Variadic Template
Java
xxxxxxxxxx
1
1
template<
2
template <typename...> class C,
3
typename... Args
4
5
void print_container(C<Args...> container) {
6
for (const auto andcontainer_2nd : container)
7
for (const auto andv : container_2nd)
8
cout << v << endl;
9
}
- This is our standard solution using the variadic template will work for a single container or any number of the nested container.
Passing Function to Class Template Argument
- Passing class/struct/union to another class/struct/union as template argument is common thing. But passing function to class/struct/union as template argument is a bit rare. But yes it's possible indeed. Consider the Functional Decorator using a variadic class template .
Java
xxxxxxxxxx
1
31
1
// Need partial specialization for this to work
2
template <typename T>
3
struct Logger;
4
5
// Return type and argument list
6
template <typename R, typename... Args>
7
struct Logger<R(Args...)> {
8
function<R(Args...)> m_func;
9
string m_name;
10
Logger(function<R(Args...)> f, const string andn) : m_func{f}, m_name{n} { }
11
12
R operator()(Args... args) {
13
cout << "Entering " << m_name << endl;
14
R result = m_func(args...);
15
cout << "Exiting " << m_name << endl;
16
return result;
17
}
18
};
19
20
template <typename R, typename... Args>
21
auto make_logger(R (*func)(Args...), const string andname) {
22
return Logger<R(Args...)>(function<R(Args...)>(func), name);
23
}
24
25
double add(double a, double b) { return a + b; }
26
27
int main() {
28
auto logged_add = make_logger(add, "Add");
29
auto result = logged_add(2, 3);
30
return EXIT_SUCCESS;
31
}
- The above example may seem a bit complex to you at first sight. But if you have a clear understanding of the variadic class temple then it won't take more than 30 seconds to understand what's going on here.
Conclusion
I hope I have covered most of the topics around the C++ Template. And yes, this was a very long and intense article. But I bet you that if you do master the C++ template well, it will give you an edge. And also open a door to sub-world of C++ i.e. template meta-programming.
以上所述就是小编给大家介绍的《C++ Template Story So Far (C++11 to C++20)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
创业之初你不可不知的融资知识
桂曙光 / 机械工业出版社 / 2010-6-1 / 48.00元
从零到精通 成功融资必读书 像小说一样好看的趣味融资书 手把手教你找到VC拿到钱 本书以创业者寻找风险投资的逻辑顺序为主线,运用理论分析和实例剖析相结合的手法,将简洁、通俗的语言与丰富的图表工具相结合,辅以中肯的建议,同时运用大量鲜活的、有代表性的成败案例,为读者解读创业之初企业有效成功融资的途径和方法,帮助你的企业开创新的辉煌。一起来看看 《创业之初你不可不知的融资知识》 这本书的介绍吧!