Define instantiation.
Class or function generated by the compiler from a template.
Write and test your own versions of the
comparefunctions.
Call your
comparefunction on twoSales_dataobjects 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
findalgorithm. 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
beginandendfunctions that take an array argument work? Define your own versions of these functions.
Write a
constexprtemplate 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.
What is a function template? What is a class template?
- function template: Definition from which specific functions can be instantiated.
- class template: Definition from which specific classes can be instantiated.
- differ: the compiler cannot deduce the template parameter type(s) like function templates for a class template. we should supply the list of template arguments to use in place of the template parameters inside angle brackets following the template's name.
What happens when a class template is instantiated?
the compiler rewrites the class template, replacing each instance of the template parameter T by the given template argument.
The following definition of
Listis incorrect. How would you fix it?template <typename elemType> class ListItem; template <typename elemType> class List { public: List<elemType>(); List<elemType>(const List<elemType> &); List<elemType>& operator=(const List<elemType> &); ~List(); void insert(ListItem *ptr, elemType value); private: ListItem *front, *end; };
use of class template ListItem requires template arguments.
void insert(ListItem<elemType> *ptr, elemType value);
ListItem<elemType> *front, *end;Write your own version of the
BlobandBlobPtrtemplates. including the variousconstmembers that were not shown in the text.
Blob, BlobPtr and ConstBlobPtr | Test
Explain which kind of friendship you chose for the equality and relational operators for
BlobPtr.
General friendship that each instantiation of BlobPtr grants access to the version of the equality and relational operators instantiated with the same type.
Write a
Screenclass template that uses nontype parameters to define the height and width of theScreen.
Implement input and output operators for your
Screentemplate. Which, if any, friends are necessary in classScreento make the input and output operators work? Explain why each friend declaration, if any, was needed.
same reason with Blob.
Rewrite the
StrVecclass (§ 13.5, p. 526) as a template namedVec.
What, if any, are the differences between a type parameter that is declared as a
typenameand one that is declared as aclass? When musttypenamebe used?
When we want to inform the compiler that a name represents a type, we must use the keyword typename, not class.
Explain each of the following function template declarations and identify whether any are illegal. Correct each error that you find.
(a) template <typename T, U, typename V> void f1(T, U, V); (b) template <typename T> T f2(int &T); (c) inline template <typename T> T foo(T, unsigned int*); (d) template <typename T> f4(T, T); (e) typedef char Ctype; template <typename Ctype> Ctype f5(Ctype a);
Fixed:
(a) template <typename T, typename U, typename V> void f1(T, U, V);
(b) template <typename T> T f2(int &);
(c)Write a function that takes a reference to a container and prints the elements in that container. Use the container’s
size_typeandsizemembers to control the loop that prints the elements.
Rewrite the function from the previous exercise to use iterators returned from
beginandendto control the loop.
For each labeled statement explain what, if any, instantiations happen. If a template is instantiated, explain why; if not, explain why not.
template <typename T> class Stack { }; void f1(Stack<char>); // (a) class Exercise { Stack<double> &rsd; // (b) Stack<int> si; // (c) }; int main() { Stack<char> *sc; // (d) f1(*sc); // (e) int iObj = sizeof(Stack< string >); // (f) }
Solution:
- (a) No instantiation, compiles, it got instantiated when called.
- (b) No instantiation, compiles, references and pointers doesn't need instantiation
- (c) Instantiation. Doesn't compile!
- (d) No instantiation, compiles, references and pointers doesn't need instantiation
- (e) Instantiation of Stack. Doesn't compile!
- (f) Instantiation of Stackstd::string. Doesn't compileNo instantiation, compiles, references and pointers doesn't need instantiation!
Solution from How is a template instantiated? - Stack Overflow