18

Why is the following code valid:

template<typename T1>
void foo(T1 &&arg) { bar(std::forward<T1>(arg)); }

std::string str = "Hello World";
foo(str); // Valid even though str is an lvalue
foo(std::string("Hello World")); // Valid because literal is rvalue

But not:

void foo(std::string &&arg) { bar(std::forward<std::string>(arg)); }

std::string str = "Hello World";
foo(str); // Invalid, str is not convertible to an rvalue
foo(std::string("Hello World")); // Valid

Why doesn't the lvalue in example 2 get resolved in the same manner that it does in example 1?

Also, why does the standard feel it important to need to provide the argument type in std::forward versus simple deducing it? Simply calling forward is showing intention, regardless of the type.

If this isn't a standard thing and just my compiler, I am using msvc10, which would explain the crappy C++11 support.

Thanks

Edit 1: Changed the literal "Hello World" to be std::string("Hello World") to make an rvalue.

Mranz
  • 1,260
  • 1
  • 12
  • 20
  • What happens in bar? Compiling does not mean it's working necessarily. I believe it should be `void foo(T1 &arg)` and `void foo(std::string &arg)` respectively. – AJG85 Dec 01 '11 at 23:11
  • 1
    `"Hello World"` is not an rvalue, it's an lvalue with the type `const char[12]`. – GManNickG Dec 01 '11 at 23:11
  • @AJG85 What happens in bar is not important. && means rvalue reference. – Mranz Dec 01 '11 at 23:14
  • @GMan Why is "Hello World" an lvalue? It is not assignable. Am I missing something sneaky? What would it mean if I changed it to std::string("Hello World")? It is definitely an rvalue then. – Mranz Dec 01 '11 at 23:15
  • @Mranz: Right, `const` makes it non-modifiable. In your latter case, you have an rvalue of type `std::string` (constructed from the literal). – GManNickG Dec 01 '11 at 23:15
  • @Mranz I was under the impression `std::forward` took an *lvalue* reference argument and returned an *rvalue* reference? Could be wrong I've never used or had a need to use this. – AJG85 Dec 01 '11 at 23:17
  • 1
    @AJG85 what you just described is `std::move` – Mranz Dec 01 '11 at 23:20
  • @GMan Thanks for clarifying that the literal is not an rvalue. I will update my question to reflect what I was going for. – Mranz Dec 01 '11 at 23:23
  • @Mranz : All literals _except for string literals_ are rvalues. Your instinct was right; string literals just happen to be the exception. – ildjarn Dec 04 '11 at 21:23

1 Answers1

16

First of all, read this to get a full idea of forwarding. (Yes, I'm delegating most of this answer elsewhere.)

To summarize, forwarding means that lvalues stay lvalues and rvalues stay rvalues. You can't do that with a single type, so you need two. So for each forwarded argument, you need two versions for that argument, which requires 2N combinations total for the function. You could code all the combinations of the function, but if you use templates then those various combinations are generated for you as needed.


If you're trying to optimize copies and moves, such as in:

struct foo
{
    foo(const T& pX, const U& pY, const V& pZ) :
    x(pX),
    y(pY),
    z(pZ)
    {}

    foo(T&& pX, const U& pY, const V& pZ) :
    x(std::move(pX)),
    y(pY),
    z(pZ)
    {}

    // etc.? :(

    T x;
    U y;
    V z;
};

Then you should stop and do it this way:

struct foo
{
    // these are either copy-constructed or move-constructed,
    // but after that they're all yours to move to wherever
    // (that is, either: copy->move, or move->move)
    foo(T pX, U pY, V pZ) :
    x(std::move(pX)),
    y(std::move(pY)),
    z(std::move(pZ))
    {}

    T x;
    U y;
    V z;
};

You only need one constructor. Guideline: if you need your own copy of the data, make that copy in the parameter list; this enables the decision to copy or move up to the caller and compiler.

Community
  • 1
  • 1
GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • 1
    So the reason the first example works is because T1 actually gets resolved to the equivalent of `void foo(const std::string & &&arg)` which gets reduced to `void foo(const std::string &arg)` using the reference deduction rules? It fails in example 2 because there is no lvalue overload for the string? Are there any best practices for defining the template function so that it is at least relatively obvious what the type should be? Ideally I am looking for a good way to be explicit with the type while avoiding 2^N overloads. – Mranz Dec 01 '11 at 23:33
  • 1
    @Mranz: Precisely. Well, what are you going for? Forwarding and templates are meant to be used for forwarding, you shouldn't really need to know the types. – GManNickG Dec 01 '11 at 23:34
  • Lets say I have a constructor that takes in a vector like `Class(.. some args ..., vector items)`. Ideally, I would like to allow for that argument to avoid the copy using move semantics or being an rvalue. To support that, I would either have to template out items, or create an rvalue overload. If I template it out, the actual type is lost at the definition, and the actual argument would have to be deduced by consumers of my class by reading the header file or some comment. – Mranz Dec 01 '11 at 23:38
  • Thanks for the info. That makes a lot of sense. – Mranz Dec 01 '11 at 23:50
  • Why not x(std::forward(pX)), etc? What is std::forward for? – emsr Dec 02 '11 at 15:09