Re: Are throwing default constructors bad style, and if so, why?
David Abrahams wrote:
on Sun Sep 21 2008, Andrei Alexandrescu <SeeWebsiteForEmail-AT-erdani.org> wrote:
Also, if you sorted a vector<deque<int> > and wanted to swap the pivot
into a temporary, is it reasonable for that temporary's non-argument
initialization to throw?
Yeah, I think it's reasonable. It's worth noting that my standard
library implementation never needs to default-construct a pivot.
I gave Hoare's partition as a simple example. There are various
algorithms that do need additional O(log N) or even O(N) memory, such as
stable_sort or inplace_merge. STL has the temporary data manipulation
primitives get/return_temporary_buffer, uninitialized_copy,
uninitialized_fill, and uninitialized_fill_n. They are a bit awkward,
but they illustrate a clear need.
I'm guessing you want to get a nonthrowing sort based on nonthrowing
move and swap operations?
Yah. In general, again, I think a good programming system should never
fail to relocate objects. This is a pretty fundamental principle to me.
Of course, if you believe that "all types are Regular," it isn't even a
question: default construction, move, and swap are all nonthrowing in
that case.
I don't believe that all types should be regular, but I do think that a
non-throwing default constructor has certain advantages.
Andrei
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]