Zero runtime overhead: each instantiation is a concrete, type-specific function the compiler optimizes like hand-written code, no runtime type checks, no virtual dispatch, no boxing. RingBuffer<uint8_t,64>::push is as tight as a C function written specifically for bytes. The catch is code size: every distinct instantiation generates its own separate copy of the code, so RingBuffer<uint8_t,64>, RingBuffer<uint16_t,64>, and RingBuffer<Event,16> produce three full sets of functions in flash. On a flash-constrained MCU, many instantiations can bloat the binary. You mitigate by limiting how many distinct instantiations you create (avoid parameterizing on values that needn't vary), factoring type-independent logic into a non-template base so the bulk exists once, and checking the map file. So the trade is: a bit of flash for type safety and zero runtime cost, usually excellent, but worth measuring.
C++ for Embedded · Interview question
Why are templates considered zero-overhead, and what's the catch?
A strong answer
What a weak answer sounds like
You know the answer. Do you know what gets you dinged?
Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.
From the lesson
Templates: The Basics
Write a function or class once and have the compiler stamp out a type-specific version for each use: generic code with zero runtime cost, plus the code-bloat tradeoff to watch.