Function Overloading in C++
15 questions · 12 min · Medium
Test your knowledge of templates in C++, including function templates, class templates, template parameters, specialization, multiple template parameters, and the fundamentals of generic programming.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
20 questions · 15 min · Medium
15 questions · 12 min · Medium
B. To enable generic programming with different data types
Templates allow functions and classes to work with different types without requiring separate implementations for every type.
C. template
The template keyword introduces a template declaration, followed by its template parameter list.
A. template <typename T>
The syntax template <typename T> declares a type template parameter named T.
D. A blueprint for generating functions for specified or deduced types
A function template describes a family of functions that can be instantiated for different types.
B. template <typename T> T maximum(T a, T b) { return a > b ? a : b; }
This definition declares a type parameter T and uses it as the return type and parameter type. The type must support the comparison and return operations used by the function.
C. A blueprint for creating classes parameterized by types or other template arguments
Class templates allow reusable class definitions, such as containers, to work with different types.
A. template <typename T> class Box { T value; };
The template declaration introduces T and defines Box as a class template with a data member of type T.
D. A function template
The identity function is a function template that accepts and returns a value of the deduced or explicitly specified type T.
B. Inferring template arguments from function arguments when possible
For function templates, the compiler can often deduce template arguments from the types of function arguments supplied at the call site.
C. Both can introduce a type template parameter
In declarations such as template <typename T> and template <class T>, the two keywords are interchangeable for introducing a type template parameter.
A. Providing a specific implementation for particular template arguments
Specialization allows a template to have a tailored implementation for specific template arguments. Class templates support full and partial specialization, while function templates support explicit full specialization but not partial specialization.
D. It can accept two types, such as Key and Value
A template can declare multiple parameters, for example template <typename Key, typename Value>, allowing each parameter to represent a different type.
B. std::vector
std::vector is a class template that provides a dynamic-size sequence container, such as std::vector<int> or std::vector<std::string>.
C. A template parameter that represents a permitted value rather than a type
A non-type template parameter represents a compile-time value, such as a size used in a declaration like template <std::size_t N>.
A. They improve code reuse while preserving type checking
Templates support reusable generic code while allowing the compiler to check whether the operations required by a template are valid for the types used.