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
16
votes
3 answers

C++, rvalue references in function parameters

I'm trying to understand rvalue references. I have seen how they are used in constructors, with things like std::move and std::forward, but I still don't understand why this doesn't work: void func(string&& str) { cout << str << endl; } int…
CarlosHD
  • 193
  • 1
  • 6
16
votes
2 answers

C++0x rvalue references - lvalues-rvalue binding

This is a follow-on question to C++0x rvalue references and temporaries In the previous question, I asked how this code should work: void f(const std::string &); //less efficient void f(std::string &&); //more efficient void g(const char * arg) { …
Doug
  • 8,780
  • 2
  • 27
  • 37
16
votes
3 answers

Why do we need to set rvalue reference to null in move constructor?

//code from https://skillsmatter.com/skillscasts/2188-move-semanticsperfect-forwarding-and-rvalue-references class Widget { public: Widget(Widget&& rhs) : pds(rhs.pds) // take source’s value { rhs.pds = nullptr; // why?? …
uchar
  • 2,552
  • 4
  • 29
  • 50
16
votes
1 answer

Swapping with rvalues

Suppose I want swap that works on rvalues, and don't want to write 4 versions for all combinations of rvalue/lvalue references (rvalue/rvalue version is kinda pointless but it doesn't hurt). I came up with this: template
yuri kilochek
  • 12,709
  • 2
  • 32
  • 59
16
votes
3 answers

Should I std::move a shared_ptr in a move constructor?

Consider: #include #include #include #include #include #include using namespace std; class Gizmo { public: Gizmo() : foo_(shared_ptr(new string("bar"))) {}; Gizmo(Gizmo&&…
John Dibling
  • 99,718
  • 31
  • 186
  • 324
15
votes
2 answers

About catching exception good practices

I'm writing a little program in C++11 and really use exceptions for one of the first time. I've got a question about how to catch the exceptions efficiently, and after some googling I still don't have the answer. Here is the question : What is the…
Jaffa
  • 12,442
  • 4
  • 49
  • 101
15
votes
2 answers

move constructor and std::move confusion

I am reading about the std::move, move constructor and move assignment operator. To be honest, all I got now is confusion. Now I have a class: class A{ public: int key; int value; A(){key = 3; value = 4;} //Simple move constructor …
Allanqunzi
  • 3,230
  • 1
  • 26
  • 58
15
votes
3 answers

Return value or rvalue reference?

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() && {…
Barry
  • 286,269
  • 29
  • 621
  • 977
15
votes
1 answer

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

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&& vec; }; int main() { S s1{std::vector(5)}; // construct with temporary std::cout <<…
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
15
votes
2 answers

construction helper make_XYZ allowing RVO and type deduction even if XZY has noncopy constraint

UPDATE1: C++17 added type deduction for constructors - which does not imply that the free function is an inferior solution. UPDATE2: C++17 added guaranteed copy elision (the copy does not even take place conceptually). So with C++17 my code actually…
15
votes
4 answers

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

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…
Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
14
votes
2 answers

Intuitive understanding of functions taking references of references

Possible Duplicate: What does T&& mean in C++11? For some reason, this is eluding my intuition, and I cannot find any explanation on the internet. What does it mean for a C++ function to take a reference of a reference? For example: void…
Daniel
  • 6,595
  • 9
  • 38
  • 70
14
votes
3 answers

Why does operator* of rvalue unique_ptr return an lvalue?

Using the return value of operator* from a "dead" unique_ptr is bad. The following code compiles but results of course in Undefined Behavior: auto& ref = *std::make_unique(7); std::cout << ref << std::endl; Why didn't the standard make the…
Amir Kirsh
  • 12,564
  • 41
  • 74
14
votes
1 answer

Avoid dangling reference for reverse range-based for-loop implementation

Background and Previous Search I'm looking for an elegant way to reverse-iterate over a container (e.g. std::vector) using a range-based for-loop in C++14. Searching for a solution I found this Q/A. It basically tells me, that this is not part of…
Don-Umbro
  • 165
  • 9
14
votes
1 answer

Classes, Rvalues and Rvalue References

An lvalue is a value bound to a definitive region of memory whereas an rvalue is an expression value whose existence is temporary and who does not necessarily refer to a definitive region of memory. Whenever an lvalue is used in a position in which…
No One
  • 143
  • 1
  • 5