rvalue-reference

C++ universal reference in constructor and return value optimization (rvo)

拜拜、爱过 提交于 2019-12-19 16:06:13
问题 Why does rvalue optimization not occur in classes with constructor with universal reference arguments? http://coliru.stacked-crooked.com/a/672f10c129fe29a0 #include <iostream> template<class ...ArgsIn> struct C { template<class ...Args> C(Args&& ... args) {std::cout << "Ctr\n";} // rvo occurs without && ~C(){std::cout << "Dstr\n";} }; template<class ...Args> auto f(Args ... args) { int i = 1; return C<>(i, i, i); } int main() { auto obj = f(); } Output: Ctr Ctr Dstr Ctr Dstr Dstr 回答1: I

C++11 templated function with rvalue param call

时光总嘲笑我的痴心妄想 提交于 2019-12-19 12:24:26
问题 In some class O, I have templated function test2 : struct A{int value;}; struct O{ A value; template<typename Args> static void test2(Args &&args){ std::cout << std::endl << "!!!" << std::is_rvalue_reference<decltype(args)>::value << std::endl; } }; Than, I want to call this function from another one: template<typename Args> void test(Args &&args){ using t = decltype(std::forward<Args>(args).value); std::cout << std::is_rvalue_reference<decltype(args)>::value; std::cout << std::is_rvalue

C++11 templated function with rvalue param call

拥有回忆 提交于 2019-12-19 12:23:04
问题 In some class O, I have templated function test2 : struct A{int value;}; struct O{ A value; template<typename Args> static void test2(Args &&args){ std::cout << std::endl << "!!!" << std::is_rvalue_reference<decltype(args)>::value << std::endl; } }; Than, I want to call this function from another one: template<typename Args> void test(Args &&args){ using t = decltype(std::forward<Args>(args).value); std::cout << std::is_rvalue_reference<decltype(args)>::value; std::cout << std::is_rvalue

C++11: Why rvalue reference parameter implicitly converted to lvalue

血红的双手。 提交于 2019-12-19 11:20:48
问题 Following is the simplistic code of my problem, void overloaded (int&& x) { cout << "[rvalue]"; } template <class T> void fn (T&& x) { overloaded(x); } int main() { fn(0); return 0; } I got a compile error cannot bind ‘ int ’ lvalue to ‘ int&& ’ overloaded(x); I am confused here, x is passed as a rvalue reference into fn() . But why the overload() call in fn() complains x is a lvalue? 回答1: One, the x argument to fn isn't an r-value reference, it's a "universal reference" (yes, this is rather

When will adding a move constructor and a move assignment operator really start make a difference?

让人想犯罪 __ 提交于 2019-12-19 09:09:34
问题 Considering the high quality of today's compilers regarding return value optimization (both RVO and NRVO), I was wondering at what class complexity it's actually meaningful to start adding move constructors and move assignment operators. For instance, for this really_trivial class, I just assume that move semantics cannot offer anything more than RVO and NRVO already does when copying around instances of it: class really_trivial { int first_; int second_; public: really_trivial(); ... };

When will adding a move constructor and a move assignment operator really start make a difference?

痴心易碎 提交于 2019-12-19 09:09:09
问题 Considering the high quality of today's compilers regarding return value optimization (both RVO and NRVO), I was wondering at what class complexity it's actually meaningful to start adding move constructors and move assignment operators. For instance, for this really_trivial class, I just assume that move semantics cannot offer anything more than RVO and NRVO already does when copying around instances of it: class really_trivial { int first_; int second_; public: really_trivial(); ... };

Why an Rvalue Reference is Turned into Lvalue Reference by a Universal Reference

橙三吉。 提交于 2019-12-19 08:53:22
问题 I suppose when a universal reference parameter is matched with an rvalue reference argument, an rvalue reference argument is returned. However, my testing shows that the rvalue reference is turned into a lvalue reference by the universal reference function template. Why is it so? #include <iostream> #include <type_traits> using namespace std; template <typename T> T f1(T&&t) { //<-----this is a universal reference cout << "is_lvalue reference:" << is_lvalue_reference<T>::value << endl; cout <

Can't bind lvalue to rvalue reference

匆匆过客 提交于 2019-12-19 06:40:10
问题 I have this C++ test code snippet, #include <vector> class A { std::vector<int> x; public: A(std::vector<int>&& _x) : x(_x) {} }; class B { A a; public: B(std::vector<int>&& _x) : a(/*move(*/_x/*)*/) {} }; I'm passing _x to B as rvalue reference, but it's getting converted to lvalue when passed into A 's constructor and I have to use std::move() to make it work. My question is why _x is lvalue and not an rvalue reference in a() ? 回答1: Quote from WIKI For safety reasons, some restrictions are

Move from *this in an rvalue method?

[亡魂溺海] 提交于 2019-12-19 05:35:15
问题 In C++11, methods can be overloaded on whether or not the expression that denotes the object on which the method is called is an lvalue or an rvalue. If I return *this from a method called via an rvalue, do I need to explicitly move from *this or not? Foo Foo::method() && { return std::move(*this); // Is this move required or not? } Unfortunately, I can't simply test this on my compiler since g++ does not support this feature yet :( 回答1: The type of *this is always an lvalue: §9.3.2 [class

Move from *this in an rvalue method?

[亡魂溺海] 提交于 2019-12-19 05:35:03
问题 In C++11, methods can be overloaded on whether or not the expression that denotes the object on which the method is called is an lvalue or an rvalue. If I return *this from a method called via an rvalue, do I need to explicitly move from *this or not? Foo Foo::method() && { return std::move(*this); // Is this move required or not? } Unfortunately, I can't simply test this on my compiler since g++ does not support this feature yet :( 回答1: The type of *this is always an lvalue: §9.3.2 [class