5 ms·
Regarding returning lambdas, of course it could be done even in c++11 via std::function.
by gpderetta 5y ago
Regarding returning lambdas, of course it could be done even in c++11 via std::function.
- steerablesafe 5y agoThat is not really the same. `std::function` is a heavy-weight type-erased wrapper over callable types.
- nickysielicki 5y agoUnfortunately it’s the only way to get a recursive lambda, as far as I’m aware.
- gpderetta 5y agoYou can use a fix point combinator.
- mhogomchungu 5y agoProblems 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.