rvalue-reference

Workarounds for no 'rvalue references to *this' feature

蹲街弑〆低调 提交于 2019-12-31 08:11:30
问题 I have a proxy container class around a movable object, and wish the proxy to be able to implicitly yield an rvalue reference to the underlying object, but only when the proxy itself is being moved. I believe that I will be able to implement this behaviour as per proposal n2439 "Extending move semantics to *this", but it is not yet available in a release of gcc and won't be for a while. The code below is what I am ultimately aiming for, but is not currently possible. Until this feature is

Shared memory and copy on write or rvalue references and move semantics?

梦想的初衷 提交于 2019-12-30 17:51:10
问题 Is a shared memory/copy on write implementation for general containers (like that found in Qt's containers) superseded by C++11 move semantics and rvalue references? Where does one fail and the other succeed? Or are they complementary rather than alternatives? 回答1: Both copy on write and move semantics have been used to optimize value semantics of objects that hold their data on the heap. std::string , for example has been implemented both as a copy-on-write object, and as a move-enabled

Is an xvalue's lifetime extended when it is bound to a const lvalue reference?

旧时模样 提交于 2019-12-30 08:25:10
问题 If I write the following code: #include <iostream> using namespace std; int main() { cout << &(int &&)123 << endl; return 0; } Then g++ complains: foo.cc: In function ‘int main()’: foo.cc:7:20: error: taking address of xvalue (rvalue reference) Ok, thanks to What are rvalues, lvalues, xvalues, glvalues, and prvalues? I get that an xvalue means that it's about to "expire", which makes sense. But now if I do this: #include <iostream> using namespace std; int main() { const int &x = (int &&)123;

How to reduce redundant code when adding new c++0x rvalue reference operator overloads

南楼画角 提交于 2019-12-28 10:05:04
问题 I am adding new operator overloads to take advantage of c++0x rvalue references, and I feel like I'm producing a lot of redundant code. I have a class, tree , that holds a tree of algebraic operations on double values. Here is an example use case: tree x = 1.23; tree y = 8.19; tree z = (x + y)/67.31 - 3.15*y; ... std::cout << z; // prints "(1.23 + 8.19)/67.31 - 3.15*8.19" For each binary operation (like plus), each side can be either an lvalue tree , rvalue tree , or double . This results in

How to make template rvalue reference parameter ONLY bind to rvalue reference?

情到浓时终转凉″ 提交于 2019-12-28 02:01:27
问题 I'm writing a network library and use move semantics heavily to handle ownership for file descriptors. One of my class wishes to receive file descriptor wrappers of other kinds and take ownership, so it's something like struct OwnershipReceiver { template <typename T> void receive_ownership(T&& t) { // taking file descriptor of t, and clear t } }; It has to deal multiple unrelated types so receive_ownership has to be a template, and to be safe, I wish it ONLY binds to rvalue references, so

C++11 rvalue object field

陌路散爱 提交于 2019-12-25 07:10:02
问题 Can I have class/struct with rvalue field in c++11? Like this one: template<typename T> struct RvalueTest{ RvalueTest(T&& value) : value( std::forward<T>(value) ){} T&& value; }; Because in the following test: class Widget { public: Widget(){std::cout << "Widget ctor " << std::endl; } Widget(int h) : h(h){std::cout << "Widget ctor param " << std::endl; } Widget(const Widget&) { std::cout << "Widget copy ctor " << std::endl; } Widget(Widget&&) { std::cout << "Widget move ctor " << std::endl; }

Forward or Move

好久不见. 提交于 2019-12-25 04:14:54
问题 Are these valid usage of move and forward? Are f3 and f4 the same? Is it dangerous to do so? Thank you! #include <utility> class A {}; A f1() { A a; return a; // Move constructor is called } A f2(A&& a) { return a; // Copy constructor is called, which is what I try to avoid. } A f3(A&& a) { return std::forward<A&&>(a); // Move constructor is called } A f4(A&& a) { return std::move(a); // Move constructor is called } 回答1: Use std::forward with a universal reference , i.e. a template <typename

rvalue reference in class template vs function template

巧了我就是萌 提交于 2019-12-24 08:29:14
问题 #include <iostream> template <typename T> class test { public: test(T&& t) { } }; template <typename T> void succeed(T&& t) { } int main() { int i = 1; test<int> t(i); // failed to compile succeed(i); // OK return 0; } Error from GCC 5.2: main.cpp: In function 'int main()': main.cpp:20:18: error: cannot bind 'int' lvalue to 'int&&' test t(i); ^ main.cpp:7:5: note: initializing argument 1 of 'test::test(T&&) [with T = int]' test(T&& t) ^~~~ Could someone explain why the class template cannot

Why don't xvalues bind to non-const lvalue references?

。_饼干妹妹 提交于 2019-12-24 00:35:22
问题 The following does not compile: #include <iostream> using namespace std; int x = 5; int && f () { return std::move(x); } int g(int & y) { return y; } int main() { g(f()); return 0; } It's clear to me why prvalues (unnamed temporaries) do not bind to non-const lvalue references -- it does not make sense to modify them, as they will soon disappear. Yet why do xvalues not bind to non-const lvalue references? If a function returns int && , the referenced object can't be temporary, otherwise we

Return “this” as rvalue

人盡茶涼 提交于 2019-12-23 10:07:11
问题 The following code does, as expected, not compile #include <iostream> class A { public: A() = default; ~A() = default; A(const A&) = delete; A(A&&) = delete; A& operator=(const A&) = delete; A& operator=(A&&) = delete; A& operator<<(const int i) { std::cout << "operator<< called" << std::endl; return *this; } }; void foo(A&& a) { std::cout << "foo called" << std::endl; } int main() { A a; a << 14; foo(std::move(a)); // works fine foo(A() << 14); // does not compile return 0; } Changing the