From: Khan on
Why is there no variant of pop() in the standard library that returns
the top element? I can understand why there is a void pop() - for
efficiency. But why isnt there also a variant that returns the top
element - bool pop(T&), so that those who want to use it can? It'd be
so much simpler to write a routine while (x.pop(t)) { } rather than
while (x.empty()) { t = x.top(); x.pop(); } and I suppose the
atomicity would help MT etc operations too.

The one reason I can think of (Dont give a way to the user to do
inefficient things) has counterarguments:
- A lot of people are going to write T t = x.pop() anyway. Cant
prevent that.
- Prevent user from shooting themselves in the foot is not C++
philosophy.
- It may not be inefficient in many cases, so why not leave it to the
user?

I'm sure this must have been asked before, but I guess my searchfu is
inadequate, sorry. Thanks in advance,
chengiz

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Neil Butterworth on
Khan wrote:
> Why is there no variant of pop() in the standard library that returns
> the top element? I can understand why there is a void pop() - for
> efficiency. But why isnt there also a variant that returns the top
> element - bool pop(T&), so that those who want to use it can? It'd be
> so much simpler to write a routine while (x.pop(t)) { } rather than
> while (x.empty()) { t = x.top(); x.pop(); } and I suppose the
> atomicity would help MT etc operations too.

The general reason there isn't a pop() function is because such a
function cannot be exception safe. This is discussed in (among other
places) Herb Sutter's Exceptional C++ books.

As for why no bool pop(T&), this would require that the class T had a
default constructor. so that you could say:

T t;
stack.pop( t );

and C++ generally dislikes classes and templates that need a default
constructor.

Neil Butterworth


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Nick Hounsome on
On 30 Oct, 22:33, Khan <chen...(a)my-deja.com> wrote:
> Why is there no variant of pop() in the standard library that returns
> the top element? I can understand why there is a void pop() - for
> efficiency. But why isnt there also a variant that returns the top
> element - bool pop(T&), so that those who want to use it can? It'd be
> so much simpler to write a routine while (x.pop(t)) { } rather than
> while (x.empty()) { t = x.top(); x.pop(); } and I suppose the
> atomicity would help MT etc operations too.
>
> The one reason I can think of (Dont give a way to the user to do
> inefficient things) has counterarguments:
> - A lot of people are going to write T t = x.pop() anyway. Cant
> prevent that.
> - Prevent user from shooting themselves in the foot is not C++
> philosophy.
> - It may not be inefficient in many cases, so why not leave it to the
> user?
>
> I'm sure this must have been asked before, but I guess my searchfu is
> inadequate, sorry. Thanks in advance,
> chengiz

Because a method with more than one effect cannot be gauranteed to be
correct in the face of exceptions.

pop() can be implemented so that it never throws (unless ~T() does but
it is a fundamental rule that destructors must not throw)
top() can be implemented so that x is unchanged if the assignment
failed. i.e. the element that you wanted still exists somewhere.

but

The 2 operations cannot be done together atomically so if T::operator=
(const T&) throws you can end up with a changed collection and no copy
anywhere of the old top.

Personaly I agree that this seems pretty hypothetical as I rarely have
classes whose assignment can fail (except through memory exhaustion
which is invariably fatal anyway) and when I do they are rarely held
in collections by value.

IMHO there could be an extra method "bool pop_value(T&)" that
explicitly states that the assignment is done first.



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Matthew Collett on
In article
<469a2dfd-8b15-474f-baa1-e24adb8d7fa6(a)d34g2000vbm.googlegroups.com>,
Khan <chengiz(a)my-deja.com> wrote:

> Why is there no variant of pop() in the standard library that returns
> the top element?

Because of the difficulty of making such a pop() exception safe. For
example, what happens if the copy constructor making the returned
element throws? That element has already been removed from the stack,
and so will be lost. The issue is discussed in detail in Item 10 of
Exceptional C++.

Best wishes,
Matthew

--
http://homepages.ihug.co.nz/~m_collett

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: peter koch larsen on
On 30 Okt., 23:33, Khan <chen...(a)my-deja.com> wrote:
> Why is there no variant of pop() in the standard library that returns
> the top element? I can understand why there is a void pop() - for
> efficiency. But why isnt there also a variant that returns the top
> element - bool pop(T&), so that those who want to use it can? It'd be
> so much simpler to write a routine while (x.pop(t)) { } rather than
> while (x.empty()) { t = x.top(); x.pop(); } and I suppose the
> atomicity would help MT etc operations too.
>
> The one reason I can think of (Dont give a way to the user to do
> inefficient things) has counterarguments:
> - A lot of people are going to write T t = x.pop() anyway. Cant
> prevent that.
> - Prevent user from shooting themselves in the foot is not C++
> philosophy.
> - It may not be inefficient in many cases, so why not leave it to the
> user?
>
> I'm sure this must have been asked before, but I guess my searchfu is
> inadequate, sorry. Thanks in advance,
> chengiz

Because you can't pop safely in an exception-safe manner.

/Peter


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]