This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++17 status.

2565. std::function's move constructor should guarantee nothrow for reference_wrappers and function pointers

Section: [func.wrap.func.con] Status: C++17 Submitter: Tim Song Opened: 2015-12-05 Last modified: 2017-07-30

Priority: 0

View all other issues in [func.wrap.func.con].

View all issues with C++17 status.

Discussion: [func.wrap.func.con]/5 guarantees that copying a std::function whose "target is a callable object passed via reference_wrapper or a function pointer" does not throw exceptions, but the standard doesn't provide this guarantee for the move constructor, which makes scant sense.

[2016-02, Issues Telecon]

P0; move to Tentatively Ready.

Proposed resolution:

This wording is relative to N4567.

[Drafting note: The inserted paragraph is a copy of [func.wrap.func.con]/5, only changing "copy constructor" to "copy or move constructor". It does not attempt to fix the issue identified in LWG 2370, whose P/R will likely need updating if this wording is adopted.]

  1. Insert after [func.wrap.func.con]/6:

    function(function&& f);
    template <class A> function(allocator_arg_t, const A& a, function&& f);

    -6- Effects: If !f, *this has no target; otherwise, move-constructs the target of f into the target of *this, leaving f in a valid state with an unspecified value.

    -?- Throws: Shall not throw exceptions if f's target is a callable object passed via reference_wrapper or a function pointer. Otherwise, may throw bad_alloc or any exception thrown by the copy or move constructor of the stored callable object. [Note: Implementations are encouraged to avoid the use of dynamically allocated memory for small callable objects, for example, where f's target is an object holding only a pointer or reference to an object and a member function pointer. — end note]