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

  Рет қаралды 41,412

CppCon

CppCon

Күн бұрын

Watch Part 2: • Back to Basics: Templa...
cppcon.org/
github.com/Cpp...
---
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
---
Videos Filmed & Edited by Bash Films: www.BashFilms.com
KZbin Channel Managed by Digital Medium Ltd events.digital...
*--*

Пікірлер: 40
@superscatboy
@superscatboy 2 жыл бұрын
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
@SamWhitlock
@SamWhitlock 2 жыл бұрын
20:40 it should be `if constexpr (is_arithmetic_v)`, using the template just above to return the bool value needed for the expression.
@atib1980
@atib1980 2 жыл бұрын
Yes, that's correct. You want to know the value of the static const/constexpr bool constant named 'value' (defined in the is_arithmetic struct) in order to evaluate the if expression at compile time. It can also be explained as follows. You want to call the is_aritmethic value returning metafunction to check its return value in order to evaluate the if expression at compile time.
@OptimusVlad
@OptimusVlad 2 жыл бұрын
Mr Steagall is a good lecturer. Excellent and thorough presentation.
@CppCon
@CppCon 2 жыл бұрын
Thank you kindly!
@ИльнарБорханов
@ИльнарБорханов 2 жыл бұрын
Why memcpy(p, 0, ...); instead of memset ? Time frame 21:00
@bobsteagall8453
@bobsteagall8453 2 жыл бұрын
Good catch! That is a typo, and you're correct - it should be memset(). Thanks :-)
@Roibarkan
@Roibarkan 2 жыл бұрын
Perhaps also is_trivially_constructible instead of is_arithmeic
@asmwarriorYT
@asmwarriorYT Жыл бұрын
@@bobsteagall8453 It should be is_arithmeic_v, I think you have forget the _v in 21:00, thanks
@apivovarov2
@apivovarov2 9 ай бұрын
exactly!
@ChristianBrugger
@ChristianBrugger 2 жыл бұрын
This is indeed a superb talk. Thank you.
@CppCon
@CppCon 2 жыл бұрын
Thanks for listening
@Fetrovsky
@Fetrovsky 2 жыл бұрын
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.
@pleaseexplain4396
@pleaseexplain4396 Жыл бұрын
Can someone provide any references on the look-up rules for function templates vs look-up rules for member function templates?
@nguyendaison27
@nguyendaison27 2 жыл бұрын
I am more than happy while listening the presentation. Thanks.
@CppCon
@CppCon 2 жыл бұрын
You are most welcome
@PUZO37RS
@PUZO37RS 2 жыл бұрын
Thank you! Very good talk!
@sanjaygatne1424
@sanjaygatne1424 2 жыл бұрын
thanks for the talk. you cleared many doubts on template. waiting for 2/2.
@CppCon
@CppCon 2 жыл бұрын
Glad it was helpful!
@karlo4719
@karlo4719 2 жыл бұрын
What is and why we use "const&" in... Template T const& min(...) { return...} At 33:26 Thanks in advance, it was a pleasure listening to you!
@atib1980
@atib1980 2 жыл бұрын
const T& min(const T& x, const T& y) { return x < y ? x : y; } You want to take both arguments by reference so that whenever you call min(value1, value2) or min(object1, object2) you don't make copies of the passed function arguments and you pass them by const because you don't want the function min to modify their values or state in any way. Also if the type T that you use min(...) with doesn't have a copy constructor it's impossible to call T min(T x, T, y) with objects of type T because the compiler cannot create a copy of them at runtime due to their missing or explicitly deleted/private copy constructor. Also, the fact that you take both arguments x and y by const & means you don't want to modify their values/state inside the function and you don't want to create a copy of them either. Finally you return a const & to the 'lesser' object which refers to (returned const reference gets initialized to) one of the 2 functions arguments (x, y) depending on the evaluated result of the expression in the return statement.
@pedrozo-
@pedrozo- 2 жыл бұрын
@@atib1980 I imagine Karlo wanted to understand if there is a difference between "T const& min(...)" and "const T& min(...)". At least is what I also wanted to know. :D
@not_ever
@not_ever Жыл бұрын
@@pedrozo- const modifies what is to the left of it. If nothing is to the left then it modifies what is on the right. "T const& min(...)" and "const T& min(...)" are equivalent. One is following the convention of "East const" and the other "West const".
@ABaumstumpf
@ABaumstumpf 7 ай бұрын
@@atib1980 Taking the parameters of "min" by reference "saves" you a copy of the value pre optimization at the cost of introducing UB... one of the biggest pitfalls of std::min
@ABaumstumpf
@ABaumstumpf 7 ай бұрын
"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.
@apivovarov2
@apivovarov2 9 ай бұрын
Very structural explanation. Enjoyed it a lot. Some typos in code - probably it is a sleep test …. Spasibo!
@scienceandmathHandle
@scienceandmathHandle Жыл бұрын
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::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.
@pleaseexplain4396
@pleaseexplain4396 9 ай бұрын
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.
@apivovarov2
@apivovarov2 9 ай бұрын
@40:40 Typo. No = btw class A and default allocator
@wangcwy
@wangcwy Жыл бұрын
Great video and presentation. I have learned something new today about C++ template.
@antoni2nguyen
@antoni2nguyen 2 жыл бұрын
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"
@atib1980
@atib1980 Жыл бұрын
There is a bug on that slide regarding the explicit specizalization of the min function template. The correct explicitly specialized function template for min should be defined as follows: template const char* const& min(const char* const& pa, const char* const& pb) { return strcmp(pa, pb) < 0 ? pa : pb; }
@not_ever
@not_ever Жыл бұрын
Hello cppcon. You guys have the link to cppcon2020 slides in the description rather than 2021
@CppCon
@CppCon Жыл бұрын
Thank you for your comment. We are seeking to resolve this issue asap.
@miguelalbertooxa8699
@miguelalbertooxa8699 Жыл бұрын
19:00 if_constexpr
@alexandrebustico9691
@alexandrebustico9691 2 жыл бұрын
Tribute to ada83 which was IMHO the first widely adopted language to offer "generic" years before C++
@AbcAbc-gs5fb
@AbcAbc-gs5fb 2 жыл бұрын
12:50
@hugoyoutube1226
@hugoyoutube1226 9 ай бұрын
Great presenter, very honest and clear. Thanks for the video
💩Поу и Поулина ☠️МОЧАТ 😖Хмурых Тварей?!
00:34
Ной Анимация
Рет қаралды 2 МЛН
Nastya and balloon challenge
00:23
Nastya
Рет қаралды 69 МЛН
Andrew Kelley   Practical Data Oriented Design (DoD)
46:40
ChimiChanga
Рет қаралды 108 М.
Let's get comfortable with SFINAE (C++)
35:55
platis.solutions
Рет қаралды 8 М.
Why Isn't Functional Programming the Norm? - Richard Feldman
46:09
Why i think C++ is better than rust
32:48
ThePrimeTime
Рет қаралды 308 М.
Back to Basics: Concurrency - Mike Shah - CppCon 2021
1:02:07