What should the ‘pop()’ method return when the stack is empty? [duplicate]

The programming-by-contract style would be that having a non-empty stack is a precondition of calling pop, and that calling a method without meeting its preconditions has an undefined outcome. My implementation would throw a std::logic_error, but that would not be required. In C, my implementation would abort via assert.

The caller of popis responsible for ensuring that the precondition that the stack is not empty holds before calling pop. The stack should therefore have an isEmpty method for the caller to check.

Leave a Comment