Professional C__ - Marc Gregoire [347]
Default Values for Template Type Parameters
You can give template parameters default values. For example, you might want to say that the default container for your Grid is a vector. The template class definition would look like this:
template class Grid { public: // Everything else is the same as before. }; Code snippet from GridTemplateContainer\GridDefault.h You can use the type T from the first template parameter as the argument to the vector template in the default value for the second template parameter. In versions of C++ earlier than C++11, there must be a space between the two closing angle brackets. C++11 does not require this space anymore. The C++ syntax requires that you do not repeat the default value in the template header line for method definitions. With this default parameter, clients can now instantiate a grid with or without specifying an underlying container: Grid Grid Code snippet from GridTemplateContainer\GridDefaultTest.cpp Introducing Template Template Parameters There is one problem with the Container parameter in the previous section. When you instantiate the class template, you write something like this: Grid Note the repetition of the int type. You must specify that it’s the element type both of the Grid and of the vector. What if you wrote this instead? Grid That wouldn’t work very well. It would be nice to be able to write the following, so that you couldn’t make that mistake: Grid The Grid class should be able to figure out that it wants a vector of ints. The compiler won’t allow you to pass that argument to a normal type parameter though, because vector by itself is not a type, but a template. If you want to take a template as a template parameter, you must use a special kind of parameter called a template template parameter. The syntax is crazy, but, if you’re still interested, read on. Specifying a template template parameter is sort of like specifying a function pointer parameter in a normal function. Function pointer types include the return type and parameter types of a function. Similarly, when you specify a template template parameter, the full specification of the template template parameter includes the parameters to that template. Containers in the STL have a template parameter list that looks something like this: template class vector { // Vector definition }; The E parameter is the element type. The Allocator parameter is covered in Chapter 12. Given the preceding template specification, here is the template class definition for the Grid class that takes a container template as its second template parameter: template = vector> class Grid { public: // Omitted code that is the same as before protected: Container // Omitted code that is the same as before }; Code snippet from GridTemplateContainer\GridTemplateTemplate.h What is going on here? The first template parameter is the same as before: the element type T. The second template parameter is now a template itself for a container such as vector or deque. As you saw earlier, this “template type” must take two parameters: an element type E and an allocator Allocator. Note the repetition of the word class after the nested template parameter list. The name of this parameter in the Grid template is Container (as before). The default value is now vector, instead of vector The syntax rule for a template template parameter more generically is this: template <..., template Now