std::function provides a constructor from an rvalue ref.
What happens to the moved function object by standard? Will it be empty so that calling it again has no effects?
std::function provides a constructor from an rvalue ref. What happens to the moved function
Share
Under 20.8.11.2.1p6,
function(function &&f)leavesfin 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
functionperforms type erasure, and function objects can be arbitrarily expensive, the optimisation to leave the moved-from object empty makes sense:After
hhas been constructed by move fromg, one would expect the containedbindhave been transferred fromgtohrather than copying, sogwould be left empty.For the following program, gcc 4.5.1 prints
empty: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
gin a non-empty callable state.