rvalue-reference

In C++, what categories (lvalue, rvalue, xvalue, etc.) can expressions that produce temporaries of class type fall into?

青春壹個敷衍的年華 提交于 2019-11-26 18:59:46
Here is some example code: #include <iostream> class Foo { public: explicit Foo(int x) : data(x) {}; Foo& operator++() { data += 1; return *this; } void *get_addr() { return (void*)this; } friend Foo operator + (const Foo& lhs, const Foo& rhs); friend std::ostream& operator << (std::ostream& os, const Foo& f); private: int data; }; std::ostream& operator << (std::ostream& os, const Foo& f) { return (os << f.data); } Foo operator + (const Foo& lhs, const Foo& rhs) { return Foo(lhs.data + rhs.data); } void bar(Foo& f) { std::cout << "bar(l-value ref)" << std::endl; } void bar(const Foo& f) { std

Is there a reference_wrapper<> for rvalue references?

孤街浪徒 提交于 2019-11-26 14:22:39
问题 I wonder how the following can be done void f(string &&s) { std::string i(move(s)); /* other stuff */ } int main() { std::string s; bind(f, s)(); // Error. bind(f, move(s))(); // Error. bind(f, ref(s))(); // Error. } How can I pass an rvalue reference and store it as an rvalue reference (possibly wrapped) in the call wrapper? I know I can manually write up a class like std::reference_wrapper<> that has a conversion function to T&& , but I would rather want to avoid that and use Standard

Why is `std::move` named `std::move`?

[亡魂溺海] 提交于 2019-11-26 14:02:39
The C++11 std::move(x) function doesn't really move anything at all. It is just a cast to r-value. Why was this done? Isn't this misleading? It is correct that std::move(x) is just a cast to rvalue - more specifically to an xvalue , as opposed to a prvalue . And it is also true that having a cast named move sometimes confuses people. However the intent of this naming is not to confuse, but rather to make your code more readable. The history of move dates back to the original move proposal in 2002 . This paper first introduces the rvalue reference, and then shows how to write a more efficient

On how to recognize Rvalue or Lvalue reference and if-it-has-a-name rule

青春壹個敷衍的年華 提交于 2019-11-26 12:43:35
问题 I was reading Thomas Becker\'s article on rvalue reference and their use. In there he defines what he calls if-it-has-a-name rule: Things that are declared as rvalue reference can be lvalues or rvalues. The distinguishing criterion is: if it has a name, then it is an lvalue. Otherwise, it is an rvalue. This sounds very reasonable to me. It also clearly identifies the rvalueness of an rvalue reference. My questions are: Do you agree with this rule? If not, can you give an example where this

Do rvalue references to const have any use?

狂风中的少年 提交于 2019-11-26 12:23:51
I guess not, but I would like to confirm. Is there any use for const Foo&& , where Foo is a class type? Howard Hinnant They are occasionally useful. The draft C++0x itself uses them in a few places, for example: template <class T> void ref(const T&&) = delete; template <class T> void cref(const T&&) = delete; The above two overloads ensure that the other ref(T&) and cref(const T&) functions do not bind to rvalues (which would otherwise be possible). Update I've just checked the official standard N3290 , which unfortunately isn't publicly available, and it has in 20.8 Function objects [function

Passing rvalues through std::bind

会有一股神秘感。 提交于 2019-11-26 11:05:14
问题 I want to pass an rvalue through std::bind to a function that takes an rvalue reference in C++0x. I can\'t figure out how to do it. For example: #include <utility> #include <functional> template<class Type> void foo(Type &&value) { Type new_object = std::forward<Type>(value); // move-construct if possible } class Movable { public: Movable(Movable &&) = default; Movable &operator=(Movable &&) = default; }; int main() { auto f = std::bind(foo<Movable>, Movable()); f(); // error, but want the

What&#39;s a use case for overloading member functions on reference qualifiers?

耗尽温柔 提交于 2019-11-26 09:31:40
问题 C++11 makes it possible to overload member functions based on reference qualifiers: class Foo { public: void f() &; // for when *this is an lvalue void f() &&; // for when *this is an rvalue }; Foo obj; obj.f(); // calls lvalue overload std::move(obj).f(); // calls rvalue overload I understand how this works, but what is a use case for it? I see that N2819 proposed limiting most assignment operators in the standard library to lvalue targets (i.e., adding \" & \" reference qualifiers to

Overload resolution between object, rvalue reference, const reference

穿精又带淫゛_ 提交于 2019-11-26 09:22:17
问题 Given all three functions, this call is ambiguous. int f( int ); int f( int && ); int f( int const & ); int q = f( 3 ); Removing f( int ) causes both Clang and GCC to prefer the rvalue reference over the lvalue reference. But instead removing either reference overload results in ambiguity with f( int ) . Overload resolution is usually done in terms of a strict partial ordering, but int seems to be equivalent to two things which are not equivalent to each other. What are the rules here? I seem

Is the pass-by-value-and-then-move construct a bad idiom?

泪湿孤枕 提交于 2019-11-26 09:16:51
问题 Since we have move semantics in C++, nowadays it is usual to do void set_a(A a) { _a = std::move(a); } The reasoning is that if a is an rvalue, the copy will be elided and there will be just one move. But what happens if a is an lvalue? It seems there will be a copy construction and then a move assignment (assuming A has a proper move assignment operator). Move assignments can be costly if the object has too many member variables. On the other hand, if we do void set_a(const A& a) { _a = a; }

Syntax for universal references

谁说我不能喝 提交于 2019-11-26 08:51:42
问题 This is an rvalue reference: void foo(int&& a); It does not bind to lvalues: int i = 42; foo(i); // error This is a universal reference: template<typename T> void bar(T&& b); It binds to rvalues and it also binds to lvalues: bar(i); // okay This is an rvalue reference: template<typename T> struct X { void baz(T&& c); }; It does not bind to lvalues: X<int> x; x.baz(i); // error Why do universal references use the same syntax as rvalue references? Isn\'t that an unnecessary source of confusion?