Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



3. Type-erased (its primary use case, but hinders optimization)

4. Can't be constexpr


Also, std::functions are not comparable. This makes it harder to use for registration/de-registration or an Observer architecture.


I think lambdas also heap allocate, at least on embedded C++11/14 projects I remember having to have a little of heap for them.


Can you point to better alternate ways or idioms?


It'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/Funct...


stlab has a task type that works around a couple of these issues: https://stlab.cc/libraries/concurrency/task/task/


(joke) : Common Lisp




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: