How do templates, specialization and concepts work?
Assesses fundamental understanding of C++ conventions, runtime behavior, and memory/performance considerations.
Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.
Templates parameterise code by type and are instantiated at compile time, giving zero-cost abstraction.
template <typename T>
T maxOf(T a, T b) { return a > b ? a : b; }
template <>
const char* maxOf<const char*>(const char* a, const char* b) {
return std::strcmp(a, b) > 0 ? a : b; // explicit specialization
}
Variadic templates and fold expressions handle arbitrary argument counts. C++20 concepts constrain templates and produce readable diagnostics:
template <typename T> requires std::integral<T>
T twice(T x) { return x + x; }
Template definitions usually live in headers because the compiler needs them at instantiation. The downsides are longer compile times, code bloat and historically terrible error messages. auto and decltype help deduction, while static_assert turns assumptions into clear compile-time checks. Prefer concepts over SFINAE for readability in modern code.
Candidate Response Strategy & Interview Tips
- Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
- Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
- Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
- Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.