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 does 'template argument deduction' do?
Answer: Automatically infers template parameters from function arguments at the call site
Template argument deduction lets the compiler infer template parameters from the types of function call arguments without explicit specification.
Which template feature allows a single template to handle both lvalue and rvalue references universally?
Answer: Forwarding references (universal references) with T&&
When `T&&` appears in a deduced context, it becomes a forwarding (universal) reference that binds to both lvalues and rvalues.
What does `std::forward(arg)` do in a template function?
Answer: Preserves the value category (lvalue or rvalue) of the argument when forwarding
`std::forward` performs a conditional cast that maintains the original value category, enabling perfect forwarding.
Which of the following is true about partial template specialization?
Answer: It allows specializing a template for a subset of its parameters while leaving others generic
Partial specialization lets you fix some template parameters (e.g., specialize `Pair`) while keeping others generic; this is only allowed for class/variable templates.
What is the primary use of `std::type_traits` in template metaprogramming?
Answer: Compile-time type introspection and transformation
`` provides compile-time predicates (e.g., `std::is_integral`) and transformations (e.g., `std::remove_const`) on types.
In CRTP (Curiously Recurring Template Pattern), the base class template parameter is:
Answer: The derived class itself
In CRTP, a derived class passes itself as the template argument to its base: `class Derived : public Base {}`.
What happens when a non-type template parameter is used with a floating-point value in C++ before C++20?
Answer: It is ill-formed; floating-point types were not allowed as non-type template parameters before C++20
Before C++20, non-type template parameters were restricted to integral, pointer, and reference types; floating-point support was added in C++20.