CppCon 2014: Walter E. Brown "Modern Template Metaprogramming: A Compendium, Part II"

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ต.ค. 2014
  • www.cppcon.org
    -
    Presentation Slides, PDFs, Source Code and other presenter materials are available at: github.com/CppCon/CppCon2014
    --
    Template metaprogramming has become an important part of a C++ programmer's toolkit. This talk will demonstrate state-of-the-art metaprogramming techniques, applying each to obtain representative implementations of selected standard library facilities.
    Along the way, we will look at void_t, a recently-proposed, extremely simple new type_traits candidate whose use has been described by one expert as "highly advanced (and elegant), and surprising even to experienced template metaprogrammers."
    --
    With broad experience in industry, academia, consulting, and research, Dr. Walter E. Brown has been a C++ programmer for over thirty years, joining the C++ standards effort in 2000. Among numerous other contributions, he is responsible for introducing such now-standard C++ library features as cbegin/cend and common_type as well as headers random and ratio , and has significantly impacted such core language features as alias templates, contextual conversions, and variable templates. He conceived and served as project editor for the International Standard on Special Mathematical Functions in C++.
    When not playing with his grandchildren, Dr. Brown is an Emeritus participant in the C++ standards process, with several more core and library proposals under consideration. He was recently appointed an associate project editor for the C++ standard itself.
    --
    Videos Filmed & Edited by Bash Films: www.BashFilms.com
    *-----*
    Register Now For CppCon 2022: cppcon.org/registration/
    *-----*

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

  • @MrIosonoleggenda
    @MrIosonoleggenda 9 ปีที่แล้ว +45

    Thank you Walter E. Brown...I could see the happiness in your eyes when you were talking.
    The best talk in the CppCon.

  • @oisyn
    @oisyn 3 ปีที่แล้ว +26

    Absolute great talk by Dr. Brown! However, I don't really like his answer to the question asked at 43:20. He says "more specialized". Although true, it's important to keep in mind that default template arguments are only part of the declaration, and have nothing to do with the main unspecialized definition that just happens to be in the same line of code. It only started to make sense to me after realizing this. The main definition still defines the generic type, and you can specialize for the case where the arguments are in fact the default arguments further on in a separate specialization.
    Here's what I mean:
    template struct foo { ... }; // #1
    template struct foo { ... }; // #2
    The generic class template foo is defined in #1. It mentions a default argument of void, but that has nothing to to with the actual definition of the generic case! The specialization for when it actually *is* void is provided in #2. Perhaps it helps to read it like this:
    template struct foo; // just declaration, provides the defaults
    template struct foo { ... }; // #1
    template struct foo { ... }; // #2
    This way the disconnect between the default arguments and the main definition and its specializations are more clear.
    So, in the case of has_member_function, there is another implicit argument that's being used in the 'call', which defaults to void, so it's actually has_member_function. Furthermore, it just so happens to have a specialization for if that void_t thing happens to be well-formed. Otherwise, the whole specialization doesn't exist due to SFINAE.

  • @i.8530
    @i.8530 ปีที่แล้ว +11

    this talk has singlehandedly tought me more than all metaprogramming guides i have read till now. this is truly a fantastic beginner friendly overview.

  • @__hannibaalbarca__
    @__hannibaalbarca__ 8 หลายเดือนก่อน +1

    Before 9 month and after 4 months of c++ learning__self learning__ i understood only 20 % , after 9 months, Im proud of finally u derstand 120% of talk, very good feeling.

  • @mywtfmp3
    @mywtfmp3 6 ปีที่แล้ว +11

    This old man makes me love template metaprogrammig.

  • @bobweiram6321
    @bobweiram6321 5 หลายเดือนก่อน

    In Part I, Dr. Brown prefaced his talk as covering advanced topics not intended for beginners. He underestimates, however, his ability to distill a complex topic in a clear and intuitive way. I've watched several videos and tried to read a few books and articles on this topic, but they were difficult to grasp. Their explanations quickly glossed over the odd notation, and their chosen examples were obfuscated by other C++ areas. I only planned on watching for a few minutes, expecting to be intimidated and demoralized, but found myself fully engrossed with a clear understanding of the topic.

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

    Such a genius!
    I loved every minute of your talk Dr. Brown.

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

    It is an amazing presentation. Thank you Walter E. Brown and CppCon for such a great content on metaprogramming.

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

    This is one of my favorite talks of CppCon along with Jon Kalb's talk on exceptions.

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

    Both, the given presentation, as well the presented technique of metaprogramming are exceptionally excellent. Bjarne couldn't do better. Thank you Walter E. Brown.

  • @redchards
    @redchards 8 ปีที่แล้ว +3

    I must admit when I first saw it in a stackoverflow thread (can't quite remember which), I was shocked. It's a huge improvement compared to what we had before, this ugly "no evaluation" trick. Nice talk !

  • @anirbansarkar8518
    @anirbansarkar8518 5 ปีที่แล้ว

    This is genius! Mind... Blown!

  • @LamboPhil
    @LamboPhil 9 ปีที่แล้ว +7

    I'm gonna call it emvoiden for the time being.
    Been some time since I last saw something of this degree of awesomeness done to C++, but I wonder if there'll ever be an end to the discoveries in the field of spectacular abuse of Bjarne's accidental meta-child.

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

    I learned a lot, and I'm still trying to process it. mostly it takes month to settle down with clarity of what I learned here.

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

    Impressive. Thank you

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

      Thank you too!

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

    Awesome talk

  • @lgsunnyvale
    @lgsunnyvale 3 ปีที่แล้ว

    Awesome!

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

    eye opening

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

    23:10 I wonder if the is_copy_assignable follows the convention. If its _type_ member is aliased to true_type or false_type, wouldn’t one need to write is_copy_assignable::type::value to evaluate it? Wouldn’t you rather define a constexpr bool member _value_ with _decltype( try_assignment(declval()) )::value_ as its definition ?

  • @foxtacles
    @foxtacles 9 ปีที่แล้ว +5

    void_t for president! fucking awesome

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

    Last week I saw a trick to create stateful constexpr functions and through this stateful template metaprogramming. Now that was something *really* unexpected. So much unexpected that Standards Committee is considering rewording parts of the standard that allow this for C++17.

    • @eepp
      @eepp 6 ปีที่แล้ว

      Link?

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

      Yes, I would love to see that link also, if you could!! (I know that the comment was 4 years ago, but hopefully you still remember the idea!

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

    Legend

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

    these 2 hr(+) were spent profoundly

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

    32:00 I loved it when he laughed at that piece of code

  • @Slicc12345
    @Slicc12345 9 ปีที่แล้ว

    correct. but because of reference collapsing it doesn't make a difference cause T&& && will be T&&

  • @ElizaberthUndEugen
    @ElizaberthUndEugen 7 ปีที่แล้ว +12

    So why does void_t and class = void have to match?

    • @J1337H4X0R
      @J1337H4X0R 7 ปีที่แล้ว +9

      Because when you call the metafunction, you get [V = void] as the default, and so to get the specialization to activate on that template parameter (the other being a generic type T that can't be specialized), you have to have the same type (V) for that template argument, which means the e.g. void_t, has to be an alias for V, in this case void. void_t is just a weird way of writing void that also does SFINAE on its (unused) template parameters. Without the standardization fix mentioned in the video, the unused parameters might be ignored such that they don't activate SFINAE and so you need the more complex definition of void_t.

    • @michaelkohlhaas4427
      @michaelkohlhaas4427 4 ปีที่แล้ว

      @@J1337H4X0R Wrong!

    • @krzysztofkosinski6664
      @krzysztofkosinski6664 4 ปีที่แล้ว +3

      Because you invoke the template as has_type_member.
      If they didn't match, has_type_member would not consider the specialization at all, because the second type parameter would never match.

    • @oisyn
      @oisyn 3 ปีที่แล้ว

      I added a top-level comment that might answer your question.

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

    48:50 You rock!

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

    It's not working in MSVC2013 because of the missing expression SFINAE, as Stephan Lavavej mentioned at 25'. Otherwise I would have started my own little concepts lite right now. Damn.

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

    Thanks for the very good and explanatory talk.
    Here is the simplified version of is_one_of thanks to fold expressions.
    template< typename T , typename... T0toTN >
    struct is_one_of : std::conditional_t< ( std::is_same_v< T , T0toTN > || ... ) ,
    std::true_type ,
    std::false_type >
    {};
    // More simplified
    template
    struct is_one_of : std::bool_constant< ( std::is_same_v< Head , Remaining > && ... ) >
    {};

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

      The last one should better not be named Head and Remaining. They do not form a list conceptually.
      template
      struct is_one_of : std::bool_constant {};

  • @tk-jf6wc
    @tk-jf6wc 6 ปีที่แล้ว

    wizard!

  • @MaceUA
    @MaceUA 9 ปีที่แล้ว

    Great talk! void_t is an awesome trick. Still, I don't understand one thing. See 50:45, changing is_copy_assignable to is_move_assignable: why "T const&" is changed to "T&&" but not to "T"? declval(T) is already returning an rvalue of type T, isn't it?

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

      For other people coming here I'll answer this.
      It's because the return type of `declval` is an rvalue reference, and we're acting like we're passing a value of that type into the `try_assignment`.

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

    template
    struct is_copy_assignable {
    private:
    template
    static std::true_type try_assignment(U&&);
    static std::false_type try_assignment(...);
    public:
    using type = decltype(try_assignment(std::declval()));
    };
    This version of copy assignable doesn't seem to work with std::string or any type that I've defined with the assignment operator. Any idea why?

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

    I kinda follow

  • @michal.gawron
    @michal.gawron 9 ปีที่แล้ว +1

    Wouldn't it be better to name void_t as ensure_well_formed_t?

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

    26:53

  • @Slicc12345
    @Slicc12345 9 ปีที่แล้ว

    korrect. but because of reference collapsing it doesn't make a difference couase T&& && will be T&&

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

    10:47

  • @michal.gawron
    @michal.gawron 9 ปีที่แล้ว +1

    Or I just found a better candidate: when_valid_t!

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

    First time i see this; is look for me like alien languages.

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

    Can't has_member_type be implemented as below
    template
    struct has_member_type : false_type{};
    template
    struct has_member_type : true_type{};
    without the use of void_t at all.
    I checked it with
    cout

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

      Your code is only working when typename T::type is void, in which case the compiler can invoke the partial specialization of the has_member_type template, otherwise it will default to the general case. Try this with the following struct and you'll see its not working:
      struct Dummy
      {
      using type = int;
      };