存在依赖于本地自动函数变量在创建返回值后销毁的代码,例如:
1) Unmangling the result of std::type_info::name
std::string demangle(const char* name)
{
int status = -4;
std::unique_ptr<char, void(*)(void*)> res {
abi::__cxa_demangle(name, NULL, NULL, &status),
std::free
};
return (status==0) ? res.get() : name;
}
2) Timing of scope-based lock guards and return values
class C {
mutable std::mutex _lock;
map<string,string> deep_member;
public:
auto get_big_lump()
{
std::unique_lock<std::mutex> lock(_lock);
return deep_member;
}
};
标准在哪里规定这个顺序是有保证的?
最佳答案
The copy-initialization of the result of the call is sequenced before the destruction of temporaries at the end of the full-expression established by the operand of the return statement, which, in turn, is sequenced before the destruction of local variables ([stmt.jump]) of the block enclosing the return statement.
关于c++ - 局部自动函数变量的销毁与返回值的构造之间的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55998516/