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...
    *--*

КОМЕНТАРІ • 40

  • @SamWhitlock
    @SamWhitlock 2 роки тому +8

    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 роки тому +1

      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 роки тому +8

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

    • @CppCon
      @CppCon  2 роки тому +3

      Thank you kindly!

  • @superscatboy
    @superscatboy 2 роки тому +4

    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

  • @Fetrovsky
    @Fetrovsky 2 роки тому +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.

  • @karlo4719
    @karlo4719 2 роки тому +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 роки тому +1

      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 роки тому +1

      @@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 Рік тому +2

      @@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 6 місяців тому

      @@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

  • @nguyendaison27
    @nguyendaison27 2 роки тому +2

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

    • @CppCon
      @CppCon  2 роки тому

      You are most welcome

  • @ChristianBrugger
    @ChristianBrugger 2 роки тому +5

    This is indeed a superb talk. Thank you.

    • @CppCon
      @CppCon  2 роки тому +1

      Thanks for listening

  • @sanjaygatne1424
    @sanjaygatne1424 2 роки тому +2

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

    • @CppCon
      @CppCon  2 роки тому

      Glad it was helpful!

  • @hugoyoutube1226
    @hugoyoutube1226 8 місяців тому +1

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

  • @apivovarov2
    @apivovarov2 9 місяців тому

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

  • @ABaumstumpf
    @ABaumstumpf 6 місяців тому

    "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.

  • @wangcwy
    @wangcwy Рік тому

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

  • @PUZO37RS
    @PUZO37RS 2 роки тому +2

    Thank you! Very good talk!

  • @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

  • @user-rc4nd9xt3h
    @user-rc4nd9xt3h 2 роки тому +5

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

    • @bobsteagall8453
      @bobsteagall8453 2 роки тому +4

      Good catch! That is a typo, and you're correct - it should be memset(). Thanks :-)

    • @Roibarkan
      @Roibarkan 2 роки тому +1

      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!

  • @pleaseexplain4396
    @pleaseexplain4396 Рік тому +1

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

  • @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.

  • @alexandrebustico9691
    @alexandrebustico9691 2 роки тому +1

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

  • @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

  • @AbcAbc-gs5fb
    @AbcAbc-gs5fb 2 роки тому

    12:50