Hello everyone,


In Bjarne's book, it is mentioned that sort of STL may throw exception, like
sorting elements in a vector.

In what situation will sort throw exception? I can not find a case.


thanks in advance,
George

Re: STL sort throw exception? by Carl

Carl
Fri Jan 04 10:08:44 CST 2008

George wrote:
> Hello everyone,
>
>
> In Bjarne's book, it is mentioned that sort of STL may throw
> exception, like sorting elements in a vector.
>
> In what situation will sort throw exception? I can not find a case.

"May throw" means "allowed to throw" not "does throw".

What if the comparison predicate throws, for example? How 'bout if memory
allocation fails, for example?

-cd



Re: STL sort throw exception? by George

George
Sat Jan 05 02:53:00 CST 2008

Thanks cd,


1. In what situation, comparison predicate throws?

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.


regards,
George

"Carl Daniel [VC++ MVP]" wrote:

> George wrote:
> > Hello everyone,
> >
> >
> > In Bjarne's book, it is mentioned that sort of STL may throw
> > exception, like sorting elements in a vector.
> >
> > In what situation will sort throw exception? I can not find a case.
>
> "May throw" means "allowed to throw" not "does throw".
>
> What if the comparison predicate throws, for example? How 'bout if memory
> allocation fails, for example?
>
> -cd
>
>
>

Re: STL sort throw exception? by Alex

Alex
Sat Jan 05 04:16:16 CST 2008

"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



Re: STL sort throw exception? by George

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
>
>
>

Re: STL sort throw exception? by Alex

Alex
Sat Jan 05 04:38:38 CST 2008

"George" wrote:
>> 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

My code is essentially the same. I could provide `operator <'
instead of `EvilSort' function:

bool operator <(const X& x1, const X& x2)
{
...
throw "Error";
}

std::sort(vx.begin(), vx.end());

Bjarne's point is that `std::sort' must be ready to the fact that
user provided functions might throw.

Alex



Re: STL sort throw exception? by George

George
Sat Jan 05 04:56:01 CST 2008

Thanks for your sample, Alex!


My question is answered.


regards,
George

"Alex Blekhman" wrote:

> "George" wrote:
> >> 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
>
> My code is essentially the same. I could provide `operator <'
> instead of `EvilSort' function:
>
> bool operator <(const X& x1, const X& x2)
> {
> ...
> throw "Error";
> }
>
> std::sort(vx.begin(), vx.end());
>
> Bjarne's point is that `std::sort' must be ready to the fact that
> user provided functions might throw.
>
> Alex
>
>
>