3 ms·
What makes move_only_functions useful?
by charcircuit 4y ago
What makes move_only_functions useful?
- corysama 4y agostd::function is literally an object that can be invoked like a function. As an object, it can contain multiple values. Move-only values are very used for common situations of unique resources that should not be duplicated. But, what if you want to enclose a move-only value in a std::function? Are you simply out of luck and must give up on your dreams? A move-only function lets you get that work done.
- kllrnohj 4y agoMy majority usage of lambdas is for use with work queues (so think Java executor). With the std only the easiest way to build that is a vector of std::function. But then your lambdas can't capture unique_ptrs, even though lambdas themselves have supported move captures for a long long time now. You can do this with std::packaged_task, but that has internal heap allocations for the future. Which if you don't need is just overhead, and not cheap overhead at that.