I heard a recent talk by Herb Sutter who suggested that the reasons to pass std::vector
and std::string
by const &
are largely gone. He suggested that writing a function such as the following is now preferable:
std::string do_something ( std::string inval )
{
std::string return_val;
// ... do stuff ...
return return_val;
}
I understand that the return_val
will be an rvalue at the point the function returns and can therefore be returned using move semantics, which are very cheap. However, inval
is still much larger than the size of a reference (which is usually implemented as a pointer). This is because a std::string
has various components including a pointer into the heap and a member char[]
for short string optimization. So it seems to me that passing by reference is still a good idea.
Can anyone explain why Herb might have said this?
转载于:https://stackoverflow.com/questions/10231349/are-the-days-of-passing-const-stdstring-as-a-parameter-over