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

前端 未结 6 652
陌清茗
陌清茗 2021-01-30 16:07

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         


        
6条回答
  •  南笙
    南笙 (楼主)
    2021-01-30 16:41

    Have you tried it? The string is copied when it's returned. Well that's the official line, actually the copy is probably optimised away, but either way it's safe to use.

提交回复
热议问题