gpt4 book ai didi

c++ - 局部自动函数变量的销毁与返回值的构造之间的顺序

转载 作者:太空狗 更新时间:2023-10-29 19:51:02 25 4
gpt4 key购买 nike

存在依赖于本地自动函数变量在创建返回值后销毁的代码,例如:

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;
}
};

标准在哪里规定这个顺序是有保证的?

最佳答案

[stmt.return]/3 :

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com