Move semantic with std::function

There is too much confusion around this question. I'm going to try to lay things out clearly...

This section describes the moved-from state of std-defined objects:

17.6.5.15 [lib.types.movedfrom]

Objects of types defined in the C++ standard library may be moved from (12.8). Move operations may be explicitly specified or implicitly generated. Unless otherwise specified, such moved-from objects shall be placed in a valid but unspecified state.

What does this mean? This means that given a std-defined moved-from object, you can do anything with that object that doesn't require a priori knowledge of the state of that object. The class of actions that require no a priori knowledge of the current state are those that have no preconditions.

For example you can call clear() on a moved-from vector because there are no preconditions on vector::clear(). But you can't call pop_back() because that does have preconditions.

Looking specifically at the call operator of function:

20.8.11.2.4 [func.wrap.func.inv]

R operator()(ArgTypes... args) const

Effects: INVOKE(f, std::forward(args)..., R) (20.8.2), where f is the target ob- ject (20.8.1) of *this.

Returns: Nothing if R is void, otherwise the return value of INVOKE (f, std::forward( args)..., R).

Throws: bad_function_call if !*this; otherwise, any exception thrown by the wrapped callable object.

Note that there is no precondition or Requires clause. That means that calling the call operator of function of a moved-from function is not undefined behavior. No matter what state the function is in, you're not going to violate any preconditions with this call.

Note that in no case does the specification say that the call will have no effect. So having no effect is not a possibility.

The call will either call the wrapped function, or throw a bad_function_call. Those are the only two choices. And which behavior it has depends on the state of the function object. And the state of the function object is unspecified ([lib.types.movedfrom]).


Under 20.8.11.2.1p6, function(function &&f) leaves f in a valid state with an unspecified value.

The empty state is a valid state, so you should expect that the moved-from function object can be empty.

Because function performs type erasure, and function objects can be arbitrarily expensive, the optimisation to leave the moved-from object empty makes sense:

std::function<void()> g{std::bind{f, std::array<int, 1000>{}}};
std::function<void()> h{std::move{g}};

After h has been constructed by move from g, one would expect the contained bind have been transferred from g to h rather than copying, so g would be left empty.

For the following program, gcc 4.5.1 prints empty:

#include <functional>
#include <iostream>
void f() {}
int main() {
    std::function<void()> g{f}, h{std::move(g)};
    std::cout << (g ? "not empty\n" : "empty\n");
}

This is not necessarily the most optimal behaviour; inlining small callables (e.g. function pointers) creates a situation where copying the callable is more efficient than moving it and emptying the moved-from object, so another implementation could leave g in a non-empty callable state.


What happens to the moved function object by standard?

It will be in a valid state (thus the object can be used), but the actual state that it is in is unspecified. The last part means that calling any function that requires the object to be in a specific state will not necessarily work.

Will it be empty so that calling it again has no effects?

You cannot assume it will be. Calling the function requires that it actually have a function to call. That's part of its state. And since the state is unspecified, the results of calling it are unspecified.

If you want to use the object in some meaningful way again, simply create a new function and assign it to it:

function<...> old;
function<...> new_ = std::move(old);
old = function<...>(...); //Reset to known state.
old(...); //Call is well-defined.