I'm implementing a generic stack (with an array) in C++ and am confused about what to return in this situation:
template <class T>
T Stack<T>::pop(void) {
if (size != 0) {
return items[size - 1];
size--;
} else {
cerr << "Cannot pop from empty stack." << endl;
return ???;
}
}
template <class T>
T Stack<T>::peek(void) {
if (size != 0)
return items[size - 1];
else {
cerr << "Cannot peek from empty stack." << endl;
return ???;
}
}
What are my options here? I think it would be messy to do something like declaring a new T variable and returning it. I'm drawing a blank.
This depends on what you want the behaviour (protocol) of your class to be. Since you're logging into the error stream there, I assume you consider this an error condition to call
pop()
on an empty stack. The standard C++ way of signalling errors is to throw an exception. Something like this:An alternative would be to say that
pop()
has a precondition "stack is not empty." Violation of a precondition is generally undefined behaviour, so you could simply assume the stack is not empty. This is the useful approach for performance-critical code:The above two approaches assume that calling
pop()
on an empty stack is an error, i.e. that it shouldn't happen. If you instead want that to be a valid operation with a well-defined result, you have a few other options.Return a flag indicating success:
Return a
boost::optional
:Return a default-constructed
T
: