What happens first, implicit conversion to return value or destruction of a local variable?

I just came across a code similar to the following that looks suspicious to me (details are not provided to protect the innocent):

std::string MakeString() { char buf[12] = { 0 }; return &buf[0]; } 

Is this normal or unsafe? Is it guaranteed that std :: string is created before buf goes out of scope?

+4
source share
1 answer

What you wrote is equivalent to:

 std::string MakeString() { char buf[12] = { 0 }; return buf; } 

And it is always guaranteed that this code is safe. In fact, this case is not much different from any function that returns a value by copying.

+6
source

Source: https://habr.com/ru/post/1501426/


All Articles