Why a try/catch in priority_queue::push/pop?!?
Howard Hinnant
hhinnant@apple.com
Thu Aug 3 19:36:00 GMT 2006
On Aug 3, 2006, at 2:42 PM, Paolo Carlini wrote:
> Hi,
>
> today I noticed by chance these strange things: for example, push
> (likewise pop):
>
> void
> push(const value_type& __x)
> {
> try
> {
> c.push_back(__x);
> std::push_heap(c.begin(), c.end(), comp);
> }
> catch(...)
> {
> c.clear();
> __throw_exception_again;
> }
> }
>
> why that try/catch? Certainly I cannot find support in the Standard
> for its presence and, AFAICS, can also run against reasonable user
> expectations: i.e., if c.push_back throws (e.g., bad_alloc) the
> user may reasonably expect that c remains unchanged! The case of
> push_heap is different, more complex, but again, I don't think the
> user, basing on the letter of the Standard can generally (i.e.,
> portably) expect that a clear() will be invoked (supposedly to do
> him the "favor" of a "consistent", read, empty! , container)
>
> I think the try/catch is very old, coming from the HP/SGI STL,
> maybe I missing historical facts which I'd like to know... Anyone
> helping?
<shakes head> Can't blame this one on HP/SGI, just checked, no-try/
catch there. :-)
I agree with your analysis of the code and the standard. I think the
user could reasonably expect priority_queue<int>::push to have the
strong exception guarantee, though I note that the standard doesn't
quite say that (that I can find).
-Howard
More information about the Libstdc++
mailing list