best way to return an std::string that local to a function

Solution 1:

No. That is not true. Even if mystring has gone out of scope and is destroyed, ret has a copy of mystring as the function MyFunc returns by value.

Solution 2:

There will be a problem if your code is like:

std::string& MyFunc()
{
    std::string mystring("test");
    return mystring;
}

So, the way you've written it is OK. Just one advice - if you can construct the string like this, I mean - you can do it in one row, it's sometimes better to do it like this:

std::string MyFunc()
{
    return "test";
}

Or if it's more "complicated", for example:

std::string MyFunct( const std::string& s1,
                     const std::string& s2,
                     const char* szOtherString )
{
    return std::string( "test1" ) + s1 + std::string( szOtherString ) + s2;
}

This will give a hint to your compiler to do more optimization, so it could do one less copy of your string (RVO).