forwarding-reference

What's the standard/official name for universal references?

╄→гoц情女王★ 提交于 2019-11-27 20:31:10
I know that if a variable or parameter is declared to have type T&& for some deduced type T , that variable or parameter is widely called a universal reference . The term universal reference was introduced by Scott Meyers in his original talk “Universal References in C++11” . However, I wonder what's the official/standard term for universal references . Overview It is known that since C++11, a parameter of type T&& is called an rvalue reference [ ISO/IEC 14882:2011 §8.3.2/p2 References [dcl.ref] ]. That is, unless T is a template parameter type or auto or a typedef for some lvalue reference

Why does std::move take a forward reference?

我与影子孤独终老i 提交于 2019-11-27 18:05:47
问题 The implementation of std::move basically looks like this: template<typename T> typename std::remove_reference<T>::type&& move(T&& t) { return static_cast<typename std::remove_reference<T>::type&&>(t); } Note that the parameter of std::move is a universal reference (also known as a forwarding reference, but we're not forwarding here). That is, you can std::move both lvalues and rvalues: std::string a, b, c; // ... foo(std::move(a)); // fine, a is an lvalue foo(std::move(b + c)); // nonsense,

Does an lvalue argument prefer an lvalue reference parameter over a universal reference?

爱⌒轻易说出口 提交于 2019-11-27 03:12:08
问题 While playing with universal references, I came across this instance where clang and gcc disagree on overload resolution. #include <iostream> struct foo {}; template<typename T> void bar(T&) { std::cout << "void bar(T&)\n"; } template<typename T> void bar(T&&) { std::cout << "void bar(T&&)\n"; } int main() { foo f; bar(f); // ambiguous on gcc, ok on clang } gcc reports the call above is ambiguous. However, clang selects the T& overload and compiles successfully. Which compiler is wrong, and

What does `auto && e` do in range-based for-loops?

夙愿已清 提交于 2019-11-27 01:16:23
问题 Assuming my current rule when programming with range-based loops says Use for(auto const &e :...) or for(auto &e:...) when possible over for(auto a: ...) . I base this on my own experience and this question for example. But after reading about the new terse for loops I wonder, should I not replace my & in my rule with && ? As written here this looks like the Meyers' Universal References. So, I ask myself, should my new rule either be Use for(auto const &&e :...) or for(auto &&e:...) when

Is there a difference between universal references and forwarding references?

孤者浪人 提交于 2019-11-26 16:30:50
问题 An argument to this function will bind to an rvalue reference: void f(int && i); However, an argument to this function will bind to either an rvalue or an lvalue reference: template <typename T> void f(T && t); I've often heard this referred to as a universal reference. I've also heard it been called a forwarding reference. Do they mean the same thing? Is it only a forwarding reference if the function body calls std::forward ? 回答1: Do they mean the same thing? Universal reference was a term

Syntax for universal references

谁说我不能喝 提交于 2019-11-26 08:51:42
问题 This is an rvalue reference: void foo(int&& a); It does not bind to lvalues: int i = 42; foo(i); // error This is a universal reference: template<typename T> void bar(T&& b); It binds to rvalues and it also binds to lvalues: bar(i); // okay This is an rvalue reference: template<typename T> struct X { void baz(T&& c); }; It does not bind to lvalues: X<int> x; x.baz(i); // error Why do universal references use the same syntax as rvalue references? Isn\'t that an unnecessary source of confusion?

What does auto&& tell us?

◇◆丶佛笑我妖孽 提交于 2019-11-26 04:02:25
问题 If you read code like auto&& var = foo(); where foo is any function returning by value of type T . Then var is an lvalue of type rvalue reference to T . But what does this imply for var ? Does it mean, we are allowed to steal the resources of var ? Are there any reasonable situations when you should use auto&& to tell the reader of your code something like you do when you return a unique_ptr<> to tell that you have exclusive ownership? And what about for example T&& when T is of class type? I