Tutorial by Examples

Templating can also be applied to functions (as well as the more traditional structures) with the same effect. // 'T' stands for the unknown type // Both of our arguments will be of the same type. template<typename T> void printSum(T add1, T add2) { std::cout << (add1 + add2) &...
Template may accept both lvalue and rvalue references using forwarding reference: template <typename T> void f(T &&t); In this case, the real type of t will be deduced depending on the context: struct X { }; X x; f(x); // calls f<X&>(x) f(X()); // calls f<X>(...
The basic idea of a class template is that the template parameter gets substituted by a type at compile time. The result is that the same class can be reused for multiple types. The user specifies which type will be used when a variable of the class is declared. Three examples of this are shown in m...
You can define implementation for specific instantiations of a template class/method. For example if you have: template <typename T> T sqrt(T t) { /* Some generic implementation */ } You can then write: template<> int sqrt<int>(int i) { /* Highly optimized integer implement...
In contrast of a full template specialization partial template specialization allows to introduce template with some of the arguments of existing template fixed. Partial template specialization is only available for template class/structs: // Common case: template<typename T, typename U> st...
Just like in case of the function arguments, template parameters can have their default values. All template parameters with a default value have to be declared at the end of the template parameter list. The basic idea is that the template parameters with default value can be omitted while template ...
C++11 Basic example: template<typename T> using pointer = T*; This definition makes pointer<T> an alias of T*. For example: pointer<int> p = new int; // equivalent to: int* p = new int; Alias templates cannot be specialized. However, that functionality can be obtained indi...
Sometimes we would like to pass into the template a template type without fixing its values. This is what template template parameters are created for. Very simple template template parameter examples: template <class T> struct Tag1 { }; template <class T> struct Tag2 { }; templ...
Prior to C++17, when writing a template non-type parameter, you had to specify its type first. So a common pattern became writing something like: template <class T, T N> struct integral_constant { using type = T; static constexpr T value = N; }; using five = integral_constant&l...
Apart from types as a template parameter we are allowed to declare values of constant expressions meeting one of the following criteria: integral or enumeration type, pointer to object or pointer to function, lvalue reference to object or lvalue reference to function, pointer to member, st...
C++14 It is often useful to define classes or structures that have a variable number and type of data members which are defined at compile time. The canonical example is std::tuple, but sometimes is it is necessary to define your own custom structures. Here is an example that defines the structure ...
An explicit instantiation definition creates and declares a concrete class, function, or variable from a template, without using it just yet. An explicit instantiation can be referenced from other translation units. This can be used to avoid defining a template in a header file, if it will only be i...

Page 1 of 1