Re: Are throwing default constructors bad style, and if so, why?
Dave Harris wrote:
SeeWebsiteForEmail@erdani.org (Andrei Alexandrescu) wrote (abridged):
I agree. So let me restart. Do you agree that in a GC system, there
should be _some_ constructor that doesn't throw (that being the
constructor called when the object enters the destroyed state)?
I don't agree with this. At least, not if you are calling it a
"constructor".
I think there should be a "disposed" state, but I don't see why there has
to be a constructor that enters it directly. A newly-constructed object
should be ready to use; a disposed object need not be.
I see resurrecting or recycling disposed objects as a special, and often
dangerous, technique, rather than standard best practice.
Upon reflection, I agree.
But I hope you'll also agree that an object holding a scarce resource
should have a no-throw "enter disposed state" operation that is
performed during destruction and after resource disposal? Do you also
agree that that state should be checked for in all of that object's
member functions?
I tried to simplify things and make that state identical with the
default-constructed state. It turns out that that is a bad idea.
Andrei
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]