skip to content

Templates and Metaprogramming

Templates generate code per type at compile time, and constexpr, traits and concepts compute and constrain at compile time too. Senior C++ interviews go deep here.

on this pageshow

explore

questions

page 2 of 2

A C++ caching library defines Hasher<T*>, Hasher<const T*> and Hasher<const char*>: which one does each pointer type use, and when is the choice ambiguous?

level: seniorimportance: should knowfreq 45%

basics

~20 s

A full specialization matching exactly wins outright; otherwise the compiler picks the most specialized matching partial specialization. Hasher<const char*> uses its full specialization, const double* uses Hasher<const T*>, char* falls to Hasher<T*>. Two matches with neither more specialized is an error.

open as a page

How do you write your own C++ trait is_sequence<T> that is true for std::vector and std::list, and why does it miss const std::vector<int>?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Declare a primary template deriving from std::false_type, add partial specializations for std::vector<T, A> and std::list<T, A> deriving from std::true_type, and provide an is_sequence_v helper. Specializations match exact types, so the helper should strip cv and references first.

open as a page

Your C++ telemetry logger prints its arguments by recursion over a parameter pack; why rewrite it as a comma fold, and why not expand into function arguments instead?

level: seniorimportance: should knowfreq 45%

basics

~10 s

Recursion instantiates one function per remaining pack length and needs a base case; a comma fold, (static_cast<void>(emit(args)), ...), is one expression evaluated left to right. Expanding into function arguments, sink(emit(args)...), leaves the order unspecified.

open as a page

Your numeric algorithms library's public API is built on std::enable_if; how do you decide whether, when and how to move it to C++20 concepts?

level: principalimportance: should knowfreq 30%

basics

~20 s

Move when every supported consumer builds as C++20, or keep a dual path behind __cpp_concepts. The gains are named requirements, readable errors and overloads ordered by subsumption; treat each changed constraint as an API change and pin the accepted types in compile-only tests.

open as a page

Your header-only C++ serialization templates dominate build time across 2,000 files: when do you switch to explicit instantiation with extern template, and what does it cost?

level: principalimportance: should knowfreq 30%

basics

~20 s

Switch when profiling shows a few heavy specializations instantiated in many files and the type set is closed. extern template in the header plus one explicit instantiation removes duplicate code generation; parsing and inline members still cost every includer.

open as a page

How does C++26 pack indexing with Ts...[I] and values...[I] replace the older ways of reaching the Nth element of a pack?

level: seniorimportance: nice to knowfreq 20%

basics

~20 s

C++26 lets you subscript a pack: Ts...[I] names the Ith type and values...[I] the Ith value, with I a constant expression below sizeof.... Earlier code needed recursive helper templates or std::tuple_element_t and std::get on a tuple.

open as a page

showing 31–36 of 36