C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

problem with definition of pop_back()
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
John Vincent
Guest





PostPosted: Thu May 19, 2005 2:11 am    Post subject: problem with definition of pop_back() Reply with quote



Hi,

I have examined both the first and the second edition of the C++ standard,
and as far as I can see the pop_back() method is not properly defined. I
cannot find anything in the standard that specifies either that it removes
an item from a container, or which item it removes.

Is this an error, or did I miss something?

Thanks
/John Vincent.


---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
James Dennett
Guest





PostPosted: Thu May 19, 2005 8:35 pm    Post subject: Re: problem with definition of pop_back() Reply with quote



John Vincent wrote:

Quote:
Hi,

I have examined both the first and the second edition of the C++
standard, and as far as I can see the pop_back() method is not properly
defined. I cannot find anything in the standard that specifies either
that it removes an item from a container, or which item it removes.

Is this an error, or did I miss something?

Table 68 defines the operational semantics of pop_back; were
you looking for additional definitions, or is this sufficient?

-- James

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Seungbeom Kim
Guest





PostPosted: Thu May 19, 2005 8:35 pm    Post subject: Re: problem with definition of pop_back() Reply with quote



John Vincent wrote:
Quote:

I have examined both the first and the second edition of the C++
standard, and as far as I can see the pop_back() method is not properly
defined. I cannot find anything in the standard that specifies either
that it removes an item from a container, or which item it removes.

Is this an error, or did I miss something?

The operational semantics of a.pop_back() is defined as
a.erase(--a.end()), and it is defined for vector, list, and deque.
See Table 68 ("Optional sequence operations") in 23.1.1.

--
Seungbeom Kim

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Bronek Kozicki
Guest





PostPosted: Thu May 19, 2005 8:35 pm    Post subject: Re: problem with definition of pop_back() Reply with quote

"John Vincent" <jpv50 (AT) hotmail (DOT) com> wrote:
Quote:
I have examined both the first and the second edition of the C++
standard, and as far as I can see the pop_back() method is not
properly defined.

in fact, it is not defined at all. This is by design - trying to do two
operations at once (copy last element to function result and then remove
it from top of stack) is bad design (especially from exception safety
POV). This has been discussed in literature, e.g. Herb Sutter
"Exceptional C++"


B.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Alberto Barbati
Guest





PostPosted: Thu May 19, 2005 8:35 pm    Post subject: Re: problem with definition of pop_back() Reply with quote

John Vincent wrote:
Quote:
Hi,

I have examined both the first and the second edition of the C++
standard, and as far as I can see the pop_back() method is not properly
defined. I cannot find anything in the standard that specifies either
that it removes an item from a container, or which item it removes.

Is this an error, or did I miss something?


Have a look to table 68, in 23.1.1.

Alberto

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Clark S. Cox III
Guest





PostPosted: Thu May 19, 2005 8:36 pm    Post subject: Re: problem with definition of pop_back() Reply with quote

On 2005-05-18 22:11:54 -0400, [email]jpv50 (AT) hotmail (DOT) com[/email] ("John Vincent") said:

Quote:
Hi,

I have examined both the first and the second edition of the C++
standard, and as far as I can see the pop_back() method is not properly
defined. I cannot find anything in the standard that specifies either
that it removes an item from a container, or which item it removes.

Is this an error, or did I miss something?

From 23.1.1 [lib.sequence.reqmts]
--------------

12 The operations in Table 68 are provided only for the containers for
which they take constant time:
--------------

And, from said table:
--------------
expression | return type | operational semantics | container
.. | ... | ... | ...
a.pop_back() | void | a.erase(--a.end()) | vector, list, deque
--------------


--
Clark S. Cox, III
[email]clarkcox3 (AT) gmail (DOT) com[/email]

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
kuyper@wizard.net
Guest





PostPosted: Thu May 19, 2005 9:35 pm    Post subject: Re: problem with definition of pop_back() Reply with quote


"John Vincent" wrote:
Quote:
Hi,

I have examined both the first and the second edition of the C++
standard,
and as far as I can see the pop_back() method is not properly
defined. I
cannot find anything in the standard that specifies either that it
removes
an item from a container, or which item it removes.

Is this an error, or did I miss something?

You are correct that the definitions of list<>::pop_back(),
deque::pop_back() and vector<>::pop_back() do not specify those things.
However, what you missed is the implications of the fact that 23.2.1p2,
23.2.2p2 and 23.2.4p2 all specify that those containers satisfy "most
of the optional sequence requirements (23.1.1)".

If you look at 23.1.1p12 you'll find Table 68, which describes the
optional sequence requirements. The requirements are optional, because
the operations they specify are mandotory only when they have constant
complexity. It says that for pop_back() this is the case for list,
deque, and vector. It specifies that a.pop_back() is equivalent to
a.erase(--a.end()).

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
David Abrahams
Guest





PostPosted: Fri May 20, 2005 2:49 am    Post subject: Re: problem with definition of pop_back() Reply with quote

[email]brok (AT) rubikon (DOT) pl[/email] ("Bronek Kozicki") writes:

Quote:
"John Vincent" <jpv50 (AT) hotmail (DOT) com> wrote:
I have examined both the first and the second edition of the C++
standard, and as far as I can see the pop_back() method is not
properly defined.

in fact, it is not defined at all. This is by design - trying to do
two operations at once (copy last element to function result and
then remove it from top of stack) is bad design (especially from
exception safety POV). This has been discussed in literature,
e.g. Herb Sutter "Exceptional C++"


1. Of course pop_back is defined -- in fact, it's defined
for all the containers in the standard.

2. Almost every useful function is a composite of multiple
sub-operations. Labelling that "bad design" is not only dogmatic,
it's silly.

3. The issue you're thinking of was raised by Tom Cargill's Article:
"Exception Handling: A False Sense of Security"
(http://tinyurl.com/9tnnk) or
(http://www.awprofessional.com/content/images/020163371x/supplements/Exception_Handling_Article.html)
It was improperly labelled a safety problem. The interface as
described is simply unable to provide more than the basic
guarantee. There's nothing wrong with that in principle, though
it's true that in this case it makes sense to also supply a
separate top() function. The assumption that every operation must
give the strong guarantee in order to be exception safe is
widespread, but incorrect. In general, the strong guarantee is
neither sufficient, nor is it always neccessary, for exception
safety.

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Howard Hinnant
Guest





PostPosted: Fri May 20, 2005 4:10 am    Post subject: Re: problem with definition of pop_back() Reply with quote

In article <BAY101-F25F93A3613EF628DD56370A6170 (AT) phx (DOT) gbl>,
[email]jpv50 (AT) hotmail (DOT) com[/email] ("John Vincent") wrote:

Quote:
Hi,

I have examined both the first and the second edition of the C++ standard,
and as far as I can see the pop_back() method is not properly defined. I
cannot find anything in the standard that specifies either that it removes
an item from a container, or which item it removes.

Is this an error, or did I miss something?

The table in 23.1.1p12 says that the operational semantics of
a.pop_back() is a.erase(--a.end()).

-Howard

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Pete Becker
Guest





PostPosted: Fri May 20, 2005 4:10 am    Post subject: Re: problem with definition of pop_back() Reply with quote

John Vincent wrote:

Quote:

I have examined both the first and the second edition of the C++
standard, and as far as I can see the pop_back() method is not properly
defined. I cannot find anything in the standard that specifies either
that it removes an item from a container, or which item it removes.

Is this an error, or did I miss something?


The table entitled "Optional Sequence Operations" (I think it's 68 in
the official ISO versions, but it's 69 in the latest working draft)
gives the specification as { iterator tmp = a.end(); --tmp; a.erase(tmp); }

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Bronek Kozicki
Guest





PostPosted: Fri May 20, 2005 4:10 am    Post subject: Re: problem with definition of pop_back() Reply with quote

Bronek Kozicki wrote:
Quote:
in fact, it is not defined at all. This is by design - trying to do two
operations at once (copy last element to function result and then remove
it from top of stack) is bad design (especially from exception safety
POV). This has been discussed in literature, e.g. Herb Sutter
"Exceptional C++"

oooops. I do not know how it happend, but when writing above I was sure
that you are asking about std::stack.


B.

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
John Vincent
Guest





PostPosted: Fri May 20, 2005 7:22 pm    Post subject: Re: problem with definition of pop_back() Reply with quote

Hi,

Thanks to everyone who pointed out Table 68. For some reason I'd missed it
?!?

/John Vincent.

Quote:
From: Ray Lischner To: John Vincent Subject: Re: problem with definition of pop_back()
Date: Thu, 19 May 2005 17:15:51 -0400

On Wednesday 18 May 2005 10:11 pm, John Vincent wrote:

... as far as I can see the pop_back() method is not
properly defined.

Section 23.1.1, Table 68.
--
Ray Lischner, author of C++ in a Nutshell
http://www.tempest-sw.com/cpp


---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Joe Gottman
Guest





PostPosted: Fri May 20, 2005 9:59 pm    Post subject: Re: problem with definition of pop_back() Reply with quote


"David Abrahams" <dave (AT) boost-consulting (DOT) com> wrote

Quote:
brok (AT) rubikon (DOT) pl ("Bronek Kozicki") writes:

"John Vincent" <jpv50 (AT) hotmail (DOT) com> wrote:
I have examined both the first and the second edition of the C++
standard, and as far as I can see the pop_back() method is not
properly defined.

in fact, it is not defined at all. This is by design - trying to do
two operations at once (copy last element to function result and
then remove it from top of stack) is bad design (especially from
exception safety POV). This has been discussed in literature,
e.g. Herb Sutter "Exceptional C++"


3. The issue you're thinking of was raised by Tom Cargill's Article:
"Exception Handling: A False Sense of Security"
(http://tinyurl.com/9tnnk) or

(http://www.awprofessional.com/content/images/020163371x/supplements/Exception_Handling_Article.html)
It was improperly labelled a safety problem. The interface as
described is simply unable to provide more than the basic
guarantee. There's nothing wrong with that in principle, though
it's true that in this case it makes sense to also supply a
separate top() function. The assumption that every operation must
give the strong guarantee in order to be exception safe is
widespread, but incorrect. In general, the strong guarantee is
neither sufficient, nor is it always neccessary, for exception
safety.

If the rvalue reference proposal is accepted, then pop_back() might be
able to use move() to return the top element by rvalue reference. This
would be much safer, though not 100% safe, as move constructors and move
assignment operators will probably be nothrow. Java stacks return their top
element with move semantics when they call pop(), and there is no risk of
them throwing.

Joe Gottman

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
kuyper@wizard.net
Guest





PostPosted: Fri May 20, 2005 11:00 pm    Post subject: Re: problem with definition of pop_back() Reply with quote

"Bronek Kozicki" wrote:
Quote:
"John Vincent" <jpv50 (AT) hotmail (DOT) com> wrote:
I have examined both the first and the second edition of the C++
standard, and as far as I can see the pop_back() method is not
properly defined.

in fact, it is not defined at all. This is by design - trying to do
two
operations at once (copy last element to function result and then
remove
it from top of stack) is bad design (especially from exception safety

POV). This has been discussed in literature, e.g. Herb Sutter
"Exceptional C++"

The pop_back() member is specified in Table 68 as having no return
value. What are you talking about?

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
James Dennett
Guest





PostPosted: Sat May 21, 2005 9:22 pm    Post subject: Re: problem with definition of pop_back() Reply with quote

Joe Gottman wrote:

Quote:
"David Abrahams" <dave (AT) boost-consulting (DOT) com> wrote in message
news:u3bsig3nz.fsf (AT) boost-consulting (DOT) com...

[email]brok (AT) rubikon (DOT) pl[/email] ("Bronek Kozicki") writes:


"John Vincent" <jpv50 (AT) hotmail (DOT) com> wrote:

I have examined both the first and the second edition of the C++
standard, and as far as I can see the pop_back() method is not
properly defined.

in fact, it is not defined at all. This is by design - trying to do
two operations at once (copy last element to function result and
then remove it from top of stack) is bad design (especially from
exception safety POV). This has been discussed in literature,
e.g. Herb Sutter "Exceptional C++"


3. The issue you're thinking of was raised by Tom Cargill's Article:
"Exception Handling: A False Sense of Security"
(http://tinyurl.com/9tnnk) or

(http://www.awprofessional.com/content/images/020163371x/supplements/Exception_Handling_Article.html)
It was improperly labelled a safety problem. The interface as
described is simply unable to provide more than the basic
guarantee. There's nothing wrong with that in principle, though
it's true that in this case it makes sense to also supply a
separate top() function. The assumption that every operation must
give the strong guarantee in order to be exception safe is
widespread, but incorrect. In general, the strong guarantee is
neither sufficient, nor is it always neccessary, for exception
safety.


If the rvalue reference proposal is accepted, then pop_back() might be
able to use move() to return the top element by rvalue reference. This
would be much safer, though not 100% safe, as move constructors and move
assignment operators will probably be nothrow. Java stacks return their top
element with move semantics when they call pop(), and there is no risk of
them throwing.

Note that Java stacks don't return things with move semantics;
the object is never part of the stack, but rather is allocated
separately on the heap, and when it is popped it doesn't move,
but rather a pointer (reference, in Java-speak) to it is returned
to the caller and the stack ceases to reference it. Copying a
pointer (reference) is a no-throw operation, so that's all fine;
you just pay the penalties of another level of indirection
compared to a value-based C++ stack. The complications in C++
come from allowing values to be stored inside the stack, rather
than having the stack just refer to objects elsewhere.

-- James

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.