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
12
votes
4 answers

function && qualifier behaviour

I'm confused by the following code: struct test { void f() & { std::cout << "&" << std::endl; } void f() const& { std::cout << "const&" << std::endl; } void f() && { std::cout << "&&" << std::endl; } void f() const&& { …
Tim
  • 693
  • 3
  • 11
12
votes
1 answer

Can an rvalue reference bind to a function?

I tested the following code with GCC, Clang, ICC and VS: void f() {} void g(void (&&)()) { } int main() { g(f); } As we can see, g takes an rvalue reference but f is an lvalue and, in general, rvalue references cannot be bound to lvalues.…
Cassio Neri
  • 19,583
  • 7
  • 46
  • 68
12
votes
2 answers

Assigning this pointer to rvalue reference to a pointer

Should the following sample compile? struct B; struct A { A(B*&&){} }; struct B : A { B() : A(this){} }; int main(){} On LWS with clang it compiles, but with gcc I get: no known conversion for argument 1 from 'B* const' to 'B*&&' and if I…
Jesse Good
  • 50,901
  • 14
  • 124
  • 166
12
votes
3 answers

Move Semantics and Pass-by-Rvalue-Reference in Overloaded Arithmetic

I am coding a small numeric analysis library in C++. I have been trying to implement using the latest C++11 features including move semantics. I understand the discussion and top answer at the following post: C++11 rvalues and move semantics…
Tientuinë
  • 205
  • 2
  • 7
11
votes
4 answers

Move semantics - what it's all about?

Possible Duplicate: Can someone please explain move semantics to me? Could someone point me to a good source or explain it here what are the move semantics?
smallB
  • 16,662
  • 33
  • 107
  • 151
11
votes
3 answers

Conditional compilation for move operations

How can I check whether my compiler supports rvalue references or not? Is there a standard preprocessor macro, or do different compilers have different macros? Ideally, I would want to write this: #ifdef RVALUE_REFERENCES_SUPPORTED foobar(foobar&&…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
11
votes
1 answer

Is it possible to return the current object if it is an r-value reference?

I have recently learned about r-value references. In order to more thoroughly experiment I decided to write a simple DenseMatrix class. My question is is it possible to write any function ( Transpose for this example ) such that for auto A =…
Hen3
  • 175
  • 8
11
votes
2 answers

rvalue for a std::string parameter

What's the difference in practice between LVALUE and RVALUE in the following code when I pass the text? I mean, in this specific case of a string (where the string is a string literal), is there any benefit of using RVALUE (&&)? void…
anc
  • 106
  • 4
  • 18
11
votes
1 answer

What is multimap::emplace() and move()?

I was viewing the MSDN doc about multimap and find that it has a member function multimap::emplace(). Below is the example of that member function. int main( ) { using namespace std; multimap m1; pair is1(1,…
MorrisLiang
  • 702
  • 2
  • 7
  • 20
11
votes
2 answers

Is it necessary to define move constructors from different classes?

Consider the following: struct X { Y y_; X(const Y & y) :y_(y) {} X(Y && y) :y_(std::move(y)) {} }; Is it necessary to define a constructor like the second one in order to take full advantage of move semantics? Or will it be taken…
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
11
votes
2 answers

Why is T&& instantiated as int&?

Can anyone please explain why this compiles and why does t end up with type int&? #include void f(int& r) { ++r; } template void g(Fun fun, T&& t) { fun(std::forward(t)); } int main() { int i…
Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
11
votes
2 answers

Can't bind lvalue to rvalue reference

I have this C++ test code snippet, #include class A { std::vector x; public: A(std::vector&& _x) : x(_x) {} }; class B { A a; public: B(std::vector&& _x) : a(/*move(*/_x/*)*/) {} }; I'm…
Samik
  • 575
  • 2
  • 8
  • 19
11
votes
2 answers

Can an rvalue be moved into a shared_ptr

Is it possible to 'move' an rvalue into a shared_ptr. All the methods which I've tried so far result in a copy. My desired usage pattern is: class Element { public: Element(const string &); Element(const Element &) = delete; //delete copy…
Elliot Woods
  • 834
  • 11
  • 20
11
votes
2 answers

Variables declared by &&

Thinking about (x|r|l|pr|gl)values, the following question came to my mind: Consider the following two variable declarations: X x = ...; and X&& x = ...; and assume the ... do not deliver an xvalue. Can anybody think of code not using decltype in…
JohnB
  • 13,315
  • 4
  • 38
  • 65
11
votes
1 answer

Move from *this in an rvalue method?

In C++11, methods can be overloaded on whether or not the expression that denotes the object on which the method is called is an lvalue or an rvalue. If I return *this from a method called via an rvalue, do I need to explicitly move from *this or…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662