On wrapping a callable in a lambda that just calls it with the same parameters
cppdashboard.dev/r/2026/08/on-wrapping-a-callable-in-a-lambda-that-just-calls-it-with-tThis article discusses the unnecessary complexity of wrapping a lambda inside another lambda when the inner lambda can be called directly. It explains that a lambda is just syntactic sugar for a class with a function call operator, and highlights the importance of understanding this to avoid unnecessary indirection.
From the article
Suppose you have a function that accepts a lambda and wants to use it when calling another function. I’ve seen people wrap the lambda inside another lambda:
template<typename Lambda> bool Widget::QueueToWorkerThread(Lambda&& lambda) { CreateWorkerThreadIfNeeded(); return m_dispatcherQueue.TryEnqueue( [lambda = std::forward<Lambda>(lambda)]() { lambda(); } ); } But there’s no point in wrapping a lambda inside another lambda if you are just calling the inner lambda with the same parameters as the outer one. You can use the inner lambda’s function call operator directly.
template<typename Lambda> bool Widget::QueueToWorkerThread(Lambda&& lambda) { CreateWorkerThreadIfNeeded(); return m_dispatcherQueue.TryEnqueue( std::forward<Lambda>(lambda) ); } My guess is that some people don’t realize…
Share this resource