【问题标题】:Sequencing between the destruction of local automatic function variables vs the construction of the return value局部自动函数变量的破坏与返回值的构造之间的排序
【发布时间】:2019-05-06 03:40:40
【问题描述】:

存在依赖于局部自动函数变量在创建返回值后被销毁这一事实的代码,例如:

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

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

【问题讨论】:

    标签: c++ c++11 c++14 language-lawyer c++17


    【解决方案1】:

    [stmt.return]/3:

    调用结果的复制初始化顺序在之前 完整表达结束时的临时性破坏 由 return 语句的操作数建立,反过来,它是 在破坏局部变量([stmt.jump])之前排序 包含 return 语句的块。

    【讨论】:

      【解决方案2】:

      您标记了多个语言版本。所以我会注意到@songyuanyao 带来的引用并不总是存在。它在DR 1885 下进行了修改,其中详细说明了 return 语句是如何指定不足的。具体而言,C++14 中缺少与排序相关的措辞,并在 C++17 的 DR 下进行了修改。

      实际上,您的代码在 C++14 中也可能是正确的。供应商的 QoI 标准很高。

      【讨论】:

        猜你喜欢
        • 2018-01-20
        • 2023-03-12
        • 1970-01-01
        • 2022-06-23
        • 2019-04-09
        • 2013-06-17
        • 1970-01-01
        • 2016-01-03
        • 1970-01-01
        相关资源
        最近更新 更多