I'm familiar, that the append
in std::string
returns std::string&
and therefore it do not qualify to be moved from, so the result will not be moved.
#include <string>
int main()
{
std::string s = std::string("A").append("B");
return s.size();
}
#include <string>
int main()
{
std::string s = std::move(std::string("A").append("B"));
return s.size();
}
There you can see, that the latter example will produce one fewer allocation and therefore in this case it is better to move something that may looks like a temporary. My question is why they (the committee) do not add simple overload on &&
to make the result of append
either std::string&
, or std::string&&
depending on the context? I mean something similar to std::optional
is doing with value
. Is there a example that will prove such optimization bogus?