libstdc++ Digest 3 Aug 2006 18:42:06 -0000 Issue 2181

Dhruv Matani dhruvbird@gmail.com
Fri Aug 4 13:01:00 GMT 2006


>
>
>
> ---------- Forwarded message ----------
> From: Paolo Carlini <pcarlini@suse.de>
> To: libstdc++ <libstdc++@gcc.gnu.org>
> Date: Thu, 03 Aug 2006 20:42:51 +0200
> Subject: Why a try/catch in priority_queue::push/pop?!?
> 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?

IIRC, all containers are required to leave containers in a consistent
state after push & pop operations. This seems to be an implementation
of that particular requirement. IMHO, there could be 2 levels of try &
catch blocks. One for inserting into the container, and another for
re-creating the heap structure.

Somthing like:

try {
c.push_back();
} catch(...) {
throw exception again;
}

try {
push_heap();
} catch(...) {
c.clear();
}


Which means that if the container throws in push_back(), then u don't
need to clear the whole heap. I may be wrong though.

Regards,
-Dhruv.




>
> Thanks,
> Paolo.
>
>
>


-- 
   -Dhruv Matani.
http://www.geocities.com/dhruvbird/

"The biggest room is the room for improvement."
     -- Navjot Singh Siddhu.



More information about the Libstdc++ mailing list