rvalue-reference

Should implicitly generated assignment operators be & ref-qualified?

北慕城南 提交于 2019-11-27 22:23:19
The following code compiles without problem on gcc 4.8.1: #include <utility> struct foo { }; int main() { foo bar; foo() = bar; foo() = std::move( bar ); } It seems the implicitly generated assignment operators for foo are not & ref-qualified and so can be invoked on rvalues. Is this correct according to the standard? If so, what reason is there for not requiring implicitly generated assignment operators to be & ref-qualified? Why doesn't the standard require the following to be generated? struct foo { foo & operator=( foo const & ) &; foo & operator=( foo && ) &; }; Well, there are certain

Extending temporary's lifetime through rvalue data-member works with aggregate, but not with constructor, why?

荒凉一梦 提交于 2019-11-27 22:21:07
I've found the following scheme to extend a temporaries lifetime works, I don't know if it should, but it does. struct S { std::vector<int>&& vec; }; int main() { S s1{std::vector<int>(5)}; // construct with temporary std::cout << s1.vec[0] << '\n'; // fine, temporary is alive } However, when S is given an explicit value constructor it is no longer an aggregate, and this scheme fails with an invalid read on s1.vec[0] struct S { std::vector<int>&& vec; S(std::vector<int>&& v) : vec{std::move(v)} // bind to the temporary provided { } }; int main() { S s1{std::vector<int>(5)}; // construct with

Are value parameters implicitly moved when returned by value?

谁都会走 提交于 2019-11-27 21:34:23
问题 Consider the following function: Foo foo(Foo x) { return x; } Will return x invoke the copy constructor or the move constructor? (Let's leave NRVO aside here.) To investigate, I wrote a simple Foo class that is only movable but not copyable: struct Foo { Foo() = default; Foo(const Foo&) = delete; Foo(Foo&&) = default; }; If the move constructor were invoked when returning value parameters by value, all should be fine. But the current g++ compiler complains about return x with the following

Do C++11 compilers turn local variables into rvalues when they can during code optimization?

白昼怎懂夜的黑 提交于 2019-11-27 21:10:29
Sometimes it's wise to split complicated or long expressions into multiple steps, for example (the 2nd version isn't more clear, but it's just an example): return object1(object2(object3(x))); can be written as: object3 a(x); object2 b(a); object1 c(b); return c; Assuming all 3 classes implement constructors that take rvalue as a parameter, the first version might be faster, because temporary objects are passed and can be moved. I'm assuming that in the 2nd version, the local variables are considered to be lvalues. But if the variables aren't later used, do C++11 compilers optimize the code so

Does D have something akin to C++0x's move semantics?

♀尐吖头ヾ 提交于 2019-11-27 21:08:18
问题 A problem of "value types" with external resources (like std::vector<T> or std::string ) is that copying them tends to be quite expensive, and copies are created implicitly in various contexts, so this tends to be a performance concern. C++0x's answer to this problem is move semantics , which is conceptionally based on the idea of resource pilfering and technically powered by rvalue references . Does D have anything similar to move semantics or rvalue references? 回答1: I believe that there are

Should the assignment operator observe the assigned object's rvalueness?

拜拜、爱过 提交于 2019-11-27 20:57:41
问题 For class types it is possible to assign to temporary objects which is actually not allowed for built-in types. Further, the assignment operator generated by default even yields an lvalue: int() = int(); // illegal: "expression is not assignable" struct B {}; B& b = B() = B(); // compiles OK: yields an lvalue! ... but is wrong! (see below) For the last statement the result of the assignment operator is actually used to initialize a non- const reference which will become stale immediately

Should I return an rvalue reference (by std::move'ing)?

三世轮回 提交于 2019-11-27 18:24:24
A C++Next blog post said that A compute(…) { A v; … return v; } If A has an accessible copy or move constructor, the compiler may choose to elide the copy. Otherwise, if A has a move constructor, v is moved. Otherwise, if A has a copy constructor, v is copied. Otherwise, a compile time error is emitted. I thought I should always return the value without std::move because the compiler would be able to figure out the best choice for users. But in another example from the blog post Matrix operator+(Matrix&& temp, Matrix&& y) { temp += y; return std::move(temp); } Here the std::move is necessary

Difference between “return-by-rvalue-ref” & “return-by-value” when you return using std::move?

大憨熊 提交于 2019-11-27 18:18:48
问题 Considering the following code: #include <iostream> using namespace std; struct I { I(I&& rv) { cout << "I::mvcotr" << endl; } }; struct C { I i; I&& foo() { return move(i) }; } }; int main() { C c; I i = c.foo(); } C contains I. And C::foo() allows you to move I out of C. What is the difference between the member function used above: I&& foo() { return move(i) }; // return rvalue ref and the following replacement member function: I foo() { return move(i) }; // return by value To me, they

Overloading on R-value references and code duplication

橙三吉。 提交于 2019-11-27 18:12:29
Consider the following: struct vec { int v[3]; vec() : v() {}; vec(int x, int y, int z) : v{x,y,z} {}; vec(const vec& that) = default; vec& operator=(const vec& that) = default; ~vec() = default; vec& operator+=(const vec& that) { v[0] += that.v[0]; v[1] += that.v[1]; v[2] += that.v[2]; return *this; } }; vec operator+(const vec& lhs, const vec& rhs) { return vec(lhs.v[0] + rhs.v[0], lhs.v[1] + rhs.v[1], lhs.v[2] + rhs.v[2]); } vec&& operator+(vec&& lhs, const vec& rhs) { return move(lhs += rhs); } vec&& operator+(const vec& lhs, vec&& rhs) { return move(rhs += lhs); } vec&& operator+(vec&&

Avoid exponential grow of const references and rvalue references in constructor

假如想象 提交于 2019-11-27 18:07:43
I am coding some templated classes for a machine learning library, and I'm facing this issue a lot of times. I'm using mostly the policy pattern, where classes receive as template argument policies for different functionalities, for example: template <class Loss, class Optimizer> class LinearClassifier { ... } The problem is with the constructors. As the amount of policies (template parameters) grows, the combinations of const references and rvalue references grow exponentially. In the previous example: LinearClassifier(const Loss& loss, const Optimizer& optimizer) : _loss(loss), _optimizer