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.