Информация об изменениях

Сообщение Exception safe T Stack::pop() ? от 19.08.2018 18:47

Изменено 19.08.2018 18:50 Максим Рогожин

Exception safe T Stack::pop() ?
Привет!

Можно ли сделать exception-safe T Stack::pop()? Например, так:
template<class T>
T Stack<T>::Pop()
{
  if( vused_ == 0)
    throw "pop from empty stack";

  T result = v_[vused_-1];
  --vused_;

  try {
    return result;
  } catch (...) {
    ++vused_;
    throw;
  }
}
Exception safe T Stack::pop() ?
Привет!

Можно ли сделать exception-safe T Stack::pop()? Например, так:
template <class T> class Stack {
  T Pop();
private:
  T*     v_;      // ptr to a memory area big
  size_t vsize_;  //  enough for 'vsize_' T's
  size_t vused_;  // # of T's actually in use
};

template<class T>
T Stack<T>::Pop()
{
  if( vused_ == 0)
    throw "pop from empty stack";

  T result = v_[vused_-1];
  --vused_;

  try {
    return result;
  } catch (...) {
    ++vused_;
    throw;
  }
}