fbpx ...

Templates – Basics

Templates are pointed out to be the most difficult part of the C++ language. Difficult, but very useful. They allow writing very generic and universal code. Thanks to them, we can avoid code duplication, but also, make it incomprehensible for beginners. This session allows for gaining the necessary knowledge and good practices on using templates.

We will analyse such code

  #include <iostream>

template <typename T, typename U>
class SomeClass {
public:
    T getValue() { return value; }
private:
    T value = {};
    U* ptr = nullptr;
};

int main() {
    SomeClass<int, void> sc;
    std::cout << sc.getValue() << std::endl;
    return 0;
}  
  template <typename T1, typename T2, typename I>
class A {};                  // primary template

template<typename T, typename I>
class A<T, T*, I> {};        // #1: partial specialization where T2 is a pointer to T1

template<typename T, typename T2, typename I>
class A<T*, T2, I> {};       // #2: partial specialization where T1 is a pointer

template<typename T>
class A<int, T*, double> {}; // #3: partial specialization where T1 is int, I is double,
                             //     and T2 is a pointer

template<typename X, typename T, typename I>
class A<X, T*, I> {};        // #4: partial specialization where T2 is a pointer  

Your abilities after Templates – Basics training

Agenda

Activities

Duration

Form

Order Templates – Basics training

Related trainings

Templates: advanced techniques

More advanced C++ template programming and metaprogramming

Move semantics

Forwarding references and perfect forwarding are used in template functions
ninjaletter

Już uciekasz?

Zanim to zrobisz, zapisz się na Ninjaletter, aby wiedzieć, co piszczy w C++. 

Informujemy, iż w celu realizacji usług dostępnych w naszym serwisie, optymalizacji jej treści, dostosowania strony do Państwa indywidualnych potrzeb oraz wyświetlania, personalizacji i mierzenia skuteczności reklam w ramach zewnętrznych sieci reklamowych korzystamy z informacji zapisanych za pomocą plików cookies na urządzeniach końcowych użytkowników. Pliki cookies można kontrolować za pomocą ustawień swojej przeglądarki internetowej. Dalsze korzystanie z naszego serwisu, bez zmiany ustawień przeglądarki internetowej oznacza, iż użytkownik akceptuje stosowanie plików cookies. Więcej informacji zawartych jest w polityce prywatności serwisu.