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
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
Good!
A search in Mingw implementation of algorithms didn't turn up any instances of "throw".
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
It is possible that this wording is for precautions only - allowing sort to throw an exception allows implementations to CHOOSE whether they throw an exception under some circumstances or not, whilst stating that it can't limits the implementation to only such ways that can not throw an exception. Just like there are plenty of compilers that allow things that are undefined by the C or C++ standard - because that particular compiler or C runtime has a suitable solution for this undefined behaviour.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I haven't looked at that passage, but he could very well be referring to an exception being thrown while sort is being run, not necessarily that the sort function itself throws an exception.
A simple example is if the custom sorting algorithm you pass to sort can throw an exception, or if the copy assignment operator for your class throws an exception.
Assignment operators are allowed to throw, and vector sorting definitely involves doing that, therefore it has to be able to throw. Otherwise you'd get a partially sorted vector as a result and no notification that it is so.
However it probably doesn't actually throw new exceptions itself, but rather it would catch and rethrow any user-thrown exceptions. If your operators don't throw, then the sort shouldn't throw.
I think this all answers the original question as well.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
If you call foo_1 then *an exception will be thrown while foo_1 is being run* but if you call foo_2 then the *foo_2 function itself throws an exception*.Code:void bar(int i) { if (i == 0) throw std::exception(); // do work } void foo_1(int i) { bar(i); } void foo_2(int i) { if (i == 0) throw std::exception(); bar(i); }