C++ and Beyond 2012: Scott Meyers - Universal References in C++11
Why Stack Overflow recommends it
Stack Overflow users link it when explaining forwarding references, reference collapsing and std::forward, and as the original source of the term "universal reference".
Recommended by 3 people across 4 different months, 2014 to 2024.
What it is
A full-length session recording of Scott Meyers presenting "Universal References in C++11" at C++ and Beyond 2012. He explains how T&& behaves in deduced contexts, covering type deduction, reference collapsing and perfect forwarding.
What people said
Recommends arkan · 2022-09 · Stack Overflow · on “What are the reference collapsing rules, and how are they utilized by the C++ standard library?”
[this video] the top comment msdn link is dead unfortunately. Title: "C++ and Beyond 2012: Scott Meyers - Universal References in C++11"
Recommends arkan · 2022-10 · Stack Overflow · on “How does std::forward work, especially when passing lvalue/rvalue references?”
At 34:06 Scott Meyers explains how type and value are two distinct things [this video] (Title: "C++ and Beyond 2012: Scott Meyers - Universal References in C++11")
Recommends Larry · 2024-01 · Stack Overflow · on “Why can't I use universal reference in a way like `void(*func)(T&&)` and `for (T&& v : vec)`?”
For users not familiar with this stuff, Google for the old term "Universal References" coined by "Scott Meyers". See his article here [LINK] video here [this video] and an absolute must read for all C++ developers (goes a long way to explain some issues I've never seen anywhere else!!), Chapter 1 ("Deducing Types") and Chapter 5 ("Rvalue References ...") in his book "Effective Modern C++: 42 Specific Ways to Improve ...". Too bad he retired from C++ a number of years ago now (everyone's loss).
Recommends ttzytt · 2024-07 · Stack Overflow · on “Forwarding of return values. Is std::forward is needed?”
Did some tests myself, and the following are the results: template <class T> T&& wrapper_without_forwarding(T&& t) { return t; } This compiles for these scenarios: int&& ri = 123; int& li = ri; wrapper_without_forwarding(li); wrapper_without_forwarding(ri); But if I directly pass in a number like this: wrapper_without_forwarding(123); Compilation fails with the following messages: error: cannot bind rvalue reference of type ‘int&&’ to lvalue of type ‘int’ 28 | return t; | ^ The error messages might be confusing, but personally, I think it is equivalent to this situation: int&& test(){ int&& rv = 123; return rv; // compilation error return 123; // correct } Basically, the type of rv is indeed
Cites Felix Glas · 2014-10 · Stack Overflow · on “How to bind function to an object by reference?”
Note on using std::forward First of all, std::forward is meant to be used for perfect forwarding [LINK] , i.e. to forward the reference type (l-value or r-value). If you pass an l-value reference to std::forward that is what is returned, and likewise if an r-value reference is passed then an r-value is returned. This works as opposed to std::move that will always return an r-value reference. Also remember that named r-value references are l-value references. /* Function that takes r-value reference. */ void func(my_type&& t) { // 't' is named and thus is an l-value reference here. // Pass 't' as lvalue reference. some_other_func(t); // Pass 't' as rvalue reference (forwards rvalueness). some
Cites Miles Budnek · 2024-01 · Stack Overflow · on “Why can't I use universal reference in a way like `void(*func)(T&&)` and `for (T&& v : vec)`?”
Forwarding references only arise when applied directly to a deduced template parameter. That isn't the case with your function pointer. Forwarding references aren't a "real" type. They're a trick of the way template type deduction works. The way forwarding references work is by deducing different types depending on the value category of the expression passed to them. That is, given a function like: template <typename T> void func(T&& arg); When you call func(some_lvalue) , where some_lvalue is an int , T gets deduced as int& . That leaves func 's parameter as an int& && . Since you can't have references to references, the reference collapsing rules are applied, and the type of arg collapses