Back to Basics: Move Semantics - Nicolai Josuttis - CppCon 2021
Вставка
- Опубліковано 9 лип 2024
- cppcon.org/
github.com/CppCon/CppCon2021
---
Move semantics, introduced with C++11, has become a hallmark of modern C++ programming. However, it also complicates the language in some ways. For thisu reason it is key to understand move semantics (it purpose, its basic rules, and how to use them in practice).
This session teaches the basics of C++ move semantics. Based on the basic principles, it motivates and explains move semantics so that you as an application programmer you can use move semantics correctly und understand its use better.
---
Nicolai Josuttis
Nicolai Josuttis (www.josuttis.com) is well known in the programming community because he not only speaks and writes with authority (being the (co-)author of the world-wide best sellers The C++ Standard Library (www.cppstdlib.com), C++ Templates (www.tmplbook.com), C++17 - The Complete Guide (www.cppstd17.com), C++ Move Semantics - The Complete Guide (www.cppmove.com), and SOA in Practice), but is also an innovative presenter, having talked at various conferences and events.
He has been an active member of the C++ standards committee for more than 20 years.
---
Videos Streamed & Edited by Digital Medium: online.digital-medium.co.uk
*--* - Наука та технологія
A thoroughly excellent talk, this is by far the clearest and least confused explanation of C++ move semantics I have encountered. Bravo!
Yes, after so many references this is the first material which gave me a clear idea what is actually happening. Like more videos from the C++ committee.
wow. After struggling to understand move semantics for months...I finally get it. Thank you CPPCON & Nicolai Josuttis!!
Thank you for making these videos, Mr Josuttis.
Very clear, concise and meticulous talk, much appreciated.
Thank you for the talk! I am learning C++ as a novice and really enjoying these sessions!
Very clear, with specific examples to explain the context and the value of the concept. 5/5!
Quite a complete summary that exposes most if not all the move semantic intricacies.
Thank you so much for these amazing videos. I've borrowed Nicolai's book "The c++ Standard Library", You are so inspirational and give great examples, thank you Nicolai.
I wore the spine out of his 1st edition of "The C++ Standard Library". Thanks so much for that book, it very much helped me learn how to use the C++ standard library!
Brilliantly explained. Thank you Mr Josuttis.
The best talk ever on this topic.
Thank you for explaining why std::forward exists, it makes it easy to understand what it does and when you have to use it.
Thank you so much for deeply understanding of the move semantic and universal forwarding.
This is a perfect explanation what the move semantics is.
Well explained! Thx a lot!
Great example! Thanks to this video, now I have better understanding on move semantics.
Excellent!
Thank you Nicolai for such informative and detailed explanation of move semantics.
It would be interesting to read your ne books as well. ;)
Excellent, clear talk and the best on the subject. thank you!
Glad you enjoyed it!
Thank you for this talk.
Awesome 👏 ❤Thank you so much for sharing ❤
Brilliant explanation.
The last part about universal reference was very useful.
best explanation on move semantics
Excellent talk
the best move semantics out there.
good one, got the basics thanks
Really informative
Great Talk!!!!!
just amazing
great talk as always
Thank you very much for your comment.
55:45 , 57:00
In C++ 20 you will use this a lot.
Is there an actual compelling example?
I haven’t seen much that using `auto&& c = thing()`
Buys you over using `auto c = thing()` and then moving later instead of forwarding.
One less move constructor call, and if it’s a copy only type will be one less copy, which is obviously good, however the trade off is much messier syntax.
I would personally prefer the simpler, easier to read version with an extra possible copy and optimize later if it’s a performance bottle neck.
But maybe I’m missing something?
very nicely explained, thanks.
You are welcome!
At 47:00 can anyone explain why foo(std::move(x)); will call the wrong function?
Thank you for the nice talk! "Back to Basics" to the bone.
Why do destructors in derived classes destroy move semantics?
Doesn't the c++ compiler elide the destructor at 7:12? It seems as though it should.
Dear @CppCon, I would love to get the slide deck of this talk. Is sharing it a possibility?
Nice 👌
At 53:24 why would calling std::move(first) result in a _move_? The constructor has a const std::string& for the first parameter. So shouldn't it be a copy?
One point, on Page 22, string move semantics, string move constructor, dose s.data assigns to nullptr? I did some tests, data still is valid address. Pls correct me if I am wrong.
53:26 I think "Jil" from the variable first will not be moved as the constructor of Cust takes const std::string& as parameter
Good video. Give me motivation to see whats on C++20.
@@squarerootof2 @talluay3567 is right. Cust constructor is called in the emplace_back() by calling the placement new operator
11:52, instead of using `std::move()`, I think it's better to use `std::exchange()`, like:
history.push_back(std::exchange(s, getDefaultValue()));
That isn't only one-line implementation of `reinit()`, it is also efficient, as `std::exchange` moves the first parameter as well.
Question:
It's not so nice having this T&& or auto&& for a universal reference since the type is vague.
Would it be possible to confine the type with concepts / requires T is MyClass somehow?
Also is there any plan to 'fix' forwarding in C++23?
Sure, it's easy using concepts:
template
concept ref_to = std::same_as;
Then use it like:
void f(ref_to auto&& p) { /*...*/ }
This takes lvalue references and rvalue references, but only to MyClass.
@@FruchteisMitErdbeer It doesn't really look easy to read though
my life would be better if i knew less c++
🖖
how to get slide?
Sorry, the slides are presently unavailable. We are looking to rectify this in the future.
21:15
This is wrong.
T&& where T is a template type isn’t a rvalue reference.
Outside the context of a templates type this would be true, but, this accepts both r and l values.
Would it not only be a universal reference, if the template was defined for the function only, not the complete class?
"some" views not supporting iteration while const is a very nasty behaviour. More so since it is non-deterministic (at least at glance). Couldn't it be solved with mutable keyword? Recommending using auto&& so stuff works doesn't seem like the best idea as it is the first step to sacrifice const correctness.. :(
Otherwise - great talk! The more I delve into the move semantics the more they seem unfinished. The "valid but unspecified state" effectively means that unless your compiler supports use-after-move detection then you shouldn't ever move a named variable or else you risk shooting yourself in the foot. If that detection was default, then the compiler would move a lot of stuff automatically. This way, you're forced to optimize by hand a risk a bug.
You are ambiguous in crucial points, Nicolai. 11:30 you talk on this so much but you never explained what does _here_ mean. You repeat the word "move", that it doesn't _move_ anything, that it is a mark, etcetc, but you do not refer to the thing that matters: the _ownership_