Peter Milley Peter Milley - 9 months ago 46
C++ Question

Why is std::function itself a copy-constructible type?

I've found the tr.func.require section of the specification that requires

to be copy-constructable in C++11, and I've seen plenty of explanations of why this implies that the functors used to construct
have to be copyable, but why did the C++11 standard made
copyable in the first place, instead of making it a move-only type?

Answer Source

std::function was implemented before C++11 when move-semantics were not yet available (it stems from boost::function and was part of TR1).

My guess is that std::function was specified (and perhaps voted into the standard) before move semantics. And the didn't have time to review all the rest of the standard in light of this. See also this answer about why shared_ptr deleters have to be CopyConstructible.

Also, for compatibility reasons, it sounds unreasonable to all-of-a-sudden make std::function move constructible and remove its copy-constructor.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download