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

Ғылым және технология

CppCon.org
Discussion & Comments: / cpp
Presentation Slides, PDFs, Source Code and other presenter materials are available at: github.com/CppCon/CppCon2019
-
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/blaze-lib/blaze...) and the organizer of the Munich C++ user group (www.meetup.com/MUCplusplus/).
-
Videos Filmed & Edited by Bash Films: www.BashFilms.com
*-----*
Register Now For CppCon 2022: cppcon.org/registration/
*-----*

Пікірлер: 66

  • @think2086
    @think20862 жыл бұрын

    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
    @PraveenKulkarni19963 жыл бұрын

    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
    @erik77263 жыл бұрын

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

  • @CppCon

    @CppCon

    3 жыл бұрын

    Glad it helped!

  • @michalsara7941
    @michalsara79414 жыл бұрын

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

  • @MightyMoveSemantics
    @MightyMoveSemantics8 ай бұрын

    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.

  • @AM-qx3bq
    @AM-qx3bq4 жыл бұрын

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

  • @mzd8888
    @mzd88883 жыл бұрын

    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.

  • @mahdies9620
    @mahdies96202 жыл бұрын

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

  • @_Omni
    @_Omni2 жыл бұрын

    Finally I understand move and forward, great talk 👏

  • @zeez7777
    @zeez77772 ай бұрын

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

  • @hamol3d
    @hamol3d3 жыл бұрын

    Excellent presentation!

  • @CppCon

    @CppCon

    3 жыл бұрын

    Thank you kindly!

  • @MyMjrox
    @MyMjrox Жыл бұрын

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

  • @robertfreeman2792
    @robertfreeman27922 жыл бұрын

    Excellent talk for an involved subject.

  • @andreytimokhin936
    @andreytimokhin9363 жыл бұрын

    Excellent talk, very good examples.

  • @FilipeCruz-rj8bb
    @FilipeCruz-rj8bb Жыл бұрын

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

  • @andrewschuessler2482
    @andrewschuessler24823 жыл бұрын

    Excellent talk - thanks for this

  • @oguzmete8414
    @oguzmete841411 ай бұрын

    Well designed talk. Thank you.

  • @CppZiedAlaya
    @CppZiedAlaya Жыл бұрын

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

  • @CppCon

    @CppCon

    Жыл бұрын

    Glad you enjoyed it

  • @mikedessenov3475
    @mikedessenov34754 жыл бұрын

    Excellent talk for && skills brush up!

  • @viraatchandra8498
    @viraatchandra84983 жыл бұрын

    3 yrs and I finally get it!

  • @grigoriyz

    @grigoriyz

    3 жыл бұрын

    It's something too tricky :(

  • @SuhailKhan-vr6ik
    @SuhailKhan-vr6ik4 жыл бұрын

    Excellent talk.....thanks!

  • @marcospatton
    @marcospatton2 жыл бұрын

    Great talk !! cleaned up for me, thanks

  • @vibhatha
    @vibhatha2 жыл бұрын

    Absolutely wonderful 👍

  • @CppCon

    @CppCon

    2 жыл бұрын

    Many thanks

  • @Raeprim
    @Raeprim2 жыл бұрын

    Great talk, thank you so much.

  • @CppCon

    @CppCon

    2 жыл бұрын

    Glad you enjoyed it!

  • @aibou2399
    @aibou23994 жыл бұрын

    Excelent talk.

  • @hakoo2700
    @hakoo27002 жыл бұрын

    this was gold

  • @bitterthread6794
    @bitterthread679411 ай бұрын

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

  • @liveonphoenix5045
    @liveonphoenix50452 жыл бұрын

    @21:50, excellent explanation.

  • @CppCon

    @CppCon

    2 жыл бұрын

    Glad it was helpful!

  • @haoliang3480
    @haoliang34802 жыл бұрын

    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!

  • @Florian-sh9nf
    @Florian-sh9nf Жыл бұрын

    I love it!

  • @rafalmichalski4893
    @rafalmichalski48934 жыл бұрын

    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 жыл бұрын

    c-string literal is by default const

  • @objectobject6172

    @objectobject6172

    3 жыл бұрын

    However in C, a string literal is not const.

  • @m.m.8587
    @m.m.8587 Жыл бұрын

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

  • @Master-yn6ie
    @Master-yn6ie4 жыл бұрын

    nice intro

  • @speakingmia7298
    @speakingmia72985 ай бұрын

    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

    Ай бұрын

    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.

  • @alkis2407
    @alkis24074 жыл бұрын

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

  • @digama0

    @digama0

    4 жыл бұрын

    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.

  • @serhiymalokhatko
    @serhiymalokhatko4 жыл бұрын

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

  • @peterkochlarsen2792

    @peterkochlarsen2792

    4 жыл бұрын

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

  • @Quuxplusone
    @Quuxplusone4 жыл бұрын

    Part 1 is here: kzread.info/dash/bejne/hahkr7B-hZjIYNI.html

  • @CookiePepper
    @CookiePepper2 жыл бұрын

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

  • @adasz8369
    @adasz83694 жыл бұрын

    Love you Klaus

  • @rustcpp
    @rustcpp2 жыл бұрын

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

  • @chrisshyi8999
    @chrisshyi89994 жыл бұрын

    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

    3 жыл бұрын

    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

    2 жыл бұрын

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

    2 жыл бұрын

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

  • @DarrienGlasser
    @DarrienGlasserАй бұрын

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

  • @grigoriyz
    @grigoriyz3 жыл бұрын

    I can't understand it :(

  • @NickDrian

    @NickDrian

    Жыл бұрын

    Same...

  • @_Omni

    @_Omni

    Жыл бұрын

    🤣🤦‍♂️

  • @cqxx5285
    @cqxx52854 жыл бұрын

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

  • @luvincste
    @luvincste11 ай бұрын

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

Келесі