Re: Are throwing default constructors bad style, and if so, why?
In article
<524b7d9c-ff7d-44e5-aee8-c30561f9f3ec@w39g2000prb.googlegroups.com>,
JoshuaMaurice@gmail.com wrote:
The alternative I proposed for writing "fast" portable code is to
forgo all exception use and instead use error return codes. Thus all
the initialization work cannot be done in the constructor and/or you
have zombie objects. (If you're using a third party library which does
use exceptions, create a small wrapper over it to translate exceptions
to error returns to allow the rest of your program to be exception
handling free, or just immediately catch all exceptions when you call
the third party library, treating exceptions as over-glorified error
return codes like we were all taught not to do.)
Oh, it is worse than that. You have to be careful that none of your
member variables throw on construction, either. If they can, you have
to allocate them in the heap during your second phase.
Take the following example:
struct Foo
{
std::deque<char> dc;
std::deque<long> dl;
};
Since the std::deque constructor can throw, the two phase construction
equivalent would be something like:
struct Foo
{
Foo() : p() {}
// Can throw
Foo(Foo const& that) : p(that.p ? new Data(*that.p) : 0) {}
// Can throw
Foo& operator=(Foo that)
{ swap(*this, that); return *this; }
friend void swap(Foo& lhs, Foo& rhs)
{ swap(lhs.p, rhs.p; }
bool Init()
{
if (!p)
{
try { p.reset(new Data); }
catch (...) { return false; }
}
return true;
}
struct Data
{
std::deque<char> dc;
std::deque<long> dl;
};
boost::scoped_ptr<Data> p;
};
This is:
1. Much more complicated and difficult to get right.
2. Still has the exeception cost.
3. Now has an additional heap cost, which is orders of magnitude more
expensive than the exception cost.
4. You have to deal with copy and assigment possibly throwing.
How exactly is this "fast" code? What do you do to eliminate the heap
cost?
The decision not to use exceptions pretty much has to be done from the
ground up, and it will result in a code base that is harder to get
correct, harder to maintain, and overall might end up slower.
--
Nevin ":-)" Liber <mailto:nevin@eviloverlord.com> 773 961-1620
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]