Skip to content

Latest commit

 

History

History
60 lines (36 loc) · 2.09 KB

README.md

File metadata and controls

60 lines (36 loc) · 2.09 KB

Chapter 16. Templates and Generic Programming

Exercise 16.1:

Define instantiation.

Class or function generated by the compiler from a template.

Exercise 16.2:

Write and test your own versions of the compare functions.

compare

Exercise 16.3:

Call your compare function on two Sales_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)

Exercise 16.4:

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 a vector<int> and in a list<string>.

Find

Exercise 16.5:

Write a template version of the print function from 6.2.4 (p. 217) that takes a reference to an array and can handle arrays of any size and any element type.

print

Exercise 16.6:

How do you think the library begin and end functions that take an array argument work? Define your own versions of these functions.

begin and end

Exercise 16.7:

Write a constexpr template that returns the size of a given array.

SizeOfArray

Exercise 16.8:

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.