Last Updated: December 6, 2025
28 quizzes
Which declaration correctly defines a generic max function template for any comparable type?
How does the compiler choose which function template to instantiate?
What is a key benefit of using a class template like std::vector<T>?
Which statement about full template specialization is correct?
What does typename... Args mean in a variadic template?
Which C++17 feature simplifies operating over all arguments of a variadic template?
In template metaprogramming, what does static_assert typically provide?
Which SFINAE-related behavior is correct?
Why might you specialize a template for pointer types?
Which is a practical use of a compile-time Factorial<N> template?
Declare a function template that swaps two values of the same type by reference.
template <typename T>void swapValues( a, b) { std::swap(a, b); }Click an option to fill blank 1 of 2:
Instantiate a Box class template storing std::string.
Box<> messageBox("Hello");Click an option to fill the blank:
Define a variadic template function that forwards all arguments to std::make_shared.
template<typename T, typename... Args>std::shared_ptr<T> makeSharedWrapper(&&... args) { return std::make_shared<T>(std::forward<>(args)...); }Click an option to fill blank 1 of 2:
What is the output of this code using a simple function template?
1#include <iostream>
2template<typename T>
3T add(T a, T b) { return a + b; }
4int main() {
5 std::cout << add(2, 3) << "\n";
6 std::cout << add(1.5, 2.5) << "\n";
7 return 0;
8}What does this code print with a class template instantiation?
1#include <iostream>
2
3template<typename T>
4class Box {
5public:
6 Box(T v) : value(v) {}
7 void print() const { std::cout << value << "\n"; }
8private:
9 T value;
10};
11
12int main() {
13 Box<int> bi(42);
14 Box<std::string> bs("templates");
15 bi.print();
16 bs.print();
17}What is the output of this partial specialization example?
1#include <iostream>
2
3template<typename T>
4struct TypePrinter {
5 static void print() { std::cout << "Generic" << "\n"; }
6};
7
8template<typename T>
9struct TypePrinter<T*> {
10 static void print() { std::cout << "Pointer" << "\n"; }
11};
12
13int main() {
14 TypePrinter<int>::print();
15 TypePrinter<int*>::print();
16}
17What is the output using a variadic template with fold expression?
1#include <iostream>
2
3template<typename... Args>
4int sum(Args... args) {
5 return (args + ... + 0);
6}
7
8int main() {
9 std::cout << sum(1, 2, 3) << "\n";
10 std::cout << sum(5) << "\n";
11}
12What is the output of this compile-time factorial template?
1#include <iostream>
2
3template<int N>
4struct Factorial {
5 static const int value = N * Factorial<N - 1>::value;
6};
7
8template<>
9struct Factorial<0> {
10 static const int value = 1;
11};
12
13int main() {
14 std::cout << Factorial<3>::value << "\n";
15 std::cout << Factorial<5>::value << "\n";
16}
17Find the bug in this generic max function using templates.
Click on the line(s) that contain the bug.
#include <string> template<typename T>T myMax(T a, T b) { return (a > b) ? a : b;} int main() { const char* s1 = "apple"; const char* s2 = "banana"; auto m = myMax(s1, s2);} Find the bug in this class template specialization for managing dynamic memory.
Click on the line(s) that contain the bug.
#include <iostream> template<typename T>class Holder {public: Holder(T value) : value(value) {} ~Holder() {}private: T value;}; template<>class Holder<int*> {public: Holder(int* p) : ptr(p) {} ~Holder() {}private: int* ptr;}; int main() { Holder<int*> h(new int(5));} Click the line that relies on template argument deduction for function templates.
Click on the line to select.
#include <vector> template<typename T>void pushTwice(std::vector<T>& v, const T& value) { v.push_back(value); v.push_back(value);} int main() { std::vector<int> nums; pushTwice(nums, 10);} Click the line where a class template is explicitly specialized.
Click on the line to select.
#include <iostream> template<typename T>struct Printer { static void print(const T& value) { std::cout << value << "\n"; }}; template<>struct Printer<bool> { static void print(bool value) { std::cout << (value ? "true" : "false") << "\n"; }}; int main() { Printer<int>::print(42); Printer<bool>::print(true);} Complete the function template that works with iterators.
template < It>void printRange(It first, It last) { for (; first != last; ++first) { std::cout << << " "; } std::cout << "\n";}Click an option to fill blank 1:
Complete the variadic template that logs all arguments.
#include <iostream>template<typename... Args>void logAll( args) { (std::cout << ... << ) << "\n";}Click an option to fill blank 1:
Match each template-related concept with its description.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match each metaprogramming tool with its purpose.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Order the steps to design and use a generic class template that wraps std::vector<T>.
Drag and drop to reorder, or use the arrows.
Order the steps to write a compile-time factorial using template metaprogramming.
Drag and drop to reorder, or use the arrows.