MCQs
None
There are two types specialization. They are full specialization and partial specialization.
In this program, We are defining three templates and specializing it and passing the values to
it and printing it.
Output:
$ g++ spec5.cpp
$ a.out
6default10
In this program, We are computing the result in the specalized block of the program.
Output:
$ g++ spec3.cpp
$ a.out
template
Answer:d
Explanation:In this program, We are passing the value of specified type and printing it by specialization.
Output:
$ g++ spec2.cpp
$ a.out
1
1
1.2
In this program, We specifed the type in the template function. We need to compile this program by adding -std=c++0x.
Output:
$ g++ -std=c++0x spec1.cpp
$ a.out
2.1: 378.228
None
When you allocate memory for an array of objects, the default constructor must be called to
construct each object. If no default constructor exists, you’re stuck needing a list of pointers
to objects.
In the template specialization, it will make the template to be specific for some data types.
What is the output of this program?
1.
#include
2.
using namespace std;
3.
template
4.
inline T square(T x)
5.
{
6.
T result;
7.
result = x * x;
8.
return result;
9.
};
10.
template
11.
string square(string ss)
12.
{
13.
return (ss+ss);
14.
};
15.
int main()
16.
{
17.
int i = 2, ii;
18.
string ww("A");
19.
ii = square(i);
20.
cout
Template specialization is used when a different and specific implementation is to be used for a
specific data type. In this program, We are using integer and character.
Output:
$ g++ spec.cpp
$ a.out
2:4AA