Should all C++ functions be declared taking rvalue from now?

Viewed 2951

Why should I or shouldn't I create all my functions and members functions to take a rvalue and leave out versions that take a lvalue? You can always forward lvalue to rvalue, right? I can even have const rvalue, so why is this a bad idea, or a good idea?

What I mean in code is below. The "&&" rvalue reference allows the users to use temporaries and can still use lvalue by a simple forward. So with this in mind, why should I provide print_string(string& str) (lvalue reference) of any function in c++11 (besides const reference, because rvalue there is fine)?

#include <iostream>
#include <string>
#include <utility>

using namespace std;


void print_string(string&& str)
{
    str += "!!!";
    cout << str << endl;
}
void print_string2(string& str)
{
    str += "???";
    cout << str << endl;
}

int main(int argc, char* argv[])
{

    print_string("hi there");  // works
    print_string(string("hi again")); // works
    string lvalue("lvalue");
    print_string(forward<string>(lvalue)); // works as expected
    //print_string(lvalue);  // compile error

    //print_string2("hi there"); // comile error
    //print_string2(string("hi again")); // compile error
    print_string2(lvalue);


    char a;
    cin >> a;
}

output

hi there!!!
hi again!!!
lvalue!!!
lvalue!!!???

void print_string(string&& str) provides a more flexible use case than void print_string2(string& str) so why shouldn't we use rvalue arguments all the time?

3 Answers
Related