From: Pete Becker 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.
>

T t = x.pop();

Suppose pop() throws away the internal data element. Now, if the copy
operation throws an exception, the program has lost data with no way to
get it back.

If pop() doesn't throw away the element, the program can recover from
that exception without losing data.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)

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

From: Thomas Maeder on
Khan <chengiz(a)my-deja.com> writes:

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

Cf.
http://ptgmedia.pearsoncmg.com/images/020163371x/supplements/Exception_Handling_Article.html

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

From: Thomas Richter on
Khan wrote:
> Why is there no variant of pop() in the standard library that returns
> the top element?

Because it wouldn't be exception safe. For the details, look in Herb
Sutter's "Exceptional C++", but in a nutshell, what would happen if the
copy-constructor of the class to be popped would fail? The container
will have the element be removed already, but the copy of the top
element failed to copy, so you've lost the element to remove from the stack.

So long,
Thomas

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

From: shahav on
On Oct 31, 12:33 am, 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

{ edits: quoted banner removed. please don't quote the banner. -mod }

STL has mainly atomic operations, whenever an "application" level
operation was added it seems to be a wrong decision.e.g. map []
operator, is it a bad compound operation.
The pattern you are referring as passing an pre-constructed obj & and
filling it, was never taken in STL, it requires operator= and bad
return value in case the container is empty, it is way too complex.
I never had the need to add your pop.

Rabin


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

From: Khan on
For everybody that suggested the exception-safe answer, that applies
to T pop(), not to bool pop(T&). I just read Sutter where he addresses
this issue, and in fact the relevant Item mentions that bool pop(T&)
doesnt have the exception-safety issues. He says that the Real Problem
with this design is that pop has two responsibilities: pop and return.
I say, so what?

@Neil: T does not require a default ctor. My app is constructing the
T, not any standard library function (unless I'm missing something).

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