Self-test 5:
Templates
-
Which of the following is true about templates?
- Template is a feature of C++ that allows us to write one code for different data types.
- We can write one function that can be used for all data types including user defined types. Like sort(), max(), min(), ..etc.
- We can write one class or struct that can be used for all data types including user defined types. Like Linked List, Stack, Queue ..etc.
- Template is an example of compile-time polymorphism.
SolutionAll are correct. For D, when a function or class is instantiated from a template, a specialization of that template is created by the compiler for the set of arguments used, and the specialization is referred to as being a generated specialization.
-
What is the output of the following code?
#include <iostream> using namespace std; template <typename T> void fun(const T& x) { static int count = 0; cout << "x = " << x << " count = " << count << endl; ++count; return; } int main() { fun
(1); fun (1); fun (1.1); return 0; } - x = 1 count = 0
x = 1 count = 1
x = 1.1 count = 0 - x = 1 count = 0
x = 1 count = 0
x = 1.1 count = 0 - x = 1 count = 0
x = 1 count = 1
x = 1.1 count = 2 - Compilation error
SolutionA is correct. Compiler creates a new instance of a template function for every data type. So a compiler creates two functions in the above example, one for funand another for fun . Every instance has its own copy of static variable count. fun is called twice, so count is incremented for the second call, whereas fun is called only once and its count is zero at the call.
- x = 1 count = 0
-
Which of the following is true about the output?
#include <iostream> using namespace std; template <typename T> T myMax(T x, T y) { return (x > y)? x : y; } int main() { cout << myMax(3, 7) << endl; cout << myMax(3.0, 7.0) << endl; cout << myMax(3, 7.0) << endl; return 0; }
- 7
7.0
7.0 - Compilation error in all cout statements as the data type is not specified.
- Compilation error in the last cout statement as the call to myMax is ambiguous.
- None of the above
SolutionC is correct. The template argument type T is automatically deduced by the compiler.
- 7
-
What is the output of the following code? Assume that the size of char is 1 byte and size of int is 4 bytes.
#include <iostream> #include <cstdlib> using namespace std; template <class T, class U> class A { T x; U y; static int count; }; int main() { A<char, char> a; A<int, int> b; cout << sizeof(a) << endl; cout << sizeof(b) << endl; return 0; }
- 6
12 - 2
8 - Compilation error: There can not be more than one template arguments.
- 8
8
SolutionB is correct. Since count is static, it is not counted by sizeof( ).
- 6
-
What is the output of the following code?
#include <iostream> using namespace std; template <class T> class Test { private: T val; public: static int count; Test() { count++; } }; template <class T> int Test<T>::count = 0; int main() { Test<int> a; Test<int> b; Test<double> c; cout << Test<int>::count << endl; cout << Test<double>::count << endl; return 0; }
- 0
0 - 1
1 - 2
1 - 1
0
SolutionC is correct. There are two classes created by the template: Test<int> and Test<double>. Since count is a static member, every class has its own copy of it. Also, count gets incremented in constructor.
- 0
-
What is the output of the following code? Assume that the size of int is 4 byte and size of double is 8 bytes.
#include <iostream> #include <cstdlib> using namespace std; template <class T, class U, class V=double> class A { T x; U y; V z; }; int main() { A<int, int> a; A<double, double> b; cout << sizeof(a) << endl; cout << sizeof(b) << endl; return 0; }
- 16
24 - 8
16 - 20
28 - Compilation error: template parameters cannot have default values.
SolutionA is correct. Templates can also have default parameters. The rule is same as for functions: all default values must be on the rightmost side.
- 16
-
What is the output of the following code?
#include <iostream> using namespace std; template <class T, int max> int arrMin(T arr[], int n) { int m = max; for (int i = 0; i < n; i++) if (arr[i] < m) m = arr[i]; return m; } int main() { int arr1[] = {10, 20, 15, 12}; int n1 = sizeof(arr1)/sizeof(arr1[0]); char arr2[] = {1, 2, 3}; int n2 = sizeof(arr2)/sizeof(arr2[0]); cout << arrMin<int, 10000>(arr1, n1) << endl; cout << arrMin<char, 256>(arr2, n2); return 0; }
- Compilation error, template argument must be a data type.
- 10
1 - 10000
256 - 1
1
SolutionB is correct. We can pass non-type arguments to templates.
-
Which of the following is an invalid template declaration?
- template <int x> int func() {return x;}
- template <double x> double func() {return x;}
- template <typename x> void func(x t) {}
- template <class T> func<T*>(T* x) {}
SolutionB and D are invalid. For B, floating-point numbers and class-type objects are not allowed as non-type arguments. For D, func<T*> is invalid.
-
What is the output of the following code?
#include <iostream> using namespace std; template <int i> void fun() { i = 20; cout << i; } int main() { fun<10>(); return 0; }
- 10
- 20
- Compilation error
SolutionC is correct. Compilation error in line "i = 20;" Non-type parameters are treated as const and cannot be modified.
-
What is the output of the following code?
#include <iostream> using namespace std; template <class T> T max (T &a, T &b) { return (a > b)? a : b; } template <> int max <int> (int &a, int &b) { cout << "Called "; return (a > b)? a : b; } int main () { int a = 10, b = 20; cout << max<int>(a, b); }
- 20
- Called 20
- Compilation error
SolutionB is correct. The above program is an example of template specialization. Sometimes we want a different behaviour of a function/class template for a particular data type. For this, we can create a specialized version for that particular data type. Thus, in the above example, when max( ) is called with two int's, the specialized max<int>( ) is used instead of instantiating it from the general max function template.