C++ Templates & Generics Quiz
40 in-depth questions covering C++ template fundamentals including function templates, class templates, template type deduction, non-type template parameters, and generic programming style — with 16 code examples to master compile-time polymorphism patterns.
Question 1
What is a template in C++?
Question 2
What is a function template in C++?
template<typename T>
T max(T a, T b) {
return (a > b) ? a : b;
}
int main() {
int x = max(5, 3); // T = int
double y = max(5.5, 3.2); // T = double
return 0;
}Question 3
What is template argument deduction?
template<typename T>
void print(T value) {
std::cout << value << std::endl;
}
int main() {
print(42); // T deduced as int
print(3.14); // T deduced as double
print("hello"); // T deduced as const char*
return 0;
}Question 4
What is a class template in C++?
template<typename T>
class Stack {
private:
std::vector<T> data;
public:
void push(T value) { data.push_back(value); }
T pop() {
T val = data.back();
data.pop_back();
return val;
}
};
int main() {
Stack<int> intStack;
intStack.push(42);
Stack<std::string> stringStack;
stringStack.push("hello");
return 0;
}Question 5
What is template instantiation?
template<typename T>
T square(T x) { return x * x; }
int main() {
int result1 = square(5); // Instantiates square<int>
double result2 = square(3.5); // Instantiates square<double>
return 0;
}
// Compiler generates:
// int square<int>(int x) { return x * x; }
// double square<double>(double x) { return x * x; }Question 6
What is a non-type template parameter?
template<typename T, size_t N>
class Array {
private:
T data[N]; // N is compile-time constant
public:
T& operator[](size_t index) { return data[index]; }
size_t size() const { return N; }
};
int main() {
Array<int, 5> arr; // N = 5
arr[0] = 42;
std::cout << arr.size() << std::endl; // 5
return 0;
}Question 7
What is template specialization?
// General template
template<typename T>
class Printer {
public:
void print(T value) {
std::cout << "Generic: " << value << std::endl;
}
};
// Specialization for bool
template<>
class Printer<bool> {
public:
void print(bool value) {
std::cout << "Bool: " << (value ? "true" : "false") << std::endl;
}
};
int main() {
Printer<int> p1;
p1.print(42); // Uses general template
Printer<bool> p2;
p2.print(true); // Uses specialization
return 0;
}Question 8
What is partial template specialization?
// General template
template<typename T, typename U>
class Pair {
public:
T first;
U second;
};
// Partial specialization for when both types are the same
template<typename T>
class Pair<T, T> {
public:
T first;
T second;
bool same() const { return first == second; }
};
int main() {
Pair<int, double> p1; // Uses general template
Pair<int, int> p2; // Uses partial specialization
p2.first = 5;
p2.second = 5;
std::cout << p2.same() << std::endl; // true
return 0;
}Question 9
What is SFINAE (Substitution Failure is Not An Error)?
template<typename T>
class HasSize {
typedef char Yes;
typedef long No;
template<typename U> static Yes test(decltype(&U::size));
template<typename U> static No test(...);
public:
static const bool value = sizeof(test<T>(0)) == sizeof(Yes);
};
class Container {
public:
size_t size() const { return 0; }
};
int main() {
std::cout << HasSize<Container>::value << std::endl; // true
std::cout << HasSize<int>::value << std::endl; // false
return 0;
}Question 10
What are variadic templates?
template<typename... Args>
void printAll(Args... args) {
// Fold expression (C++17)
(std::cout << ... << args) << std::endl;
}
// Recursive expansion
void printAll() {} // Base case
template<typename T, typename... Args>
void printAll(T first, Args... rest) {
std::cout << first << " ";
printAll(rest...); // Recurse with remaining args
}
int main() {
printAll(1, 2.5, "hello", true); // Prints: 1 2.5 hello 1
return 0;
}Question 11
What is template metaprogramming?
// Compile-time factorial
template<size_t N>
struct Factorial {
static const size_t value = N * Factorial<N-1>::value;
};
template<>
struct Factorial<0> {
static const size_t value = 1;
};
int main() {
std::cout << Factorial<5>::value << std::endl; // 120 (computed at compile-time)
return 0;
}Question 12
What is the difference between typename and class in template parameters?
template<typename T> // Preferred
class Container {
// ...
};
template<class U> // Equivalent
class Container2 {
// ...
};
// Inside templates, typename disambiguates dependent names
template<typename T>
void func() {
typename T::iterator it; // typename required
// T::iterator it; // Error: ambiguous
}Question 13
What is a dependent name in templates?
template<typename T>
class Container {
public:
void func() {
T::iterator it; // Dependent name - could be type or value
typename T::iterator it2; // Disambiguated as type
T::size(); // Dependent name - function call
// No ambiguity here
}
};
class MyClass {
public:
typedef int iterator;
static void size() {}
};Question 14
What is template argument deduction for functions?
template<typename T>
void func(T param) {}
template<typename T>
void func2(T& param) {}
int main() {
int x = 5;
const int cx = 10;
func(x); // T = int
func(cx); // T = int (const is adjusted)
func2(x); // T = int
// func2(cx); // Error: can't bind const int& to int&
return 0;
}Question 15
What is explicit template instantiation?
// In header file
template<typename T>
T multiply(T a, T b);
// In source file
template<typename T>
T multiply(T a, T b) { return a * b; }
// Explicit instantiations
template int multiply(int, int);
template double multiply(double, double);
int main() {
int result = multiply(3, 4); // Uses explicit instantiation
return 0;
}Question 16
What is a template template parameter?
// Template template parameter example
// Allows templates to accept other templates as parametersQuestion 17
What is generic programming in C++?
Question 18
What is the difference between templates and macros?
Question 19
What is template recursion?
// Compile-time Fibonacci using template metaprogramming
// Templates generate code at compile-time for computationQuestion 20
What is the difference between function templates and class templates?
Question 21
What is the difference between templates and inheritance for code reuse?
Question 22
What is the difference between templates and generics in other languages?
Question 23
What is the difference between template parameters and function parameters?
Question 24
What is the difference between template specialization and overloading?
Question 25
What is the difference between template metaprogramming and runtime programming?
Question 26
What is the difference between templates and constexpr?
Question 27
What is the difference between template instantiation and template compilation?
Question 28
What is the difference between template arguments and template parameters?
Question 29
What is the difference between template deduction and template specification?
Question 30
What is the difference between template specialization and template overloading?
Question 31
What is the difference between template metaprogramming and functional programming?
Question 32
What is the difference between template recursion and runtime recursion?
Question 33
What is the difference between template code bloat and runtime code size?
Question 34
What is the difference between template debugging and runtime debugging?
Question 35
What is the difference between template libraries and runtime libraries?
Question 36
What is the difference between template performance and runtime performance?
Question 37
What is the difference between template evolution and runtime evolution?
Question 38
What is the difference between template constraints and runtime constraints?
Question 39
What is the difference between template documentation and runtime documentation?
Question 40
What are the fundamental principles for effective template design in C++?
