| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"George2" |
| Date: |
04 Jan 2008 09:08:01 AM |
| Object: |
STL sort throw exception? |
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
.
|
|
| User: "Victor Bazarov" |
|
| Title: Re: STL sort throw exception? |
04 Jan 2008 09:21:01 AM |
|
|
George2 wrote:
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.
Since no limitation is put on the comparison functor (whether the
default 'std::less' or the user-defined one), so it may throw, and
the exception will be propagated, i.e. 'sort' does not attempt to
catch it.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
|
|
|
|
| User: "" |
|
| Title: Re: STL sort throw exception? |
04 Jan 2008 09:18:55 AM |
|
|
On Jan 4, 10:08=A0am, George2 <george4acade...@yahoo.com> 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.
thanks in advance,
George
When the sort moves elements around, the copy constructor or the
assignment operator of the objects being sorted may throw. It
escapes me at the moment which method (copy construction or
assignemt) is used in the sort algorithms.
HTH
.
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: STL sort throw exception? |
04 Jan 2008 09:23:51 AM |
|
|
wrote:
On Jan 4, 10:08 am, George2 <george4acade...@yahoo.com> 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.
thanks in advance,
George
When the sort moves elements around, the copy constructor or the
assignment operator of the objects being sorted may throw. It
escapes me at the moment which method (copy construction or
assignemt) is used in the sort algorithms.
Shouldn't it actually be 'swap'?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
|
|
|
| User: "Howard Hinnant" |
|
| Title: Re: STL sort throw exception? |
04 Jan 2008 01:55:36 PM |
|
|
In article <fllj28$al3$1@news.datemas.de>,
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote:
AnonMail2005@gmail.com wrote:
On Jan 4, 10:08 am, George2 <george4acade...@yahoo.com> 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.
thanks in advance,
George
When the sort moves elements around, the copy constructor or the
assignment operator of the objects being sorted may throw. It
escapes me at the moment which method (copy construction or
assignemt) is used in the sort algorithms.
Shouldn't it actually be 'swap'?
The C++03 sort doesn't specify. In practice implementations use all of:
swap
copy construction
copy assignment.
The C++0X working paper currently has for sort:
Requires: The type of *first shall satisfy the Swappable
requirements (37), the MoveConstructible requirements
(Table 33), and the the MoveAssignable requirements (Table 35).
This will allow sorting sequences of "move only" types such as fstream
and unique_ptr.
With respect to the original question: sort can still throw in C++0X by
similar means: swap, move construct, move assign, comparison. If it
does, the basic guarantee is in effect: no memory leaks or corruption.
-Howard
.
|
|
|
|
|
|

|
Related Articles |
|
|