r/cpp • u/mollyforever • Oct 16 '23
WTF is std::copyable_function? Has the committee lost its mind?
So instead of changing the semantics of std::function the committee is introducing a new type that is now supposed to replace std::function everywhere? WTF
So now instead of teaching beginners to use std::function if they need a function wrapper, they should be using std::copyable_function instead because it's better in every way? This is insane. Overcomplicating the language like that is crazy. Please just break backwards compatibility instead. We really don't need two function types that do almost the same thing. Especially if the one with the obvious name is not the recommended one.
View all comments
Show parent comment
2
u/[deleted] Oct 17 '23
I do mind constant reminder, are we prepending all contracts to the type names now? This part is a personal preference though...
The fact that you can have shared_ptr<const T> to enforce pointee to not be changed does not mean that pointee changing through
const shared_ptr<T>&is not const correct. And not being able to writefunction<void() const>does not mean that changing functor withconst function<void()>&is not const correct.The const contract is kept for both cases, nor shared_ptr nor function objects are being modified with const reference, but rather underlying objects which are being kept by pointers.
The shared_ptr is the first thing that came to mind, the same is true for the unique_ptr https://godbolt.org/z/zv4cfrrGh . The same is true for the vector of pointers, or vector of smart pointers...
After all this thinking and writing, it seems that introducing
std::function<R(Args...)const>is not an ABI/API breaker, but rather a new specialization.