473,508 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jan 4 '08 #1
4 2217
On Jan 4, 10:08*am, George2 <george4acade...@yahoo.comwrote:
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
Jan 4 '08 #2
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
Jan 4 '08 #3
An**********@gmail.com wrote:
On Jan 4, 10:08 am, George2 <george4acade...@yahoo.comwrote:
>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
Jan 4 '08 #4
In article <fl**********@news.datemas.de>,
"Victor Bazarov" <v.********@comAcast.netwrote:
An**********@gmail.com wrote:
On Jan 4, 10:08 am, George2 <george4acade...@yahoo.comwrote:
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
Jan 4 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
458
by: Kerri | last post by:
Hi, I am new to .NET In my Error Logic on my Aspx pages when an error happens it hits my catch statement where I throw an Exception. My question is : what is the difference between Thwo...
3
3480
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
17
1930
by: hplloyd | last post by:
Hi, I have a function that adds data to a database and I want to return true if the database was updated and false if there was a problem, so I am using a try... catch block... My problem is...
6
5704
by: Arjen | last post by:
Hi, I'm reading the enterprise library documentation and there I see the throw statement. try { // run code } catch(Exception ex) {
1
2680
by: Gunjan Garg | last post by:
Hello All, I am working to create a generic datagrid which accepts a datasource(ListData - This is our own datatype) and depending on the calling program customizes itself for sorting,...
6
7840
by: Paul van Brenk | last post by:
When you run the Shuffle method often enough it will throw exception. And I can't figure out why. Anybody? Paul van Brenk the code: static void Shuffle(){ int ints = { 1, 2, 3, 4, 5, 6,...
28
2817
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
5
1944
by: George2 | last post by:
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....
6
1944
by: jason.cipriani | last post by:
Consider this program, which defines a template class who's template parameter is the type of an exception that can be thrown by members of the class: === BEGIN EXAMPLE === #include...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.