Chapter 13. Writing Exception-Safe Code—Part 6

Difficulty: 9

And now for an even better Stack, with fewer requirements on T—not to mention a very elegant operator=().

Imagine that the /*????*/ comment in StackImpl stood for protected. Implement all the member functions of the following version of Stack, which is to be implemented in terms of StackImpl by using StackImpl as a private base class.

template <class T> 
class Stack : private StackImpl<T>
  Stack(size_t size=0);
  Stack(const Stack&);
  Stack& operator=(const Stack&);
  size_t Count() const;
  void   Push(const T&);
  T&     Top();   // if empty, throws exception
  void   Pop();   // if empty, throws exception

As always, remember to make all the functions exception-safe and exception-neutral. ...

Get Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions now with O’Reilly online learning.

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