Back to Basics: Templates (part 1 of 2) - Andreas Fertig - CppCon 2020

preview_player
Показать описание

---

Template Basics
This talk is a two-parter. In the first part we start with the question what is generic programming. I then will introduce the different kinds of templates: function, class and method templates. We will walk through how to write and apply. Along with that we distinguish the different types of template parameters type, and non-type using std::array as an illustration. This is accompanied by a peak behind the scenes by using C++ Insights. This should also give you a valuable tool where you can checkout details for yourself.

At this point we look at alias templates and how they can help us.

Once we covered the basics, we take care that our templates do not bloat the code. For that we use an example of passing an array and a length and optimize this. While on that, we consider at type_traits and incorporate them in our solution. With type_traits we use constexpr if for conditional compile-time code paths.
This session is for developers with C++ experience who have avoided templates so far. After attending this talk, attendees have learned to think in types rather than in values and can write their own templates.

---
Andreas Fertig
Unique Code

Andreas Fertig is the CEO of Unique Code GmbH, which offers training and consulting for C++ specialized in embedded systems. He worked for Philips Medizin Systeme GmbH for ten years as a C++ software developer and architect focusing on embedded systems.

Andreas is involved in the C ++ standardization committee. He is a regular speaker at conferences internationally. Textbooks and articles by Andreas are available in German and English.

---

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

I love the use of C++ Insights, it really helps me understand what templating is doing under the hood without a 5th dimensional brain.

communistpropagandist
Автор

I got so much out of the first viewing of this video on this topic that often fills people with consternation. ~45:23 on the second viewing, I now get that due to multiple inheritance being allowed in C++, and, in fact, multiple inheritance of class templates, that you could not just simply call Func() there, as it could be ambiguous. Now that didn't happen in this case, so it would again seem like the compiler could see that and not be fussy, but yes, I see that if Bar multiply inherited Foo, that Func() would be ambiguous. What I don't see then is what this-> would buy us in that case. Wouldn't this->Func() still be ambiguous in the case that we had multiply inherited from Foo<T> and Foo<NotT> in Bar<T>? Maybe what is going on is that in the first pass this makes the compiler happy enough to not complain, and then when it compiles the instantiation this...no, it still seems to me that in the weird case you described of multiple inheritance of Foo in Bar, that this->Func() would still be ambiguous at that point, so I don't see how it would help us.
EDIT: on the third listen, it sounds like only Foo<T>::func would help us in the case of multiple inheritance of different Foo specializations.

jvsnyc
Автор

It's funny to see the German word "seit" instead of "since" on slide 4 at 7:46.

michaelkotthaus
Автор

At 30:30 you are just updating the wrong line. The char array declared on line 32 doesn't get it's size used. I think you meant to update the char array declared on line 35.

communistpropagandist
Автор

There are template templates? I need to watch every single video in the series.

max_ishere
Автор

You may want to start watching the video at min 8.

zhaoli
Автор

Thank you for this nice and easy to understand presentation.

Just want point out that you have written
"void Main" everywhere, i guess it is a typing error.

hidemytracks
Автор

He says, don't use manual for loops, use range for loops. Sure, I agree, good advice. Then he goes on to use printf instead of cout and doesn't even mention it...

Sayuri
Автор

I watch the video on 1.5x and it is still extremely slow

ВадимКлимов-йщ
Автор

I have never heard, that the C++ compiler/linker will accept the "Main()" function as the program entry point.
C++ Templates are well known to strongly reduce the speed of program execution.

alles_muss_anders_werden