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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.