George
Sat Jan 05 04:25:00 CST 2008
Thanks Alex,
1.
> `std::sort' wont allocate, but copy constructor and/or assignment
> operator of contained class may allocate and throw.
I agree. Great!
2.
> std::vector<X> vx;
> std::sort(vx.begin(), vx.end(), EvilSort);
I do not agree. I do not mean your code is not correct. But (my fault) not
conforming to Bjarne's points. It is my fault to forget to post the sample by
Bjarne.
Here it is, in the 1st sample.
http://www.research.att.com/~bs/3rd_safe.pdf
regards,
George
"Alex Blekhman" wrote:
> "George" wrote:
> > 1. In what situation, comparison predicate throws?
>
> In any situation it sees fit:
>
> bool EvilSort(const X& x1, const X& x2)
> {
> if(x1.IsNaughty())
> throw "Mwa-ha-ha-ha!";
>
> return false;
> }
>
> std::vector<X> vx;
> std::sort(vx.begin(), vx.end(), EvilSort);
>
> > 2.
> >
> >> How 'bout if memory allocation fails, for example?
> >
> > I do not agree that sort needs to allocate any memory using
> > new/new[], which
> > will cause bad_alloc exception.
>
> `std::sort' wont allocate, but copy constructor and/or assignment
> operator of contained class may allocate and throw.
>
> Alex
>
>
>