I have the following code:
#include <iostream>
#include <string>
using std::cout;
using std::endl;
void bar(const std::string& str)
{
cout << "const str - " << str << endl;
}
void bar(std::string&& str)
{
cout << "str - " << str << endl;
}
void foo(std::string&& str)
{
bar(str);
}
int main()
{
foo("Hello World");
}
In the above code the void bar(const std::string& str)
overload gets called. If I want the void bar(std::string&& str)
overload to be called I either have to write bar(std::move(str));
or bar(std::forward<std::string>(str));
Obviously the forward code is longer, but it makes more sense to me. My question is what is more commonly used and prefered. Writing bar(std::forward(str));
would be the best solution imo, but that is not an option :)