forwarding-reference

Can a forwarding reference be aliased with an alias template?

◇◆丶佛笑我妖孽 提交于 2020-03-21 20:29:39
问题 This is a continuation of my previous question: Can an identity alias template be a forwarding reference? It seems that the following code works in both Clang 3.7.0 (demo) and GCC 6.0.0 (demo): template <class T> using forwarding_reference = T&&; template <class T> void foo(forwarding_reference<T>) {} int main() { int i{}; foo(i); foo(1); } Are the compilers right to substitute the alias template for a forwarding reference and this could be a fancy way of writing one? 回答1: This is indeed

Is this a forwarding reference?

你说的曾经没有我的故事 提交于 2020-01-14 07:06:32
问题 The distinction between rvalue references and forwarding references was made clear enough in this example by Scott Meyers: Widget&& var1 = someWidget; // here, “&&” means rvalue reference (1) auto&& var2 = var1; // here, “&&” does not mean rvalue reference (2) template<typename T> void f(std::vector<T>&& param); // here, “&&” means rvalue reference (3) template<typename T> void f(T&& param); // here, “&&”does not mean rvalue reference (4) Essentially the distinction happens when we have a

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

坚强是说给别人听的谎言 提交于 2020-01-09 07:13:49
问题 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 . 回答1: 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

Function template deduction l-value reference and universal reference

独自空忆成欢 提交于 2020-01-05 04:15:06
问题 Let's say I have the function copy : template <typename Buf> void copy( Buf&& input_buffer, Buf& output_buffer) {} In which input_buffer is a universal reference and output_buffer is an l-value reference. Reference collapsing rules make sure input_buffer is indeed, regardless of the deduced type of Buf , an universal reference and output_buffer is indeed an l-value reference. However, I wonder how type Buf is deduced here. I found out that copy is passed an r-value as input_buffer , (and an l

Forward individual members of a Forward reference

拜拜、爱过 提交于 2020-01-03 18:52:50
问题 I have a function that potentially moves a generic argument but through their members. What of these options is more correct: This seems the more natural but it is strange because the argument is potentially moved twice [a], which is odd because the object can become invalid. template<class T> void fun(T&& t){ myhead_ = std::forward<T>(t).head_; myrest_ = std::forward<T>(t).rest_; } This can't be incorrect but it may not be moving anything. template<class T> void fun(T&& t){ myhead_ = std:

Forward individual members of a Forward reference

萝らか妹 提交于 2020-01-03 18:52:31
问题 I have a function that potentially moves a generic argument but through their members. What of these options is more correct: This seems the more natural but it is strange because the argument is potentially moved twice [a], which is odd because the object can become invalid. template<class T> void fun(T&& t){ myhead_ = std::forward<T>(t).head_; myrest_ = std::forward<T>(t).rest_; } This can't be incorrect but it may not be moving anything. template<class T> void fun(T&& t){ myhead_ = std:

Why a template argument of a rvalue reference type can be bound to a lvalue type?

十年热恋 提交于 2020-01-02 11:05:34
问题 As I know, a rvalue reference cannot be bound to a lvalue. e.g., void func(Foo &&f) {} int main() { Foo f; func(f); } compiler complains: error: cannot bind rvalue reference of type ‘Foo&&’ to lvalue of type ‘Foo But, why a template argument of rvalue reference type can be bound to a lvalue? e.g., template <typename T> void funcTemp(T &&arg) {} int main() { Foo f; funcTemp(f); } The compiler won't complain the error. Why? 回答1: You can read this article Universal References in C++11 to

c++: confusion about forwarding reference

寵の児 提交于 2019-12-23 06:31:07
问题 I read this (incredibly well written) article about Forwarding Reference in C++11 by Scott Meyers. Now, focus on this part of the article: template <class... Args> void emplace_back(Args&&... args); // deduced parameter types ⇒ type deduction; ... // && ≡ universal references So, in contrast with other cases, the ellipses doesn't make the && an rvalue reference, but it's still universal references. From what I've understood, when we have universal references, we can call the function passing

auto&& variable's are not rvalue reference

混江龙づ霸主 提交于 2019-12-22 09:54:27
问题 Why auto&& is not rvalue reference? Widget&& var1 = Widget(); // rvalue reference auto&& var2 = var1; //var2 not rvalue reference below are rvalue reference example void f(Widget&& param); // rvalue reference Widget&& var1 = Widget(); // rvalue reference Why var2 is not rvalue reference but f and var2 are rvalue references? 回答1: Once the type of the initializer has been determined, the compiler determines the type that will replace the keyword auto using the rules for template argument