The purpose of this quiz is to give you a chance to focus your knowledge of making a function into a template in C++.
| TRUE ✓ | FALSE ✗ | Many functions we write differ only in the values used at certain points in the code. | ||
|---|---|---|---|---|
| TRUE ✓ | FALSE ✗ | Such functions may be said to have blanks in the values used. | ||
| TRUE ✓ | FALSE ✗ | To avoid having many functions which do the same code to different values, we can pass arguments to represent those values. |
Once when we saw many functions doing the same actions to different ____ of data, there was nothing we could do about the duplication. Now, thanks to the miracle of ____, we can make the data's ____ a blank to be filled in at compile time!
When a function becomes a template, how can the compiler tell whether or not the template type can support all the operations required of it by the function?
It looks through all the function's code to see what operations are done
to that type. This forms a 'requirements list' that is checked on each
call to the function to make sure the prospective types used on actual
arguments meet the requirements of the function on them.
What two new keywords are used with templates? What does each represent?
template: says that the following function definition is a plan for a
function rather than a real function; a requirements list
is to be formed and checked against possible calls to this
'function'
typename: says that the following identifier is to represent a type for
the duration of the current template
Show a template-ized version of the following code. You may also fix any other poor usages as you go.
long maximum(long array[], long MAX_NDX)
{
long i, max;
for (i = 1, max = array[0]; i < MAX_NDX; i++)
{
if (array[i] > max)
{
max = array[i];
}
}
return max;
}
template <typename ArrType>
ArrType maximum(const ArrType array[], size_t MAX_NDX)
{
size_t i;
ArrType max;
for (i = 1, max = array[0]; i < MAX_NDX; ++i)
{
if (array[i] > max)
{
max = array[i];
}
}
return max;
}
Show a template-ized version of the following code. You may also fix any other poor usages as you go.
long maximum(long array[], long MAX_NDX)
{
long i, max;
for (i = 1, max = 0; i < MAX_NDX; i++)
{
if (array[i] > array[max])
{
max = i;
}
}
return max;
}
template <typename ArrType>
size_t maximum(const ArrType array[], size_t MAX_NDX)
{
size_t i, max;
for (i = 1, max = 0; i < MAX_NDX; i++)
{
if (array[i] > array[max])
{
max = i;
}
}
return max;
}
For each of the two previous functions:
(Assume any constant/variable not shown is valid and appropriately initialized.)
short array_s[MAX_S], max_s;
double array_d[MAX_D], max_d;
string array_st[MAX_ST], max_st;
char array_c[MAX_C], max_c;
Date array_Dt[MAX_DT], max_Dt;
size_t max_ndx;
// actual_* and *_len are size_t and represent the length
// of/number of actual data in the array, irrespectively
max_s = maximum(array_s, 10);
max_ndx = maximum(array_Dt, actual_Dt);
max_st = maximum(array_st, st_len);
max_ndx = maximum(array_c, actual_c);
max_d = maximum(array_d, 4);
i) the purpose is to find either the value of or position of
the maximum value in the array; the first one returns the
maximum value itself; the second one returns the index of
the maximum value
ii) a) the long in the first function for both the array base, the
max variable, and the return were for the type of elements
in the array; the other longs here (i and MAX_NDX), were for
positions and were subsequently changed to size_t
b) in the second function, the long was the element type only
for the array base type; all other longs were for positions
and were thus changed to size_t
iii) a) in the first function:
1) array is subscripted and the result is tested with >
and used in an assignment right side against max
2) MAX_NDX is used in the right side of a < with i
3) i is assigned 1, tested against MAX_NDX, and passed to
the subscript of array
4) max is assigned the 0-position value from array, tested
against subsequent array values, and assigned from them
when the test succeeds
b) in the second function:
1) array is subscripted and the result is tested with >
against max position value
2) MAX_NDX is used in the right side of a < with i
3) i is assigned 1, tested against MAX_NDX, and passed to
the subscript of array
4) max is assigned 0, used to subscript array, and assigned
from i when the test on array subscripts succeeds
iv) based entirely off the return value storage, the second version
should be called when max_ndx is the stored value and the first
on all three other calls
v) as they stand, the two templates cannot coexist in a single program;
the only distinguishing characteristic is the return type which is
ignored by the compiler when determining overload-ability
if used separately, however, the calls would succeed as long as the
Date class has overloaded operator> to compare if one Date falls
after another within the calendar year
Given the following template and attempted instantiations:
template <typename DataType>
void swap(DataType & x, DataType & y)
{
DataType t;
t = x;
x = y;
y = t;
return;
}
// variables:
short a, b;
char c, d;
double e, f;
string g, h;
long i, j;
Rational k, m;
// calls:
swap(a, b);
swap(c, d);
swap(e, f);
swap(g, h);
swap(i, j);
swap(k, m);
swap(a, j);
swap(g, i);
swap(c, h);
i) the DataType template type requries default construction and
assignment to/from itself; the template further requires that
both of its arguments be exactly the same type due to the pure
references
ii) a) a,b: yes
b) c,d: yes
c) e,f: yes
d) g,h: yes
e) i,j: yes
f) k,m: yes
g) a,j: no
h) g,i: no
i) c,h: no
iii) a) a,b: swap<short>
b) c,d: swap<char>
c) e,f: swap<double>
d) g,h: swap<string>
e) i,j: swap<long>
f) k,m: swap<Rational>
g) a,j: arguments are of different types
h) g,i: arguments are of different types
i) c,h: arguments are of different types