Questions tagged [rvalue-reference]

An rvalue reference is a new language feature in C++11 representing a reference to an rvalue. Together with reference collapsing, they are used to implement and enable move semantics and perfect forwarding.

An rvalue reference is a language feature added in C++11 (formerly known as C++0x). It is used to bind to an rvalue thus extending the temporary object's lifetime.

Together with reference collapsing, they are used to implement and enable move semantics and perfect forwarding.

1087 questions
37
votes
3 answers

Forwarding of return values. Is std::forward is needed?

I am writing library which wraps a lot of functions and methods from other library. To avoid coping of return values I am applying std::forward like so: template T&& wrapper(T&& t) { f(t); // t passed as lvalue return…
Leonid Volnitsky
  • 8,854
  • 5
  • 38
  • 53
33
votes
3 answers

Does the C++ standard guarantee that a failed insertion into an associative container will not modify the rvalue-reference argument?

#include #include #include using namespace std::literals; int main() { auto coll = std::set{ "hello"s }; auto s = "hello"s; coll.insert(std::move(s)); assert("hello"s == s); // Always OK? } Does the C++…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
33
votes
5 answers

Passing rvalues through std::bind

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 #include template void foo(Type &&value) { Type…
Timothy003
  • 2,348
  • 5
  • 28
  • 33
33
votes
2 answers

non-class rvalues always have cv-unqualified types

§3.10 section 9 says "non-class rvalues always have cv-unqualified types". That made me wonder... int foo() { return 5; } const int bar() { return 5; } void pass_int(int&& i) { std::cout << "rvalue\n"; } void pass_int(const int&&…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
33
votes
1 answer

Lvalue to rvalue reference binding

The compiler keeps complaining I'm trying to bind an lvalue to an rvalue reference, but I cannot see how. I'm new to C++11, move semantics, etc., so please bear with me. I have this function: template
Kristian D'Amato
  • 3,996
  • 9
  • 45
  • 69
32
votes
1 answer

What is going on: C++ std::move on std::shared_ptr increases use_count?

I was always assuming that std::move() on a std::shared_ptr steals the pointer and sets the pointer of the original to nullptr-thus not increasing the reference count. That does not seem to be true in my world. SETUP: MacOS, g++ -version => "Apple…
Frank-Rene Schäfer
  • 3,182
  • 27
  • 51
31
votes
3 answers

Passing/Moving parameters of a constructor in C++0x

If I have a constructor with n parameters such that any argument to that can be an rvalue and lvalue. Is it possible to do support this with move semantics for the rvalues without writing 2^n constructors for each possible rvalue/lvalue combination?
Opt
  • 4,774
  • 3
  • 28
  • 28
31
votes
5 answers

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

A problem of "value types" with external resources (like std::vector 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…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
31
votes
4 answers

What's a use case for overloading member functions on reference qualifiers?

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…
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
31
votes
2 answers

Is it useless to declare a local variable as rvalue-reference, e.g. T&& r = move(v)?

Could you guys give me an illustrative example under certain circumstance to prove the following statements are useful and necessary? AnyTypeMovable v; AnyTypeMovable&& r = move(v);
xmllmx
  • 39,765
  • 26
  • 162
  • 323
30
votes
4 answers

Move Constructors and Static Arrays

I've been exploring the possibilities of Move Constructors in C++, and I was wondering what are some ways of taking advantage of this feature in an example such as below. Consider this code: template class Foo { public: Foo() { …
Zeenobit
  • 4,954
  • 3
  • 34
  • 46
30
votes
2 answers

Correct use of `= delete` for methods in classes

Is the following snipplet correct for un-defining all otherwise generated methods and constructors for a class? struct Picture { // 'explicit': no accidental cast from string to Picture explicit Picture(const string &filename) { /* load image…
towi
  • 21,587
  • 28
  • 106
  • 187
30
votes
1 answer

Best form for constructors? Pass by value or reference?

I'm wondering the best form for my constructors. Here is some sample code: class Y { ... } class X { public: X(const Y& y) : m_y(y) {} // (a) X(Y y) : m_y(y) {} // (b) X(Y&& y) : m_y(std::forward(y)) {} // (c) Y m_y; } Y f() { return…
Clinton
  • 22,361
  • 15
  • 67
  • 163
29
votes
3 answers

Syntax for universal references

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 void bar(T&& b); It binds to rvalues and it also binds to lvalues: bar(i); //…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
27
votes
1 answer

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

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…
user283145
1 2
3
72 73