Templates & Generic Programming Flashcards
7 cards from real CPP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Templates & Generic Programming flashcards as text
What is 'template metaprogramming' (TMP)?
Answer: Using templates to perform computations and generate code at compile time
TMP exploits the C++ template system as a Turing-complete compile-time computation engine, enabling zero-overhead abstractions.
Which C++20 feature provides a cleaner way to constrain template parameters than `enable_if`?
Answer: Concepts
C++20 Concepts allow attaching named semantic constraints directly to templates with `requires` clauses, replacing verbose `enable_if` idioms.
What is the `requires` keyword used for in C++20 template programming?
Answer: Specifying constraints that template arguments must satisfy
`requires` introduces a constraint on a template, and if the constraint is not satisfied the template is removed from overload resolution.
What is a 'policy-based design' in C++ generic programming?
Answer: Decomposing a class's behavior into interchangeable template parameters (policies)
Policy-based design (popularized by Andrei Alexandrescu) separates concerns into composable policy classes passed as template parameters.
What does `if constexpr` allow in template functions (C++17)?
Answer: Discarding branches at compile time based on a compile-time condition
`if constexpr` evaluates the condition at compile time and discards the false branch entirely, preventing instantiation of ill-formed code in that branch.
When a function template and a non-template function are equally viable overloads, which is preferred?
Answer: The non-template function is preferred
C++ overload resolution prefers non-template functions over equally-matched function template specializations.
What is 'template template parameters'?
Answer: A template parameter that itself is a class template, not a concrete type
Template template parameters accept a template (not a type) as an argument, e.g., `template class Container>` allows passing `std::vector` itself.