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++ encounter. But while programming in C++ you need a completely different mindset as both C & C++ belongs to different programming paradigm. And 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 jargons, rules & features added) i.e. C++ Template. Modern C++ I will start with a simple class/function template and as we move along, will increase the complexity. And also cover the advance topics like the , 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. variadic template 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. Basically, it helps you to see code from the compiler's perspective. Terminology/Jargon/Idiom You May Face : 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> & vector<char>, it will create two different concrete classes during compilation. This process of creating concrete classes is known as Template Instantiation. Template Instantiation : Outcome of Template Instantiation is Template Instances i.e. concrete classes. Template Instances : Usually template instantiation done at the time of object declaration. But you can also force the compiler to instantiate class/struct/union/function with particular type without even creating the object. It may appear in the program anywhere after the template definition, and for a given argument-list. Will see this later in the article. Explicit Template Instantiation : In expression template<typename > void print( ){ };, T is parameter & when you call print(5);, 5 which is of type int is template argument. This is a trivial thing for some pips. But not for non-native English speaker or beginners. So, this ambiguity has to be clear. Template Argument vs Template Parameter T T a C++ Template Types Class Template < T1, T2> : T1 first; T2 second; }; pair< , > p1; pair< , > p2; template typename typename { class pair public int char float float The basic idea of a class template is that the template parameter i.e. T1 & T2 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 < T> { a < b ? a : b; } min< >( , ); min< >( , ); template typename T min (T a, T b) return int 4 5 // Case 1 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> & <float> after the function name is redundant. Union Template Yes! a union can also be templatized. In fact, the standard library provides some utilities like , , etc. which directly or indirectly uses templatized union. std::optional std::variant < T> test { ch[ (T)]; T variable; }; template typename union uint8_t sizeof 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 templatise the variable also since C++14 < << pi< > << ; << pi< > << ; template > = (3.1415926535897932385 ); class T constexpr T pi T L // variable template cout float endl // 3.14159 cout int endl // 3 Now, you might be wondering that what is the point of the templatizing variable. But, consider the following example: < val> fib = fib<val - > + fib<val - >; <> fib< > = ; <> fib< > = ; << fib< > << ; template uint32_t constexpr auto 1 2 template constexpr auto 0 0 template constexpr auto 1 1 cout 10 endl // 55 Above code gives you 10th Fibonacci term at compile time, without even creating class or function. C++ Template Argument Overriding Template Argument Deduction < T> { << (T).name() << ; a < b ? a : b; } min< >( , ); template typename T min (T a, T b) cout typeid endl // T will be deduce as `int` return int 5.5f 6.6f // Implicit conversion happens here Default Template Arguments < T arr[N]; }; < > arr; template , = 10> { class T size_t N struct array array int Just like in 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 . Which makes function templates far more usable than they would otherwise be. Standard For example, given a function template like: < RanIt> { } template typename void sort (RanIt first, RanIt last) // . . . You can and should sort a std::vector<int> without explicitly specifying that RanIt is std::vector<int>::iterator. When the compiler sees sort(v.begin(), v.end());, it knows what the types of v.begin() and v.end() are, so it can determine what RanIt 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 pair p4{ , }; //... 1 'A' // Not OK until C++17: Can't deduce type in initialization //... But & this to work, class/struct must have an appropriate constructor. But this limitation is also relaxed in C++20. So technically . from C++17, the compiler can deduce types in class/struct initialization from C++20, you can construct the object with aggregate initialization & 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 & unsophistically implement as: < T1, T2> pair<T1, T2> make_pair(T1&& t1, T2&& t2) { {forward<T1>(t1), forward<T2>(t2)}; } template typename typename return But have you ever wonder why these helper functions are there in the standard library? How does this even help? pair< , > p1{ , }; p2 = make_pair( , ); p3 = make_pair< >( , ); int char 1 'A' // Rather using this auto 1 2 // Use this instead auto float 1 2.4f // Or specify types explicitly Rather than specifying the arguments explicitly, you can leverage the feature of inferring template argument from function template to construct the object. In the above case, template argument deduction is done by the utility function make_pair. As a result, we have created the object of pair without specifying the type explicitly. And as discussed earlier from C++17, you can construct the object without even specifying types explicitly so std::vector v{1,2,3,4}; is perfectly valid statement. Template Argument Forwarding C++ Template Reference Collapsing Rules Apart from accepting type & value in the template parameter. You can enable the template to accept both . And to do this you need to adhere to the rules of reference collapsing as follows: lvalue and rvalue references T& & becomes T& T& && become T& T&& & becomes T& T&& && becomes T&& < T> ; template typename void f (T &&t) In the above case, the real type of t depends on the context. For example: x = ; f( ); f(x); int 0 0 // deduces as rvalue reference i.e. f(int&&) // deduces as lvalue reference i.e. f(int&) In case of f(0);, 0 is rvalue of type int, hence T = int&&, thus f(int&& &&t) becomes f(int&& t). In case of f(x);, x is lvalue of type int, hence T = int&, thus f(int& &&t) becomes f(int& t). Perfect Forwarding | Forwarding Reference | Universal Reference In order to perfectly forward t to another function , one must use std::forward as: < T> { func2( ::forward<T>(t)); } template typename void func1 (T &&t) std // Forward appropriate lvalue or rvalue reference to another function Forwarding references can also be used with variadic templates: < ... Args> { func2( ::forward<Args>(args)...); } template typename void func1 (Args&&... args) std Why Do We Need Forwarding Reference in First Place? Answer to this question lies in . Though, short answer to this question is “To perform copy/move depending upon value category type”. move semantics 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 < T> { } <> < >( i) { } template typename T sqrt (T t) /* Some generic implementation */ template int sqrt int int /* Highly optimized integer implementation */ In the above case, a user that writes sqrt(4.0) will get the generic implementation whereas sqrt(4) will get the specialized implementation. Class Template Specialization < T> {} }; <> {} }; Vector< > v1; v1.print_bool(); v1.print() Vector< > v2; template typename // Common case { struct Vector void print () template // Special case <bool> { struct Vector void print_bool () int // Not OK: Chose common case Vector<T> // OK bool // OK : Chose special case Vector<bool> Partial Template Specialization Partial Class Template Specialization In contrast of a full template specialization, you can also specialise template partially with some of the arguments of existing template fixed. Partial template specialization is only available for template class/structs/union: < T1, T2> T1 first; T2 second; {} }; < T> {} }; Pair< , > p1; p1.print_first(); Pair< , > p2; p2.print(); template typename typename // Common case { struct Pair void print_first () template typename // Partial specialization on first argument as int <int, T> { struct Pair void print () // Use case 1 ---------------------------------------------------------- char float // Chose common case // OK // p1.print(); // Not OK: p1 is common case & it doesn't have print() method // Use case 2 ---------------------------------------------------------- int float // Chose special case // OK // p2.print_first(); // Not OK: p2 is special case & it does not have print_first() // Use case 3 ---------------------------------------------------------- // Pair<int> p3; // Not OK: Number of argument should be same as Primary template Partial Function Template Specialization . Function templates may only be fully specialized You cannot partially specialize method/function < T, U> { << << ; } <> foo< , >( a1, a2) { << << ; } < U> foo< , U>( t, U u) { << << ; } foo( , ); foo( , ); template typename typename void foo (T t, U u) cout "Common case" endl // OK. template void int int int int cout "Fully specialized case" endl // Compilation error: partial function specialization is not allowed. template typename void string string cout "Partial specialized case" endl 1 2.1 // Common case 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 with std::enable_if for work around as follows: SFINAE < T, :: <! ::is_pointer<T>::value> * = > func(T val) { << << ; } < T, :: < ::is_pointer<T>::value> * = > func(T val) { << << ; } a = ; func(a); func(&a); template typename typename std enable_if_t std nullptr void cout "Value" endl template typename typename std enable_if_t std nullptr void // function signature is NOT-MODIFIED NOTE: cout "Pointer" endl int 0 Non-Type Template Parameter As the name suggests, apart from types, you can also declare the template parameter as constant expressions like addresses, , integrals, , enums, etc. references std::nullptr_t Like all other template parameters, non-type template parameters can be explicitly specified, defaulted, or derived implicitly via Template Argument Deduction. The . A more relevant example of this is std::begin & std::end specialisation for array literal from the standard library: more specific use case of a non-type template is passing a plain array into a function without specifying its size explicitly < arr; } arr[] = { , , , }; begin(arr); template , > // * ( (& )[ ]) { class T size_t size Non Type Template T begin T arr size // Array size deduced implicitly return int 1 2 3 4 // Do not have to pass size explicitly Non-type template parameters are one of the ways to achieve template recurrence & enables . Template Meta-programming Nested Template: Template Template Parameter Sometimes we have to pass templated type into another templated type. And in such case, you not only have to take care of main template type but also a nested template type. Very simple template- template parameter examples is: < < > } < T> }; My_Type< > t; print_container(t); template template typename , > ( <T> & ) { class C typename T void print_container C c // . . . template typename { class My_Type // . . . int 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 . And because of that it has several disadvantages like , cannot accept references as arguments, etc. ellipses operator type-safety Variadic Class Template Implementing Unsophisticated Tuple Class(>=C++14) Since C++11 standard library introduced class that accept variable data members at compile time using the variadic template. And to understand its working, we will build our own ADT same as std::tuple std::tuple The variadic template usually starts with the general (empty) definition, that also serves as the base-case for recursion termination in the later specialisation: < ... T> }; template typename { 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 specialisation: < T, ... Rest > T first; Tuple<Rest...> rest; Tuple( T& f, Rest& ... r) : first(f) , rest(r...) { } }; Tuple< > t1( ); Tuple< , , > t2( , , ); template typename typename <T, Rest...> { struct Tuple const const bool false // Case 1 int char string 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; and Tuple<char, string> rest; data members. The rest definition again matches with specialization, yielding a structure with char first; and Tuple<string> rest; data members. The rest definition again matches this specialization, creating its own string first; and Tuple<> rest; members. Finally, this last rest matches against the base-case definition, producing an empty structure. You can visualize this as follows: Tuple< , , > -> first -> Tuple< , > rest -> first -> Tuple< > rest -> first -> Tuple<> rest -> (empty) int char string int char string char string string I have written a separate article on , if you are interested more in the variadic temple. Variadic Template C++: Implementing Unsophisticated Tuple Variadic Function Template As we have seen earlier, variadic template starts with empty definition i.e. base case for recursion. {} void print () Then the recursive case specialisation: < First, ... Rest > { << first << ; print(rest...); } template typename typename // Template parameter pack void print (First first, Rest... rest) // Function parameter pack cout endl // Parameter pack expansion This is now sufficient for us to use the print function with variable number and type of arguments. For example: print( , , ); 500 'a' "ABC" You can further optimize the print function with forwarding reference, if constexpr() & sizeof() operator as: < First, ... Rest > { { << first << ; print( ::forward<Rest>(rest)...); } { << first << ; } } template typename typename void print (First&& first, Rest&&... rest) if constexpr ( ...(rest) > ) sizeof 0 // Size of parameter pack cout endl std // Forwarding reference else cout endl 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 & 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 & 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 . And try to understand all the template instances. cppinsights 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 base case & recursive template implementation, etc. So, with . Which you can use with parameter pack as follows: C++17 standard introduced a new feature named as Fold Expression < ... Args> { ( ( << ::forward<Args>(args) << ), ...); } template typename void print (Args &&... args) void cout std endl See, no cryptic boilerplate required. Isn’t this solution looks neater? There are total 3 types of folding: Unary fold, Binary fold & 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 and class 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) while class used with dependent type. But there are cases where either typename or class has to be certain. For example To Refer Dependent Types < container> t1 = container::value_type; t2 = :: < >::value_type; }; template typename { class Example using typename // value_type depends on template argument of container using std vector int // value_type is concrete type, so doesn't require typename typename is a must while referencing a nested type that depends on template parameter. To Specify Template Template Type < < , > ( T& v : container) << v << ; } < > v; print_container(v); template template typename typename , // ` ` ++17 , > ( <T, Allocator> ) { class C class is must prior to C typename T typename Allocator void print_container C container for const cout endl vector int This is rectified in C++17, So now you can use typename also. C++11: Template Type Alias < T> pointer = T*; pointer< > p = ; < T> v = <T>; v< > dynamic_arr; template typename using int new int // Equivalent to: int* p = new int; template typename using vector int // Equivalent to: vector<int> dynamic_arr; typedef will also work fine, but would not encourage you to use. As it isn’t part of . Modern C++ C++14/17: Template & auto Keyword . It’s kind of template shorthand as follows: Since C++14, you can use auto in function argument { } < T> { } void print ( &c) auto /*. . .*/ // Equivalent to template typename void print (T &c) /*. . .*/ Although . But, you have to mention the trailing return type. Which is rectified in C++14 & now return type is automatically deduced by compiler. auto in function return-type is supported from C++11 (I will cover this in later part this article) parameters. From C++17, you can also use auto in non-type template C++20: Template Lambda Expression A 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: generic lambda expression < T> { } template typename void f ( :: <T>& vec) std vector //. . . How do you write the lambda for the above function which takes std::vector of type T? This was the limitation till C++17, but as : with C++20 it is possible templatized lambda f = []< T>( :: <T>& vec) { }; :: < > v; f(v); auto typename std vector // . . . std vector int 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 following minimalist example: value.hpp < T> T val; : ; }; # once pragma template typename { class value public T get_value () value.cpp < T> T value<T>::get_value() { val; } # include "value.hpp" template typename return main.cpp { value< > v1{ }; << v1.get_value() << ; ; } # include "value.hpp" int main () int 9 cout endl return 0 If you compile above code you will get following error: /tmp/main 4bef.o: In function `main': main.cpp:(.text+ ): undefined reference to `value< >::get_value()' clang: error: linker command failed with code (use -v to see invocation) compiler status -4b 0x1e int exit 1 exit 1 If you do explicit initialization i.e. add template class value<int>; line at the end of value.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 of value.cpp. There are other solutions as well. Check out this . StackOverflow link C++ Template Example Use Cases Curiously Recurring Template Pattern widely employed for static polymorphism or code reusability without bearing the cost of . Consider the following code: CRTP virtual dispatch mechanism < specific_animal> { implementation().who(); } : { * <specific_animal *>( ); } }; animal<dog> { { << << ; } }; animal<cat> { { << << ; } }; < specific_animal> { animal->who(); } who_am_i( dog); who_am_i( cat); template typename { struct animal void who () private specific_animal & implementation () return static_cast this : struct dog void who () cout "dog" endl : struct cat void who () cout "cat" endl template typename void who_am_i (animal<specific_animal> *animal) new // Prints `dog` new // Prints `cat` We have not used & still achieved the functionality of polymorphism(more-specifically static polymorphism). virtual keyword 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: < C> { ( &v : container) << v << ; } template typename void print_container ( C &container) const for const auto cout endl This naive way may fail if you pass anything other than standard container as other types may not have begin & end iterator. Passing std::vector to C++ Template Function Naive Way to Capture Container’s Value Type But let say, you want to pass container & want to work with container’s storage type also. You can do: < C, T = C::value_type > print_container( C &container) { ( T &v : container) << v << ; } template typename typename typename void const for const cout endl We can provide the second type parameter to our function that uses to verify that the thing is actually a container. SFINAE 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 kicks in, and that overload is removed from consideration. SFINAE Capturing Container’s Value Type Explicitly But what if you are passing vector class which doesn’t has value_type member? std::vector is defined as: < :allocator<T> > template , = : class T class Allocator std ; class vector And you can capture two template arguments of std::vector container explicitly as: < < , > ( T& v : container) << v << ; } template template typename typename , , > ( <T, Allocator> ) { class C typename T typename Allocator void print_container C container for const cout endl Above template pattern would be 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 : variadic template < < ...> ( &v : container) << v << ; } < > v{ , , , }; print_container(v); < > s{ , , , }; print_container(s); template template typename , ... > ( <Args...> ) { class C typename Args void print_container C container for const auto cout endl vector int 1 2 3 4 // takes total 2 template type argument set int 1 2 3 4 // takes total 3 template type argument Passing Container-of-Container/2D-std::vector as C++ Template Argument This is the case of nested template i.e. template-template parameter. And there are the following solutions: Explicit & Complex Solution < < , > ( C2<T, Alloc_C2> &container_in : container) ( T &v : container_in) << v << ; } template template typename typename , <typename, typename> , , , > ( <C2<T, Alloc_C2>, Alloc_C1> & ) { class C1 template class C2 typename Alloc_C1 typename Alloc_C2 typename T void print_container const C1 container for const for const cout endl I know this is ugly, but seems more explicit. Neat Solution < T1, T2 = T1::value_type, T3 = T2::value_type > print_container( T1 &container) { ( T2 &e : container) ( T3 &x : e) << x << ; } template typename typename typename typename typename void const for const for const cout endl As seen earlier including . SFINAE Generic Solution: Using Variadic Template < < ...> ( &container_2nd : container) ( &v : container_2nd) << v << ; } template template typename , ... > ( <Args...> ) { class C typename Args void print_container C container for const auto for const auto cout endl 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 bit rare. But yes it’s possible indeed. Consider the using a . Functional Decorator variadic class template < T> < R, ... Args> function<R(Args...)> m_func; m_name; Logger(function<R(Args...)> f, &n) : m_func{f}, m_name{n} { } { << << m_name << ; R result = m_func(args...); << << m_name << ; result; } }; < R, ... Args> { Logger<R(Args...)>(function<R(Args...)>(func), name); } { a + b; } { logged_add = make_logger(add, ); result = logged_add( , ); EXIT_SUCCESS; } // Need partial specialization for this to work template typename ; struct Logger // Return type and argument list template typename typename <R(Args...)> { struct Logger string const string R operator () (Args... args) cout "Entering " endl cout "Exiting " endl return template typename typename auto make_logger (R (*func)(Args...), &name) const string return double add ( a, b) double double return int main () auto "Add" auto 2 3 return Above example may seem a bit complex to you at first sight. But if you have a clear understanding of then it won’t take more than 30 seconds to understand what’s going on here. variadic class temple Conclusion I hope I have covered most of the topics around C++ Template. And yes, this was a very long & intense article. But I bet you that if you do master the C++ template well, it will really give you an edge. And also open a door to sub-world of C++ i.e. template meta-programming. Previously published at http://www.vishalchovatiya.com/c-template-a-quick-uptodate-look/