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
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.
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.
Excellent explanation of std::forward and perfect forwarding. Thank you Klaus!
@CppCon
3 жыл бұрын
Glad it helped!
Very cool intro into move and forward.I love the examples... Thanks Klaus!!!
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.
I failed 5/6 examples and got the 6th right. I feel glorious.
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.
Everybody needs a Bjarne xD , thank you, this talk was awesome
Finally I understand move and forward, great talk 👏
One can only wish for someone like you as their prof in uni
Excellent presentation!
@CppCon
3 жыл бұрын
Thank you kindly!
Superb talk!!! This clears a lot of jargon regarding universal references and overloading rules.
Excellent talk for an involved subject.
Excellent talk, very good examples.
I really got to learn from both presentations, nice talk!
Excellent talk - thanks for this
Well designed talk. Thank you.
Thanks Cppcon for sharing and thanks Klaus for this great and good explanation (as usual) .
@CppCon
Жыл бұрын
Glad you enjoyed it
Excellent talk for && skills brush up!
3 yrs and I finally get it!
@grigoriyz
3 жыл бұрын
It's something too tricky :(
Excellent talk.....thanks!
Great talk !! cleaned up for me, thanks
Absolutely wonderful 👍
@CppCon
2 жыл бұрын
Many thanks
Great talk, thank you so much.
@CppCon
2 жыл бұрын
Glad you enjoyed it!
Excelent talk.
this was gold
That evil smile at 29:00 when there was the time for const rvalue reference lol. It hit so hard.
@21:50, excellent explanation.
@CppCon
2 жыл бұрын
Glad it was helpful!
Great talk and learned a lot. Thanks Klaus!!! Paired with Effective Modern C++ from Scott Meyers will help to understand.
@CppCon
2 жыл бұрын
Glad it was helpful!
I love it!
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
4 жыл бұрын
c-string literal is by default const
@objectobject6172
3 жыл бұрын
However in C, a string literal is not const.
I have absolutely enjoyed both parts of the presentation! Well done, Klaus!
nice intro
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
Ай бұрын
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.
If you couldn't return T&& what would std::move return?
@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.
In Example 6 (Time 50:30), why not to use std::decay?
@peterkochlarsen2792
4 жыл бұрын
You could, but it is overkill. std::remove_reference is enough.
Part 1 is here: kzread.info/dash/bejne/hahkr7B-hZjIYNI.html
Just define "&&&". If it does not work then "&&&&", or &^4...
Love you Klaus
anyone can give me an explantion at 10:11? why int ref is not allowed?
@incrediblesaint5020
2 жыл бұрын
Because make_unique() gets const& as argument.
@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. }
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
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
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
2 жыл бұрын
Neither of them, the type of "Bjarne" is `const char[7]`
Me after the first video 🌆 Me after the second video 😭
I can't understand it :(
@NickDrian
Жыл бұрын
Same...
@_Omni
Жыл бұрын
🤣🤦♂️
... 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
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.
we got to a point where you need a degree to assign variables