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

1287. std::function requires CopyConstructible target object

Section: 22.10.17.3.2 [func.wrap.func.con] Status: C++11 Submitter: Jonathan Wakely Opened: 2009-12-13 Last modified: 2016-01-28

Priority: Not Prioritized

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

View all issues with C++11 status.

Discussion:

I think std::function should require CopyConstructible for the target object.

I initially thought that MoveConstructible was enough, but it's not. If F is move-only then function's copy constructor cannot be called, but because function uses type erasure, F is not known and so the copy constructor cannot be disabled via enable_if. One option would be to throw an exception if you try to copy a function with a non-copyable target type, but I think that would be a terrible idea.

So although the constructors require that the target be initialised by std::move(f), that's only an optimisation, and a copy constructor is required.

[ 2009-12-24 Moved to Tentatively Ready after 5 positive votes on c++std-lib. ]

Proposed resolution:

Add to 22.10.17.3.2 [func.wrap.func.con] paragraph 9:

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

9 Requires: F shall be CopyConstructible. f shall be callable for argument types ArgTypes and return type R. The copy constructor and destructor of A shall not throw exceptions.