Item 56. Policies

In Specializing for Type Information [52, 183], we designed a stack template that deleted any remaining elements left in the stack at the end of the stack’s lifetime if the stack’s element type was a pointer.

template <typename T> class Stack;

This is not an unreasonable policy, but it is inflexible. There may be cases where the user of our stack does not want to delete what the stack’s pointers refer to. For instance, the pointers may refer to objects that are not on the heap or that are shared with other containers. Additionally, it’s possible that a pointer refers to an array of objects, rather than a single object. If we have a stack of character pointers, this is almost certainly the case, since character pointers usually ...

Get C++ Common Knowledge: Essential Intermediate Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.