Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a perfect forwarding function have to be templated?

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.

like image 688
Mranz Avatar asked Dec 01 '11 23:12

Mranz


People also ask

Why do we need perfect forwarding?

Perfect forwarding reduces excessive copying and simplifies code by reducing the need to write overloads to handle lvalues and rvalues separately.

What is the point of std :: forward?

std::forwardReturns an rvalue reference to arg if arg is not an lvalue reference. If arg is an lvalue reference, the function returns arg without modifying its type.

What is forwarding reference in C++?

When t is a forwarding reference (a function argument that is declared as an rvalue reference to a cv-unqualified function template parameter), this overload forwards the argument to another function with the value category it had when passed to the calling function.

What is universal reference C++?

In a type declaration, “ && ” indicates either an rvalue reference or a universal reference – a reference that may resolve to either an lvalue reference or an rvalue reference. Universal references always have the form T&& for some deduced type T .


1 Answers

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.

like image 146
GManNickG Avatar answered Nov 15 '22 17:11

GManNickG