Back to Basics: Templates (part 1 of 2) - Bob Steagall - CppCon 2021

preview_player
Показать описание
---
Generic programming is powerful idiom for building reusable software components whose behavior can be tailored (parametrized) at compile time. The C++ language supports generic programming with templates, which are at the heart of the Standard C++ Library and the basis of many popular libraries like Eigen, JUCE, POCO C++, and Boost.

This two-part series begins with a brief overview of generic programming and why it is so important and useful in modern C++. We'll look at how C++ templates support generic programming, and provide some examples of the kinds of templates most likely to be encountered: function templates, class templates, alias templates, and variable templates. We'll then provide some practical tips for using templates, debugging template code, and structuring code that contains your own templates.

After that, we'll define and discuss the most important aspects of templates, such as parameters and arguments, substitution and instantiation, specialization, qualification, two-phase translation, non-type parameters, the one-definition rule, linkage, and more. Finally, we'll take a look at some slightly more advanced topics like variadic templates, type traits, tag dispatch, and the new lambda template feature in C++20.

These sessions are for C++ programmers seeking a tutorial or refresher on template fundamentals. If you've avoided templates in the past, or felt a little intimidated by them, or want to gain a better understanding of how templates work and how to use them on a daily basis, then this series is for you. Attendees will leave with a basic understanding of what C++ templates are, how they support perform generic programming, the most common kinds of C++ templates and their uses, the fundamental concepts underlying templates, important template terminology, and how to use and write basic templates in everyday code.

---
Bob Steagall
Program Chair, KEWB Computing

---

*--*
Рекомендации по теме
Комментарии
Автор

Mr Steagall is a good lecturer. Excellent and thorough presentation.

OptimusVlad
Автор

Thank you for making this point. It's not a templated class/function/thing, it's a class/function/thing template. Meaning a template for making classes/functions/things.

Fetrovsky
Автор

20:40 it should be `if constexpr (is_arithmetic_v<T>)`, using the template just above to return the bool value needed for the expression.

SamWhitlock
Автор

What is and why we use "const&" in...

Template<class T>
T const& min(...)
{ return...}

At 33:26

Thanks in advance, it was a pleasure listening to you!

karlo
Автор

John Kalb's acting in that ad at the start cracks me up every time :)
He does great work for this conference, but I don't think he's heading to Hollywood any time soon lol

superscatboy
Автор

Great presenter, very honest and clear. Thanks for the video

hugoyoutube
Автор

Very structural explanation. Enjoyed it a lot. Some typos in code - probably it is a sleep test …. Spasibo!

apivovarov
Автор

"templetization" is a thing: It it the act of taking a non-template and transforming it into a template.
"parametrize" in math has a very different meaning - and in C++ "parameter" also already has a very different meaning making "parametrize" ill-suited at best.

ABaumstumpf
Автор

I am more than happy while listening the presentation. Thanks.

nguyendaison
Автор

This is indeed a superb talk. Thank you.

ChristianBrugger
Автор

thanks for the talk. you cleared many doubts on template. waiting for 2/2.

sanjaygatne
Автор

I believe one point that was missed in this talk w.r.t. permissive rules for multiple definitions when "inline" keyword is used is that all those definitions have to be consistent across all translation units and that each translation unit gets its own definition of the inline variable or inline function.

pleaseexplain
Автор

It was driving me mad trying to get the last couple of code examples working (the explicit specialisations of class templates). I created a map of ints, initialised with a few values, to test the primary class specialisation for the my_less functor:

std::map<int, int, my_less<int>> m1{ {1, 1154}, {2, 768}, {3, 2653} };

but was getting an "invalid comparator" error at runtime. I hold these CPPCon presenters in such high regard, so I always assume it's me making the mistake, but in fact in this case it wasn't. There is a bug in that functor. Change this:

template<class T>
struct my_less {
bool operator()(const T& a, const T& b) const {
return (a < b) ? a : b;
}
};

To this:

template<class T>
struct my_less {
bool operator()(const T& a, const T& b) const {
return (a < b);
}
};

The explicit specialisation example (the functor my_less<const char*>) works perfectly well as it is.

chiefsittingstill
Автор

Great video and presentation. I have learned something new today about C++ template.

wangcwy
Автор

Tribute to ada83 which was IMHO the first widely adopted language to offer "generic" years before C++

alexandrebustico
Автор

Can someone provide any references on the look-up rules for function templates vs look-up rules for member function templates?

pleaseexplain
Автор

Why memcpy(p, 0, ...); instead of memset ? Time frame 21:00

ИльнарБорханов
Автор

@40:40 Typo. No = btw class A and default allocator

apivovarov
Автор

Can someone explain to me the slide at 33:54, specifically the last function definition? I am okish at templates but I really do not understand that. So we have a type/class T, and a class Allocator, and another class input Iterator, but then we get an auto(return type) of namespace vector<T, Alloc>::insert, that is declaring the the return is an iterator of unknown type? does it type deduce the iterator? I am reading that right? I have not seen it written that way before.

scienceandmathHandle
Автор

How to make the code at 57:52 work? I've tried various ways but got the compiled error:
"error: template-id 'min<>' for 'const char* min(const char*, const char*)' does not match any template declaration"

antoninguyen