4

My compiler is the latest VC++ 2013 preview.

#include <utility>

struct BigObject { ... };

void f(BigObject&&){}
void f(BigObject&) {}
void f(BigObject)  {}

int main()
{
    BigObject big_obj;

    BigObject&  r1 = big_obj; // OK.
    BigObject&& r2 = big_obj; // error C2440
    BigObject&& r3 = std::move(big_obj); // OK.
    BigObject&& r4 = r3; // error C2440

    f(r3); // error C2668: 'f' : ambiguous call to overloaded function
}

When, where, and why should we use BigObject&& rv = std::move(big_obj);?

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
xmllmx
  • 39,765
  • 26
  • 162
  • 323
  • int&& expects an rvalue. n and r3 aren't rvalue's but move makes it one. – user1810087 Sep 12 '13 at 09:40
  • 1
    http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx – Alex F Sep 12 '13 at 09:43
  • std::move means: take parameter and treat is as rvalue. – Alex F Sep 12 '13 at 09:44
  • 1
    Since `int` is a fundamental type, the short answer is "never". – Kerrek SB Sep 12 '13 at 10:08
  • @Kerrek, please review my post. – xmllmx Sep 12 '13 at 10:19
  • What do you want to know? Rvalue references bind to rvalues, lvalue references to lvalues. Use whichever you need to solve a given problem. – Kerrek SB Sep 12 '13 at 12:06
  • The ambiguous overload stems from the fact that reference and by-value parameters are indistinguishable to the compiler when comparing them to determine which overload is a better match. – Xeo Sep 12 '13 at 12:37
  • Not sure what you're trying to achieve here. Are you aware that `r3` is an lvalue? May I suggest you take some time to read [this question](http://stackoverflow.com/questions/3106110/)? – fredoverflow Sep 12 '13 at 18:23

2 Answers2

3

When, where, and why should we use BigObject&& rv = std::move(big_obj);?

Short answer: never, nowhere, and for the following reason:

All named variable expressions are lvalues. So what this is doing is taking big_obj, which is an lvalue, coercing it into an xvalue, and using that to initialize an rvalue reference which can then only be used as an lvalue, and we're back to where we started. It's a completely useless line of code.

Except for function arguments, defining local rvalue references is generally not very useful. They do extend the lifetimes of the temporary objects used to initialize them, though, so they might find occasional use in separating complex expressions across multiple statements.

Value category is a property of expressions, not of references. You choose which kind of reference to use based on the value category of the expressions you want it to be able to bind to. And you use std::move to force a particular overload of a function to be called where it would otherwise call the wrong one or be ambiguous.

Bjarne's advice given during a Q&A session at GoingNative2013 was basically, don't try to use rvalue references for anything more "clever" than move constructors and move assignment operators.

And from Herb Sutter: A lot of people think move means writing && everywhere, but it's not. You don't need to write && yourself, unless you're writing a move constructor or move assignment operator, or perfectly forwarding an argument in a function template.

Oktalist
  • 14,336
  • 3
  • 43
  • 63
-3

https://www.google.ch/search?q=rvalue+references+introduction

This yields a lot of good articles:

user1810087
  • 5,146
  • 1
  • 41
  • 76
Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
  • 5
    As useful as those articles may be, link-only answers are not appreciated (link-rot, no immediate value, ...); could you sum up their points ? – Matthieu M. Sep 12 '13 at 11:57
  • 4
    The point is that the question essentially asks for a full explanation of what rvalue references are and how to use them, and this is a Q&A site, not a tutorial site. I could have instead made a "Close: Too Broad" vote, but I decided not to in this instance. – Sebastian Redl Sep 12 '13 at 12:15
  • 3
    That "answer" is really just a glorified RTFM. – Etienne de Martel Sep 12 '13 at 16:12
  • -1. Worst answer. (what if after few days the links are dead? The answer would become *completely* empty!) – Nawaz Sep 12 '13 at 16:19