Thinking in C++ Notes (16)

Chapter 16: Introduction to Templates


  • you will almost always put the entire template declaration and definition in the header file, for ease of use.
  • C++ is a strongly-typed language, but templates provide a kind of weak typing mechanism for C++.
  • Template arguments are not restricted to class types, you can also use built-in types. the value of these arguments then become compile-time constants for that particular instantiation of the template. you can even use default values for these arguments
  • P712, can't you put a static data member definitions in the header file? CANNOT
  • first creating and debugging an ordinary class, then making it into a template, is generally considered to be easier than creating the template from scratch

  • an iterator is a “smart pointer”, it usually mimic most pointer operations.
  • iterator class is nested inside the container class
  • an iterator is created with a constructor that attaches it to a single container object, and that the iterator is not attached to a different container during its lifetime.
  • the goal of iterator is for every iterator in your program to have the same interface so that any code that uses the iterator doesn’t care what it’s pointing to
  • container needs some way to produce the iterators indicating the beginning of the sequence and the end sentinel – member function begin() and end()