Back to Basics: Templates (part 1 of 2) - Bob Steagall - CppCon 2021
Вставка
- Опубліковано 12 вер 2024
- 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
UA-cam Channel Managed by Digital Medium Ltd events.digital...
*--*
20:40 it should be `if constexpr (is_arithmetic_v)`, using the template just above to return the bool value needed for the expression.
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.
Mr Steagall is a good lecturer. Excellent and thorough presentation.
Thank you kindly!
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
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.
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!
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.
@@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
@@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".
@@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
I am more than happy while listening the presentation. Thanks.
You are most welcome
This is indeed a superb talk. Thank you.
Thanks for listening
thanks for the talk. you cleared many doubts on template. waiting for 2/2.
Glad it was helpful!
Great presenter, very honest and clear. Thanks for the video
Very structural explanation. Enjoyed it a lot. Some typos in code - probably it is a sleep test …. Spasibo!
"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.
Great video and presentation. I have learned something new today about C++ template.
Thank you! Very good talk!
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.
@40:40 Typo. No = btw class A and default allocator
Why memcpy(p, 0, ...); instead of memset ? Time frame 21:00
Good catch! That is a typo, and you're correct - it should be memset(). Thanks :-)
Perhaps also is_trivially_constructible instead of is_arithmeic
@@bobsteagall8453 It should be is_arithmeic_v, I think you have forget the _v in 21:00, thanks
exactly!
Can someone provide any references on the look-up rules for function templates vs look-up rules for member function templates?
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.
Tribute to ada83 which was IMHO the first widely adopted language to offer "generic" years before C++
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"
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;
}
Hello cppcon. You guys have the link to cppcon2020 slides in the description rather than 2021
Thank you for your comment. We are seeking to resolve this issue asap.
19:00 if_constexpr
12:50