Search code examples
c++stacknullptr

How to protect user if can't return nullptr for an empty stack?


I am trying to code the implementation of the Cube, Stack example given in this Coursera course example of Towers of Hanoi to learn more C++.

In stack.h I have to implement:

class Stack {
public:
void push_back(const Cube & cube);
Cube removeTop();
Cube & peekTop();
unsigned size() const;

friend std::ostream & operator<<(std::ostream & os, const Stack & stack);

private:
std::vector<Cube> cubes_;
};

The issue I have is with removeTop(). I was thinking of returning nullptr if the stack (vector) is empty because pop_back's behavior is undefined for an empty vector.

Calling pop_back on an empty container is undefined. Cpp Reference

inline Cube Stack::removeTop() {
  if (!cubes_.empty()) {
    Cube top_cube = cubes_.back();
    cubes_.pop_back();
    return top_cube;
  }
  else {
    return nullptr;
  }
}

However, I get an error during compilation.

./stack.h:35:12: error: no viable conversion from returned value of type
      'std::__1::nullptr_t' to function return type 'uiuc::Cube'
    return nullptr;

How can I protect the user if I can't return a nullptr? Am I limited to just telling the user that the function should not be called on an empty stack and let him/her take care of the checking?


Solution

  • This is exactly what exceptions are for:

    if (cubes_.empty())
        throw std::runtime_error("stack underflow");
    Cube top_cube = cubes_.back();
    cubes_.pop_back();
    return top_cube;
    

    Complicating this with std::optional is almost certainly not the right answer here. Attempting to pop from an empty stack means the program has lost its way. That should be a hard error, not something that's masked by an interface that says "you might or might not have this, please check afterwards".