In C++ what is the best way to return a function local std::string variable from the function?
std::string MyFunc()
{
std::string mystring(\"test\");
ret
As mentioned, the std::string is copied. So even the original local variable has gone out of scope, the caller gets a copy of the std::string.
I think reading on RVO can totally clear your confusion. In this case, it's accurately referred to as NRVO (Named RVO) but the spirit is the same.
Bonus reading: The problem with using RVO is that it's not the most flexible thing in the world. One of the big buzzes of C++0x is rvalue references which intends to solve that problem.