Monday, 9 January 2017

c++ - is returning a const std::string really slower than non-const?

In another question a user made a comment that returning a const std::string loses move construction efficiency and is slower.



Is it really true that assigning a string of return of this method:



const std::string toJson(const std::string &someText);

const std::string jsonString = toJson(someText);


... is really slower than the non-const version:



std::string toJson(const std::string &str);

std::string jsonString = toJson(someText);


And what is the meaning of move-construction efficiency in this context?



I've never heard of that limitation before and do not remember having seen that in the profiler. But I'm curious.



Edit: There is a suggested question asking: What is move semantics?. While some of the explanations of course relate to efficiency, it explains what move semantics means, but does not address why returning a const value can have negative side effects regarding performance.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...