We will analyse such code
#include
struct A {
virtual ~A() {}
};
// #1 return type
template
auto construct(T* t) ->
typename std::enable_if_t, T>*
{
return new T{};
}
template
T* construct2(
T* t,
typename std::enable_if_t>* = nullptr
){
return new T{};
}
template>* = nullptr>
T* construct3(T* t)
{ return new T{}; }
int main() {
int* ptr = new int(4);
A* aptr = new A{};
auto result = construct3(aptr);
return 0;
}
#include
#include
struct Point1D {
int x;
};
struct Point2D {
int x, y;
};
struct Point3D {
int x, y, z;
};
int getY(const Point1D & p) {
std::cout << "Point1D\n";
return 0;
}
template
int getY(const T & p)
{
std::cout << "Template\n";
return p.y;
}
int main() {
Point1D a { 1 };
Point2D b { 1, 2 };
Point3D c { 1, 2, 3 };
getY(a);
getY(b);
getY(c);
return 0;
}
Your abilities after Templates – Advanced techniques training
- know the overload resolution rules and can predict which function overload will be called
- can read and understand the code with deduction guidelines, variadic template parameters, and fold expressions
- can write variadic templates with universal references and perfect forwarding
- know how to implement the SFINAE technique to optimize code
- know how to implement static polymorphism with the CRTP technique
Agenda
- intro and pre-test
- deduction guidelines
- overload resolution rules
-
SFINAE -
std::enable_if
- template template parameters
- variadic templates
- fold expressions
- CRTP
- recap
Activities
- pre-work to be done before our training
- pre-test pre-test at the beginning
- exercises followed by trainers implementation
- coding dojo
- participants solutions code review
- post-work with code review
- post-test one week after the training
- certificate of completion
Duration
- 1 day (7 hours with breaks)
Form
- online
- classroom