TRT
Sign in
mediumAdvancedconcept

Why must function templates live in headers and how does deduction pick the type?

Explain deduction, instantiation per type, and header placement with the max_of example for ints and strings.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstFunction Templates: One max for Every TypePro — members onlyWrite a single max that works for ints, strings and your own types, and read the deduction and instantiation rules that make it safe.

A strong answer

A strong candidate frames Why must function templates live in headers and how does deduction pick the type? around the exact memory layout, compiler semantics, and hardware execution model covered in Function Templates: One max for Every Type. Explain deduction, instantiation per type, and header placement with the max_of example for ints and strings.

One definition serves int, double, std::string and your Book. The compiler writes the overloads you would have written by hand, without the drift that hand copies accumulate.

Hand copies drift in small ways. One copy uses strict less. Another copy uses less or equal. A third copy forgets constant references and copies each string. Bugs hide in those small gaps. A single template removes the gaps by keeping one source of truth.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Write a function template with correct const-reference parameters.
  • Mastery of core invariant: Explain deduction, instantiation and why templates live in headers.
  • Mastery of core invariant: Predict why a template that compiles for int can fail for another type.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01What happens when two arguments deduce conflicting types?
  2. 02Why does an undefined reference appear when a template lives in a cpp file?

Not started

Sign in to save your learning progress.

Sign in to save