2

文档说 Deleter 应该是:

  • 不扔可构造
  • nothrow 可调用(因为它是从~unique_ptr() noexcept
  • nothrow destructible(出于上述原因)

我的问题是为什么uniqut_ptr定义为允许一个Deleter可能抛出的。例如,所有构造函数Deleter都允许以下内容:unique_ptr

struct BadDeleter
{
    BadDeleter() noexcept(false) {}
    ~BadDeleter() noexcept(false) {}
    BadDeleter(const BadDeleter&) noexcept(false) {}
    BadDeleter(BadDeleter&) noexcept(false) {}
    BadDeleter(BadDeleter&&) noexcept(false) {}
    void operator()(char* p) const noexcept(false) {
        delete p;
    };
};

现场演示

4

1 回答 1

2

该标准仅uniqe_ptr根据unique_ptr' 操作定义对 ' 删除器的要求。虽然这些要求总是说诸如

~unique_ptr();
要求:表达式 get_deleter()(get()) 应具有良好的格式,应具有明确定义的行为,并且不应引发异常。

该标准从未明确指定删除器必须具有noexcept operator().

我认为选择这个措辞是为了保持与 C++14 的向后兼容。在该标准中,noexcept它不是函数签名的一部分,并且在 C++17 中添加该要求可能会破坏许多使用自定义删除器但未将其操作明确标记为noexcept.

于 2018-03-06T23:07:36.893 回答