CPP Templates & Generic Programming 3 — Questions and Answers
Question 1: What does 'template argument deduction' do?
- Automatically infers template parameters from function arguments at the call site (Correct answer)
- Converts runtime types into compile-time template parameters
- Deduces return types using trailing return type syntax
- Generates default values for template parameters
Correct 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.
Question 2: Which template feature allows a single template to handle both lvalue and rvalue references universally?
- Forwarding references (universal references) with T&& (Correct answer)
- Const references with const T&
- Rvalue-only overloads with T&&
- Pointer templates with T*
Correct 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.
Question 3: What does `std::forward<T>(arg)` do in a template function?
- Preserves the value category (lvalue or rvalue) of the argument when forwarding (Correct answer)
- Converts the argument to an rvalue unconditionally
- Copies the argument and forwards it to a new thread
- Casts the argument to the base class type T
Correct answer: Preserves the value category (lvalue or rvalue) of the argument when forwarding
`std::forward<T>` performs a conditional cast that maintains the original value category, enabling perfect forwarding.
Question 4: Which of the following is true about partial template specialization?
- It allows specializing a template for a subset of its parameters while leaving others generic (Correct answer)
- It is supported for both function templates and class templates in C++
- It requires all template parameters to be specified
- It replaces the primary template entirely
Correct 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<T, int>`) while keeping others generic; this is only allowed for class/variable templates.
Question 5: What is the primary use of `std::type_traits` in template metaprogramming?
- Compile-time type introspection and transformation (Correct answer)
- Runtime type identification similar to RTTI
- Providing default implementations for standard algorithms
- Detecting and catching template instantiation errors
Correct answer: Compile-time type introspection and transformation
`<type_traits>` provides compile-time predicates (e.g., `std::is_integral<T>`) and transformations (e.g., `std::remove_const<T>`) on types.
Question 6: In CRTP (Curiously Recurring Template Pattern), the base class template parameter is:
- The derived class itself (Correct answer)
- A virtual base class
- An abstract interface type
- A policy class passed by the user
Correct answer: The derived class itself
In CRTP, a derived class passes itself as the template argument to its base: `class Derived : public Base<Derived> {}`.
Question 7: What happens when a non-type template parameter is used with a floating-point value in C++ before C++20?
- It is ill-formed; floating-point types were not allowed as non-type template parameters before C++20 (Correct answer)
- It works identically to integer non-type parameters
- It triggers implicit conversion to the nearest integer
- It compiles only if the value is constexpr
Correct 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.
What does 'template argument deduction' do?