std::function的目标对象禁止销毁的地方在哪里?

Where is it prohibited for target object of std::function to throw on destruction?

考虑 std::function 定义:

namespace std {
  template<class> class function;       // not defined

  template<class R, class... ArgTypes>
  class function<R(ArgTypes...)> {
  public:
    /* ... */
    template<class F> function(F&&);

    /* ... */

    ~function();

    /* ... */
  };

  /* ... */
}

析构函数未明确标记noexcept。该声明被解释为它在 C++14 中不是 noexcept,而是从 C++17 开始的 noexcept。实现似乎加强了这一点并在 C++14 中的 noexcept 中标记(这是允许的):https://godbolt.org/z/WPh8zs7WE

目前的草案对析构函数没有说太多,只是说它销毁目标对象。见 [func.wrap.func.con]/31:

~function();

Effects: If *this != nullptr, destroys the target of this.

构造函数参数中列出了目标对象的一些要求,[func.wrap.func.con]/8 through [func.wrap.func.con]/11。具体来说就是Lvalue-CallableCpp17CopyConstructible.

但是我没看到哪里规定目标对象析构函数不抛出。

是否在任何地方指定?

或者 function 的析构函数不是 noexcept 吗?

这是图书馆范围内的要求,在 [res.on.functions] 中指定:

In certain cases ([...], operations on types used to instantiate standard library template components), the C++ standard library depends on components supplied by a C++ program. If these components do not meet their requirements, this document places no requirements on the implementation.

In particular, the effects are undefined in the following cases:

  • [...]
  • If any [...] destructor operation exits via an exception, unless specifically allowed in the applicable Required behavior: paragraph.