将char数组作为std:string返回

Cod*_*ard 11 c++

以下是否安全,没有显式强制转换或调用std :: string构造函数?如果不安全,为什么不呢?

std:string myfunc()
{
    char buf[128] = "";
    // put something into buf or not base on logic.

   return buf;
}
Run Code Online (Sandbox Code Playgroud)

Naw*_*waz 9

是.那很好.调用者将获得本地缓冲区的副本,因为它std::string会从本地缓冲区中进行深层复制!

编辑:我假设buf是以null结尾的字符串!