Article

Reducing C++ template bloat by factoring out the type-dependent portions of the function, practical exam

The Old New Thing ·The Old New Thing ·Published 2026-08-21 ·5 min read

This article discusses techniques for reducing C++ template bloat by factoring out type-dependent portions of functions. It highlights the inefficiencies of using lambdas in templated functions and presents a method to simplify the function by using a shared type.

From the article

A short time ago, we observed that there’s usually no need to wrap a callable in a lambda , and more recently observed that we can apply our principles for reducing C++ template bloat to simplify the function further.

Just to refresh our memories, here is where we left off:

template<typename Lambda> bool Widget::QueueToWorkerThread(Lambda&& lambda) { CreateWorkerThreadIfNeeded(); return m_dispatcherQueue.TryEnqueue( std::forward<Lambda>(lambda)); } As I noted earlier, lambdas are sort of the worst-case scenario for templated functions since every lambda is a unique type. Every time you call it, you force the generation of a new function.


Share this resource


Discovered 2026-08-24 Source The Old New Thing Archive 2026-08 →