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 problem does `std::declval()` solve in template metaprogramming?
Answer: It creates a reference to T in unevaluated contexts without requiring a constructor
`std::declval()` produces a value of type T in unevaluated contexts (like `decltype`), enabling type queries on non-constructible types.
What is 'expression SFINAE'?
Answer: Using the validity of an arbitrary expression in the template substitution to trigger SFINAE
Expression SFINAE places expressions inside `decltype` or `sizeof` in template signatures so substitution failure due to an invalid expression removes that overload.
Which of the following correctly uses a concept to constrain a template in C++20?
Answer: template T square(T x) { return x * x; }
In C++20 you constrain a template by placing the concept name directly before the parameter name: `template`.
What is 'tag dispatching' in generic programming?
Answer: Selecting function overloads at compile time by passing empty tag structs based on type traits
Tag dispatching uses empty structs (tags) derived from type traits to route calls to the correct overload at compile time without runtime overhead.
What does `auto` as a template parameter in C++17 non-type parameters enable?
Answer: Deduction of the non-type parameter's type from the supplied value
C++17 allows `template` so the type of the non-type parameter is automatically deduced from the provided constant value.
What is the difference between `std::move` and a cast to `T&&` inside a template?
Answer: `std::move` unconditionally casts to rvalue; `T&&` in deduced context may be a forwarding reference binding to lvalue
`std::move` is an unconditional cast to `T&&` (rvalue), while a deduced `T&&` parameter can collapse to an lvalue reference when T is an lvalue reference type.
Which header contains the primary type trait utilities such as `std::is_same`, `std::is_pointer`, and `std::remove_reference`?
Answer:
The `` header provides the standard library's compile-time type introspection and transformation facilities.