Saturday, January 18, 2014

default template arguments c++

default template arguments c++ : templates tutorial
For class templates you can also define default values for template parameters. These values are called default template arguments. They may even refer to previous template parameters. For example, in class Stack<> you can define the container that is used to manage the elements as a second template parameter, using std::vector<> as the default value:

Note that we now have two template parameters, so each definition of a member function must be defined with these two parameters:

You can use this stack the same way it was used before. Thus, if you pass a first and only argument as an element type, a vector is used to manage the elements of this type:

In addition, you could specify the container for the elements when you declare a Stack object in your program:


you declare a stack for doubles that uses a std::deque<> to manage the elements internally.

See Also:


No comments:

Post a Comment