In C++ there are always tradeoffs. Let's consider:
using std::functional<void()>; inline void myfunc(task_t task) {
vs
template<typename Func> inline void myfunc(Func func) {
Both can be used for generically injecting predicates and lamdas with captures. The first is easy. This makes it simple to move "blocks" of code around and may seem particularly convenient for thread queues and pools. Yes, my Ruby experience shows. There is also a lot going on behind the scenes in std::function even without wrapping function arguments.
Since there is no templating in the first example, only one kind of function is produced, and the resulting binary is small. It is also slow. A nanosecond slower, perhaps. That is enough to give financial trading system developers nightmares and cause market panics!
The second is less "flexible". However, the compiler will deduce it and optimize your call site. Often it can do so in ways people simply cannot and come up with minimal code for invocation of a given function under the hood. This is why the C++ std library uses condition variable templated waits with predicates formed this way. It may also generate unique code for each usage, leading to classic C++ code bloat.
It is important to know when to think like an embedded developer and when to think like a financial one.
Top comments (0)