Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++11

I've found the tr.func.require section of the specification that requires std::function to be copy-constructable in C++11, and I've seen plenty of explanations of why this implies that the functors used to construct std::function have to be copyable, but why did the C++11 standard made std::function copyable in the first place, instead of making it a move-only type?

like image 982
Peter Milley Avatar asked May 08 '17 16:05

Peter Milley


1 Answers

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.

like image 170
jotik Avatar answered Nov 10 '22 08:11

jotik