MENU

Fun & Interesting

Why use forwarding references and how they are different from rvalue references in C++

Code for yourself 3,789 11 months ago
Video Not Working? Fix It Now

*Help me keep doing these videos!* This works relies on *your* support! You can show it in one of these ways: 📚 Start a FREE Audible trial: https://www.audibletrial.com/CodeForYourself 🛍️ Buy gear I use to make this video: https://github.com/cpp-for-yourself/sponsor/blob/main/amazon.md 💶 Directly become a sponsor on GitHub: https://github.com/sponsors/niosus 📺 Watch my videos *to the end* and leave comments ⁉️ Find *your* way to support this work here: https://github.com/cpp-for-yourself/sponsor/tree/main Since C++11 was released there has always been quite some interest in forwarding references among people practicing C++. On one hand, it is a very powerful mechanism, on the other, it requires quite some knowledge about how templates work so it is met with a lot of fear among people learning C++. In this video, we cover everything there is to know about forwarding references, why we might want to use them and how they work. This includes talking about reference collapsing and std::forward as the mechanisms that enable forwarding references to do what they are supposed to do. 📚 As always, you can find the full script to this video as well as all the code shown on the screen here: https://github.com/cpp-for-yourself/supplementary-materials/blob/main/lectures/forwarding_references.md 🔗 *Links* ▪️ Forwarding references: https://en.cppreference.com/w/cpp/language/reference#Forwarding_references ▪️ Universal references by Scott Meyers: https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers ▪️ Arthur O'Dwyer's post about forwarding references: https://quuxplusone.github.io/blog/2022/02/02/look-what-they-need/ ▪️ Reference collapsing: https://en.cppreference.com/w/cpp/language/reference#Reference_collapsing ▪️ std::is_same: https://en.cppreference.com/w/cpp/types/is_same ▪️ std::remove_reference_t: https://en.cppreference.com/w/cpp/types/remove_reference ▪️ Example code for forward: https://cppinsights.io/s/e48f0dba 00:00 - Start 01:27 - Why use forwarding references 08:35 - How forward references work 14:03 - Summary Video of "black magic" by Kampus Production: https://www.pexels.com/video/girl-preparing-a-magic-potion-7965019/ Photo of Scott Meyers by Eugene Zelenko - Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=27460170

Comment