3 ms·
Problems with std::function:- 1. It can not hold move only callable objects. 2. It heap allocate stored callable object if the object is large enough.
by mhogomchungu 5y ago
Problems with std::function:-
1. It can not hold move only callable objects.
2. It heap allocate stored callable object if the object is large enough.
- stargrazer 5y agoCan you point to better alternate ways or idioms?
- MontagFTB 5y agostlab has a task type that works around a couple of these issues: https://stlab.cc/libraries/concurrency/task/task/ https://stlab.cc/libraries/concurrency/task/task/
- MauranKilom 5y agoIt's possible to write something like `std::unique_function` (which uniquely owns the stored callable and can be moved but not copied). That's often preferable for storing functors. Example: https://github.com/facebook/folly/blob/main/folly/docs/Function.md https://github.com/facebook/folly/blob/main/folly/docs/Funct...
- jjtheblunt 5y ago(joke) : Common Lisp
- pvarangot 5y agoI think lambdas also heap allocate, at least on embedded C++11/14 projects I remember having to have a little of heap for them.
- nly 5y ago3. Type-erased (its primary use case, but hinders optimization) 4. Can't be constexpr
- tcbawo 5y agoAlso, std::functions are not comparable. This makes it harder to use for registration/de-registration or an Observer architecture.