Define instantiation.
Class or function generated by the compiler from a template.
Write and test your own versions of the
compare
functions.
Call your
compare
function on twoSales_data
objects to see how your compiler handles errors during instantiation.
error C2678: binary '<': no operator found which takes a left-hand operand of type 'const Sales_data' (or there is no acceptable conversion)
Write a template that acts like the library
find
algorithm. The function will need two template type parameters, one to represent the function’s iterator parameters and the other for the type of the value. Use your function to find a given value in avector<int>
and in alist<string>
.
Write a template version of the
How do you think the library
begin
andend
functions that take an array argument work? Define your own versions of these functions.
Write a
constexpr
template that returns the size of a given array.
In the “Key Concept” box on page 108, we noted that as a matter of habit C++ programmers prefer using != to using <. Explain the rationale for this habit.
As we’ve seen, only a few library types, vector
and string
being among them, have the subscript operator. Similarly, all of the library containers have iterators that define the ==
and !=
operators. Most of those iterators do not have the <
operator. By routinely using iterators and !=
, we don’t have to worry about the precise type of container we’re processing.