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 pageshowhide
explore
- Instantiation and Deduction5 questions
- Full and Partial Specialization4 questions
- Parameter Packs and Folds5 questions
- SFINAE and Enable If4 questions
- Constraints and Requires-Clauses4 questions
- Type Traits4 questions
- CRTP and Static Polymorphism5 questions
- Constant Expressions5 questions
questions
page 2 of 2A 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?
basics
~20 sA 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.
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>?
basics
~20 sDeclare 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.
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?
basics
~10 sRecursion 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.
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?
basics
~20 sMove 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.
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?
basics
~20 sSwitch 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.
How does C++26 pack indexing with Ts...[I] and values...[I] replace the older ways of reaching the Nth element of a pack?
basics
~20 sC++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.
showing 31–36 of 36