6 ms·
I am asking because I am relearning C++, but would it preferable to use Boost for callbacks over using lambdas, assuming you’re on a recent version?
by seandhi 8y ago
I am asking because I am relearning C++, but would it preferable to use Boost for callbacks over using lambdas, assuming you’re on a recent version?
- RcouF1uZ4gsC 8y agoYou can actually use both. You can use a lambda to provide the callable for the Boost signal.
- kenrose 8y agoGood point about lambdas. I haven't done much C++ since about 2011, which is when lambdas were added to the standard. In terms of which to use, it depends on your use case. Lambdas let you easily specify a single callback function, so they're a nice syntactic sugar for passing to STL functions (e.g., for_each). If you're the sender and only have one callback to fire, a lambda is great for this. If you have to fire n callbacks, you could probably build a signal / slot mechanism with lambdas. But at that point, I'd start to consider using a dedicated library like signals2. It takes care of a bunch of things like object lifetime (e.g., slot deallocation if an object is destructed), efficient management of the list of slots, and thread safety.
- notatcomputer68 8y agoI would use std::function and lambdas.
- jcelerier 8y ago> but would it preferable to use Boost for callbacks over using lambdas, assuming you’re on a recent version? it's not comparable, a boost::signal is equivalent to a list of functions which gets called when the signal is triggered, instead of a single function.