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

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ธ.ค. 2024

ความคิดเห็น • 41

  • @SamWhitlock
    @SamWhitlock 2 ปีที่แล้ว +10

    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 ปีที่แล้ว +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 ปีที่แล้ว +8

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

    • @CppCon
      @CppCon  2 ปีที่แล้ว +3

      Thank you kindly!

  • @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 ปีที่แล้ว +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 10 หลายเดือนก่อน

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

  • @chiefsittingstill6061
    @chiefsittingstill6061 2 หลายเดือนก่อน

    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 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
    struct my_less {
    bool operator()(const T& a, const T& b) const {
    return (a < b) ? a : b;
    }
    };
    To this:
    template
    struct my_less {
    bool operator()(const T& a, const T& b) const {
    return (a < b);
    }
    };
    The explicit specialisation example (the functor my_less) works perfectly well as it is.

  • @ИльнарБорханов
    @ИльнарБорханов 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 ปีที่แล้ว

      exactly!

  • @antoni2nguyen
    @antoni2nguyen 2 ปีที่แล้ว +1

    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;
      }

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

  • @apivovarov2
    @apivovarov2 ปีที่แล้ว

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

  • @pleaseexplain4396
    @pleaseexplain4396 ปีที่แล้ว +1

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

  • @hugoyoutube1226
    @hugoyoutube1226 ปีที่แล้ว +1

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

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

  • @pleaseexplain4396
    @pleaseexplain4396 ปีที่แล้ว

    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.

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

  • @ABaumstumpf
    @ABaumstumpf 10 หลายเดือนก่อน

    "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 ปีที่แล้ว

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

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

  • @wangcwy
    @wangcwy ปีที่แล้ว

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

  • @PUZO37RS
    @PUZO37RS 2 ปีที่แล้ว +2

    Thank you! Very good talk!

  • @not_ever
    @not_ever 2 ปีที่แล้ว

    Hello cppcon. You guys have the link to cppcon2020 slides in the description rather than 2021

    • @CppCon
      @CppCon  2 ปีที่แล้ว

      Thank you for your comment. We are seeking to resolve this issue asap.

  • @miguel119x
    @miguel119x ปีที่แล้ว

    19:00 if_constexpr

  • @AbcAbc-gs5fb
    @AbcAbc-gs5fb 2 ปีที่แล้ว

    12:50

  • @alexandrebustico9691
    @alexandrebustico9691 2 ปีที่แล้ว +1

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