Back to Basics: Move Semantics (part 2 of 2) - Klaus Iglberger - CppCon 2019

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ก.ย. 2024
  • CppCon.org
    Discussion & Comments: / cpp
    Presentation Slides, PDFs, Source Code and other presenter materials are available at: github.com/Cpp...
    -
    Back to Basics: Move Semantics (part 2 of 2)
    Move semantics is one of the most complex topics in the world of C++, including many technical details that often confuse even experts. This interactive back-to-the-basics session is entirely focused on understanding the details behind move semantics. It explains the motivation behind move semantics, the need for rvalue references and std::move, the reason for forwarding references and std::forward, and how to properly apply move semantics. The many interactive questions and exercises will help to quickly adapt the newly gained knowledge.
    -
    Klaus Iglberger
    Klaus Iglberger is a freelancing C++ trainer and consultant and is currently on the payroll of Siemens in Nuremberg, Germany. He has finished his PhD in computer science in 2010 and since then is focused on large-scale C++ software design. He shares his experience in popular advanced C++ courses around the world (mainly in Germany, but also the EU and US). Additionally, he is the initiator and lead designer of the Blaze C++ math library (bitbucket.org/...) and the organizer of the Munich C++ user group (www.meetup.com....
    -
    Videos Filmed & Edited by Bash Films: www.BashFilms.com
    *-----*
    Register Now For CppCon 2022: cppcon.org/reg...
    *-----*

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

  • @think2086
    @think2086 3 ปีที่แล้ว +33

    Fantastic talk. Well presented. Great examples. Good teaching style. Good pace. Almost all presenters go too fast and try to talk over their code without giving people a chance to grok the code (which is often overly complex) or talking through the code. Klaus, on the either hand, gave us time to reflect, and the code was presented purely and succinctly. Good stuff.

  • @PraveenKulkarni1996
    @PraveenKulkarni1996 4 ปีที่แล้ว +21

    This was a brilliant talk. I loved the use of positive and negative examples. The second example, at 33:24 was especially subtle, and that killed off a misconception that I formed in 2:15. Kudos.
    For other viewers here, I'd recommend that this be paired with "CppCon 2014: Scott Meyers "Type Deduction and Why You Care" referenced at 4:47.

  • @erik7726
    @erik7726 3 ปีที่แล้ว +11

    Excellent explanation of std::forward and perfect forwarding. Thank you Klaus!

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

      Glad it helped!

  • @michalsara7941
    @michalsara7941 5 ปีที่แล้ว +15

    Very cool intro into move and forward.I love the examples... Thanks Klaus!!!

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

    Thanks CppCon for uploading these fantastic videos! For most of us it might not be possible to attend it but really enjoy watching it after all. The quality is great. Lots of thanks.

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

    Thanks Cppcon for sharing and thanks Klaus for this great and good explanation (as usual) .

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

      Glad you enjoyed it

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

    Excellent presentation, really learned a lot from it. One question:
    At 15:20, "if the given value is an rvalue, cast to an rvalue reference", the function should look like
    Widget&& forward( Widget&& t) noexcept
    {
    return static_cast (t);
    }
    In other words, the function parameter shoud be "Widget&& t" instead of "Widget& t" , since non-const lvalue reference can't be initialed by an rvalue.

  • @hamol3d
    @hamol3d 3 ปีที่แล้ว +6

    Excellent presentation!

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

      Thank you kindly!

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

    Superb talk!!! This clears a lot of jargon regarding universal references and overloading rules.

  • @sky_is_the_limit_13
    @sky_is_the_limit_13 20 วันที่ผ่านมา

    @10:30 , I believe the following is the more accurate template type parameter for std::make_unique
    template< typename T, typename ... Args >
    unique_ptr make_unique( Args&&... args ){}

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

    Finally I understand move and forward, great talk 👏

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

    3 yrs and I finally get it!

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

      It's something too tricky :(

  • @AM-qx3bq
    @AM-qx3bq 4 ปีที่แล้ว +7

    I failed 5/6 examples and got the 6th right. I feel glorious.

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

    Excellent talk for an involved subject.

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

    One can only wish for someone like you as their prof in uni

  • @FilipeCruz-rj8bb
    @FilipeCruz-rj8bb ปีที่แล้ว

    I really got to learn from both presentations, nice talk!

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

    Everybody needs a Bjarne xD , thank you, this talk was awesome

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

    That evil smile at 29:00 when there was the time for const rvalue reference lol. It hit so hard.

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

    Part 1 is here: th-cam.com/video/St0MNEU5b0o/w-d-xo.html

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

    Great talk and learned a lot. Thanks Klaus!!! Paired with Effective Modern C++ from Scott Meyers will help to understand.

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

      Glad it was helpful!

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

    Well designed talk. Thank you.

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

    Great talk, thank you so much.

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

      Glad you enjoyed it!

  • @m.m.8587
    @m.m.8587 ปีที่แล้ว

    I have absolutely enjoyed both parts of the presentation! Well done, Klaus!

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

    Absolutely wonderful 👍

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

      Many thanks

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

    Excellent talk, very good examples.

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

    @21:50, excellent explanation.

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

      Glad it was helpful!

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

    this was gold

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

    Great talk !! cleaned up for me, thanks

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

    Excellent talk - thanks for this

  • @SystemSigma_
    @SystemSigma_ 3 หลายเดือนก่อน

    Great talk. Still, incredibile how far the madness of the c++ committee has gone just to copy a pointer.

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

    Just define "&&&". If it does not work then "&&&&", or &^4...

  • @speakingmia7298
    @speakingmia7298 8 หลายเดือนก่อน

    I don't understand the pitfall at 37:00 that talks about not using std::forward on the same object more than once. He said std::forward is basically a std::move which I don't understand either. Could anyone explain it?

    • @abhi-5783
      @abhi-5783 4 หลายเดือนก่อน

      Std forward returns an rvalue ref if it is passed an rvalue. So it acts similar to std move in case of an rvalue argument. Now if we call std forward twice on the same rvalue, it acts as if we call move twice on the same rvalue which is an issue.

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

    Excellent talk for && skills brush up!

  • @Florian-sh9nf
    @Florian-sh9nf ปีที่แล้ว

    I love it!

  • @SuhailKhan-vr6ik
    @SuhailKhan-vr6ik 4 ปีที่แล้ว

    Excellent talk.....thanks!

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

    If you couldn't return T&& what would std::move return?

    • @digama0
      @digama0 4 ปีที่แล้ว +7

      You can return a T& or T&&, but it should not be a reference to a local object because the object will have died by the time it gets to the caller. As long as the reference is derived from an object in the parameters to the function, it will live long enough for the caller to deal with it. In the case of T&& std::move(T& x), the returned reference is the same as the parameter so the object lives long enough to make this not a bug.
      I believe the core guideline is because this is most often a bug, with the reference derived from a local variable.

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

    Excelent talk.

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

    Thanks for great talk. Only question to slide 47 - what is the type of c-string literal ? is it "char[ ]" or "const char [ ]" ?

    • @Marius-ir1qn
      @Marius-ir1qn 4 ปีที่แล้ว +4

      c-string literal is by default const

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

      However in C, a string literal is not const.

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

    I can't understand it :(

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

      Same...

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

      🤣🤦‍♂️

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

    In Example 6 (Time 50:30), why not to use std::decay?

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

      You could, but it is overkill. std::remove_reference is enough.

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

    Love you Klaus

  • @Master-yn6ie
    @Master-yn6ie 4 ปีที่แล้ว

    nice intro

  • @DarrienGlasser
    @DarrienGlasser 4 หลายเดือนก่อน

    Me after the first video 🌆
    Me after the second video 😭

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

    anyone can give me an explantion at 10:11? why int ref is not allowed?

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

      Because make_unique() gets const& as argument.

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

      @Fish Fish
      The problem why this is not working is because of the struct Example which takes in int& in it's constructor. It goes like this:
      int i = 1;
      After calling make_unique "i" becomes [Arg const& arg => int const& arg]. Until this point everything is fine. Now you try to call "new T(arg)" which translates to: "new Example(arg)".
      "Example" wants to take a mutable value "int&" and our parameter is "int const&".
      Now you are trying to give a const variable which should not be changed into a constructor which has the possibility to change it and that is not allowed.
      Here is a more simple example which showcases this behavior:
      void foo(int& a)
      {
      a = 5;
      }
      void main()
      {
      int a = 0;
      foo(a); // that is fine
      const int b = 2;
      foo(b); // error because foo will change a and we declared b as const which means it shouldn't change.
      }

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

    we got to a point where you need a degree to assign variables

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

    Should the type of "Bjarne" at 18:12 be char[7] or const char*? I thought arrays decay to pointers when passed into functions?

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

      Const char[N] and const char* are the exact same type. Compilers may prefer the former for literals (e.g. when printing an error message) since the size is know at compile time. Try defining foo(const char*) together with foo(const char[N]). It won't compile, as you are allowed only one definition, and your types are identical and overloading is not possible.
      Another example is declaring some strfun(const char s[N]). This is allowed but it's mostly there for readability. You can call strfun("some literal") where the literal can be as long as you like. It will run all the same, because you are really calling a function that accepts a const char* with an argument of type const char*. A perfect match, no casting required. You have to make sure that the size is correct and handle wrong input yourself.

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

      @@thanksforthefish42 this is not the same type, but yes for function parameter: `foo(const char[N])` is the same as `foo(const char[])` and as ` foo(const char*)`.
      But in the case of forwarding reference:
      ```
      template
      void bar(T&& t) {}
      // ...
      bar("bjarne");
      ```
      it would instantiate this:
      `void bar(const char(&t)[7]);`
      this takes a reference to a `const char[7]` and it keeps the size information without decaying to a pointer because it is a reference to a c array
      see: godbolt.org/z/rc7b4q837

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

      Neither of them, the type of "Bjarne" is `const char[7]`

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

    ... Could be someone so kind to explain me why in all the examples starting from 31:33 he use B b_ instead of T b_ ?

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

      b__ is part of class A. I assume that B is some kind of type (a template class probably) and so b__ is instance of type B. If you wanna add variable like T b__, then you should add "template " on the top of class.