CPP Templates & Generic Programming 2 — Questions and Answers
Question 1: What is the purpose of `std::enable_if` in template programming?
- To conditionally enable a template specialization based on a compile-time boolean (Correct answer)
- To enable runtime polymorphism for template classes
- To allow templates to accept an unlimited number of arguments
- To enable lazy evaluation of template expressions
Correct answer: To conditionally enable a template specialization based on a compile-time boolean
`std::enable_if` uses SFINAE to include or exclude a template overload based on a compile-time boolean condition.
Question 2: Which of the following correctly declares a variadic template function?
- template<typename... Args> void f(Args... args); (Correct answer)
- template<typename Args[]> void f(Args args);
- template<variadic typename T> void f(T t);
- template<typename T, ...> void f(T t);
Correct answer: template<typename... Args> void f(Args... args);
The `...` after `typename` declares a parameter pack, and `Args... args` expands it in the function parameter list.
Question 3: What does `sizeof...(Args)` return in a variadic template?
- The number of types in the parameter pack (Correct answer)
- The total byte size of all arguments
- The maximum size of any single type in the pack
- A compile-time string of the types
Correct answer: The number of types in the parameter pack
`sizeof...(Args)` is a compile-time operator that returns the number of elements in a parameter pack.
Question 4: Which concept best describes SFINAE?
- Substitution Failure Is Not An Error — invalid template substitutions are silently discarded (Correct answer)
- Static Function Invocation And Evaluation — templates are evaluated lazily
- Single Function Instance And Namespace Encapsulation
- Strict Forwarding In Non-Arbitrary Expressions
Correct answer: Substitution Failure Is Not An Error — invalid template substitutions are silently discarded
SFINAE means that when template argument substitution fails, the compiler removes that overload from consideration instead of issuing an error.
Question 5: What is a 'fold expression' introduced in C++17 for variadic templates?
- A compact syntax to apply a binary operator to all elements of a parameter pack (Correct answer)
- A way to collapse multiple template specializations into one
- A method to fold function pointers into a single callable
- A syntax to merge two parameter packs together
Correct answer: A compact syntax to apply a binary operator to all elements of a parameter pack
Fold expressions allow applying operators like `+` or `&&` across all pack elements, e.g., `(args + ...)` sums the pack.
Question 6: What is the output of `template<typename T> T add(T a, T b) { return a + b; }` called as `add(1, 2.0)`?
- Compilation error due to conflicting type deduction (Correct answer)
- 3 (int result)
- 3.0 (double result)
- Undefined behavior
Correct answer: Compilation error due to conflicting type deduction
Template argument deduction fails because `T` is deduced as both `int` (from 1) and `double` (from 2.0), causing a compile error.
Question 7: What is an 'explicit instantiation' in C++ templates?
- A directive that forces the compiler to generate template code for a specific type (Correct answer)
- A constructor call that bypasses implicit conversions
- A specialization that overrides the primary template
- A runtime mechanism that instantiates templates on demand
Correct answer: A directive that forces the compiler to generate template code for a specific type
Explicit instantiation (e.g., `template class MyClass<int>;`) directs the compiler to generate the class for a given type, useful for controlling compilation units.
What is the purpose of `std::enable_if` in template programming?