C++23's `move_only_function`
Is there a difference with https://github.com/lefticus/cpp_weekly/issues/85?
Coming soon. Here are the examples: https://compiler-explorer.com/z/bhv8Gq3G4
Coming in Ep349, November 7, 2023
https://youtu.be/OJtGOJI0JEw
I think using the ASM output is not a correct representation of the advantages of move_only_function. What you observe as "less" code is simply that:
- std::shared is a lot more heavy weight than unique_ptr.
- the gnu move_only_function implementation uses a larger internal storage for the SBO which elide an allocation.
The gnu std::mofunc implementation will require a call for each move operation whereas the gnu std::function implementation will never need one as it only store trivially copiable types in SBO and if heap alloc there is, it will just do a pointer swap.
So in practice I would expect the move operation on an std::function storing your std::shared_ptr lambda example (the one which lead to a "shocking amount of code") to be faster than a std::mofunc move operation where the mofunc stores a unique ptr. Though I obviously did not test that so it is only speculations ;).
TLDR; I think that the only real advantage is if your type is only move constructible. I think you are still better of using std::function most of the time, even if your type is movable.