rvalue-reference

Do compilers automatically use move semantics when a movable object is used for the last time?

妖精的绣舞 提交于 2019-11-28 07:19:29
问题 I've been studying rvalue references lately and came to a conclusion that it's quite advantageous to use pass-by-value everywhere where complete copy of an object will be made (for complete justification see e.g. How to reduce redundant code when adding rvalue reference operator overloads? and Want speed? Pass by value!), because the compiler can automatically optimize a copy away in cases such as f(std::move(a)); , where f is defined as void f(A a); . One negative consequence of pass-by

Why does C++11 have implicit moves for value parameters, but not for rvalue parameters?

删除回忆录丶 提交于 2019-11-28 06:25:18
In C++11, value parameters (and other values) enjoy implicit move when returned: A func(A a) { return a; // uses A::A(A&&) if it exists } At least in MSVC 2010, rvalue reference parameters need std::move : A func(A && a) { return a; // uses A::A(A const&) even if A::A(A&&) exists } I would imagine that inside functions, an rvalue reference and a value behave similar, with the only difference that in case of values, the function itself is responsible for destruction, while for rvalue references, the responsibility is outside. What is the motivation for treating them differently in the standard?

Should I write constructors using rvalues for std::string?

萝らか妹 提交于 2019-11-28 06:01:12
I have a simple class: class X { std::string S; X (const std::string& s) : S(s) { } }; I've read a bit about rvalues lately, and I've been wondering, if I should write constructor for X using rvalue, so I would be able do detect temporary objects of std::string type? I think it should look something like: X (std::string&& s) : S(s) { } As to my knowledge, implementation of std::string in compilers supporting C++11 should use it's move constructor when available. X (std::string&& s) : S(s) { } That is not a constructor taking an rvalue , but a constructor taking an rvalue-reference . You should

Preventing non-const lvalues from resolving to rvalue reference instead of const lvalue reference

◇◆丶佛笑我妖孽 提交于 2019-11-28 05:57:10
I'm having trouble overloading a function to take a value either by const reference or, if it is an rvalue, an rvalue reference. The problem is that my non-const lvalues are binding to the rvalue version of the function. I'm doing this in VC2010. #include <iostream> #include <vector> using namespace std; template <class T> void foo(const T& t) {cout << "void foo(const T&)" << endl;} template <class T> void foo(T&& t) {cout << "void foo(T&&)" << endl;} int main() { vector<int> x; foo(x); // void foo(T&&) ????? foo(vector<int>()); // void foo(T&&) } The priority seems to be to deduce foo(x) as

Passing by value vs const & and && overloads

 ̄綄美尐妖づ 提交于 2019-11-28 05:17:29
So after looking up move semantics I see that general consensus is to pass by value when you intend to transfer ownership. But in Scott Meyer's talk on Universal references I've noticed that std::vector::push_back has 2 overloads: void push_back( const T& value ); void push_back( T&& value ); So I thought to myself, wouldn't void push_back( T value ); be enough? I've asked a few people which ultimately lead to the following test case: #include <memory> #include <iostream> #include <type_traits> struct A { A() { std::cout << "A Default constructor\n"; } A(const A &) { std::cout << "A Copy\n"; }

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

我们两清 提交于 2019-11-28 05:02:13
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 8 overloads for each binary operation: // core rvalue overloads for plus: tree operator +(const tree& a

Return value or rvalue reference?

☆樱花仙子☆ 提交于 2019-11-28 02:51:28
问题 In Scott Meyer's new book, he proposes an example usage for rvalue reference qualifiers that looks something like this: class Widget { private: DataType values; public: DataType& data() & { return values; } DataType data() && { return std::move(values); } // why DataType? }; So that: auto values = makeWidget().data(); move-constructs values instead of copy-constructing it. Why does the rvalue-ref-qualified data() return DataType instead of DataType&& ? auto would still deduce DataType in that

Why does the rvalue overload of `operator<<` for `basic_ostream` return an lvalue reference?

爱⌒轻易说出口 提交于 2019-11-28 02:27:04
问题 §27.7.3.9 defines the following overload for operator<< : template <class charT, class traits, class T> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>&& os, const T& x); Effects: os << x Returns: os ( §27.7.2.6 defines the rvalue overload for operator>> .) Basically, it just forwards to an lvalue overload. I consider this overload to be pretty dangerous (the istream one even more so than the ostream one, actually), consider the following: #include <sstream> #include

Operator overload which permits capturing with rvalue but not assigning to

爷,独闯天下 提交于 2019-11-28 00:07:09
Is it possible to design and how should I make overloaded operator+ for my class C to have this possible: C&& c = c1 + c2; but this not possible: c1 + c2 = something; Edit: I changed objects to small letters. c1 , c2 and c are objects of class C . && is not the logical operator&& , but rather an rvalue reference. For example writing: double&& d = 1.0 + 2.0; is 100% proper (new) C++ code, while 1.0 + 2.0 = 4.0; is obviously a compiler error. I want exactly the same, but instead for double, for my class C . Second edit: If my operator returns C or C&, I can have assignment to rvalue reference,

Using of rvalue references in c++11

被刻印的时光 ゝ 提交于 2019-11-28 00:03:23
问题 I would like to implement a function that fills up a vector and then returns an rvalue reference. I tired something like: std::vector<int> &&fill_list() { std::vector<int> res; ... do something to fill res ... return res; } int main(int argc, char **argv) { std::vector<int> myvec = fill_list(); return 0; } but that doesn't work, I get the following error: error: invalid initialization of reference of type 'std::vector<int>&&' from expression of type 'std::vector<int>' So, all in all, how is