Topic: STL pop_back() question
Author: "Bill Wade" <bill.wade@stoner.com>
Date: 1999/01/15 Raw View
Siemel Naran wrote in message ...
>On 13 Jan 1999 21:50:50 GMT, Nathan Myers <ncm@nospam.cantrip.org> wrote:
>>Steve Strauss <steve.c.strauss@lmco.com> wrote:
>>>Why doesn't pop_back() returned what was popped?
>>Exception-safety
>Can you please explain further?
For pop_back() to return the top element it would need code like
T stack<T>::pop()
{
T result = c.back();
c.pop_back();
return result;
}
The return statement could invoke T's copy constructor, which might throw an
exception. If it does, there is no way to recover the element since it has
already been removed from c.
Personally I believe that the efficiency argument is the real reason
pop_back doesn't return anything. Efficiency is the argument used in Matt
Austern's "Generic Programming and the STL".
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Steve Strauss <steve.c.strauss@lmco.com>
Date: 1999/01/13 Raw View
Why doesn't pop_back() returned what was popped?
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1999/01/13 Raw View
Steve Strauss <steve.c.strauss@lmco.com> wrote:
>
>Why doesn't pop_back() returned what was popped?
Exception-safety. It is not possible to implement a traditional
one-step pop operation that is guaranteed to leave the stack in
a coherent state. This is not the original reason why it was
implemented that way, but is why it cannot be changed. The original
reason is that in real algorithms it is more efficient to use with
the interface given.
The STL was designed with low-level efficiency always in mind.
This is only intermittently a consideration in algorithm texts.
--
Nathan Myers
ncm@nospam.cantrip.org http://www.cantrip.org/
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Andrei Alexandrescu" <alexandrescua@micromodeling.com>
Date: 1999/01/14 Raw View
I've read that one reason is because this way exception safety cannot be
enforced (if the constructor of the object throws).
Andrei
Steve Strauss wrote in message <369CB3CD.9AC764BD@lmco.com>...
>Why doesn't pop_back() returned what was popped?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/01/14 Raw View
On 13 Jan 1999 21:50:50 GMT, Nathan Myers <ncm@nospam.cantrip.org> wrote:
>Steve Strauss <steve.c.strauss@lmco.com> wrote:
>>Why doesn't pop_back() returned what was popped?
>Exception-safety. It is not possible to implement a traditional
>one-step pop operation that is guaranteed to leave the stack in
>a coherent state. This is not the original reason why it was
>implemented that way, but is why it cannot be changed. The original
>reason is that in real algorithms it is more efficient to use with
>the interface given.
I understand reason two, which is how my own stack, queue, and heap
classes do it. Because code using pop or pop_back is usually like
this:
while (!stack.empty()) { /*stuff*/ stack.pop(); }
The return value of pop() is not used, so we've saved a call to
the copy constructor T::T(const T&) by not returning a T.
But reason one doesn't make sense to me. Can you please explain
further?
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]