- What is a Variadic template?
- A template that does not take any arguments
- A template that can take a variable number of arguments
- A template that can take only one argument
- None of the above
Explanation:
The correct answer is option "b". A function or class template in C++ can take any number of parameters due to a variadic template. This capability is useful for creating functions that may accept a variable number of parameters, such as printf. An ellipsis (...) in the parameter list is used by variadic templates to denote the ability to take more than one argument. By enabling functions that operate on any number of parameters, they improve flexibility and simplify code.
- How should a template function that takes a non-type parameter be defined?
- template <int, T N> void functionName
- template <class T, int N> void functionName
- template <int N, class T> void functionName
- template <class T> int N functionName
Explanation:
Option "b" is the accurate choice. In C++, when a template function is created with a non-type parameter, it is essential to specify both the type of the non-type parameter and the type parameter. In this scenario, the type parameter is denoted by class T, while the non-type parameter is represented by int N. By defining these parameters, the functionName can be specialized with a particular type for T and an integer value for N. This configuration guarantees that the function can effectively utilize both the type and non-type parameters.
What will be displayed as the result of the code snippet below?
#include <iostream>
template <int N>
int multiply(int value)
{
return value * N;
}
int main()
{
std::cout << multiply<3>(5);
return 0;
}
- Compilation error
- None of the above
Explanation:
The correct answer is option "c". The code provides a template function called multiply, which accepts an integer input value and a non-type parameter N. The function is called with N set to 3 and a value set to 5 in the main function. The value is multiplied by N using the function, providing 5 * 3. Consequently, 15 is the output of the code. The program prints 15 to the standard output when it executes.
- How can a pointer return type function template be defined in C++?
- template <class T> T* functionName(T parameter)
- template <T class> T* functionName(T parameter)
- template <T> class T* functionName(T parameter)
- template <class T> class T* functionName(T parameter)
Explanation:
The correct answer is option "a". In this syntax, T is a template parameter that represents a type, which defines a function template. After receiving a parameter of type T, the function functionName returns a pointer to T, or T. Due to this, the function can operate on any data type that is given at the time the template is created. In this case, the class keyword indicates that T is a placeholder for a type, and the return type is indicated by T as a pointer to T.
- What does SFINAE mean in terms of templates?
- Substitution Failure Is No An Error
- Syntax Failure Is No An Error
- Syntax Failure Is Not An Error
- Substitution Failure Is Not An Error
Explanation:
The correct answer is option "d". SFINAE is a C++ programming principle that is used for template metaprogramming. It stands for "Substitution Failure Is Not An Error", which means that the compiler will attempt other possible template instantiations in the event that a template instantiation fails because of an invalid substitution of template parameters. It makes template code more flexible and robust and makes it possible to select suitable template specializations based on the validity of substitutions.
- What does partial specialization mean in C++ templates?
- Overloading a template function
- Specializing only some of the template parameters
- Specializing all of the template parameters
- None of the above
Explanation:
Partial specialization enables developers to define a subset of the template parameters explicitly, while maintaining the rest of the parameters in a general form. This functionality allows for the utilization of a generic template in various scenarios, while offering specialized behavior for specific conditions where certain parameters meet defined criteria. The process of tailoring templates to accommodate specific requirements becomes more streamlined with partial specialization, especially in cases where all parameters need to be constructed from scratch or fully specialized.