== Quote from Andrei Alexandrescu ([email protected])'s article > Jonathan M Davis wrote: > > On Monday, August 09, 2010 16:40:23 Andrei Alexandrescu wrote: > >> class File { > >> FILE * fp; > >> this() { fp = fopen("/tmp/temporary"); } > >> ~this() { fclose(fp); } > >> } > >> > >> The destructor does not test fp because it assumes it was opened. > >> > >> Interestingly enough, by TDPL the code above is in fact invalid already. > >> TDPL mentions that an object's lifetime starts as soon as it's been > >> branded, which is before default construction. As a direct consequence, > >> the destructor should be able to deal with an object stamped with all > >> init values for all fields - in this case a null fp. > > > > I would take that as an argument for making clear() set the object in the state > > prior to the constructor call. > [snip] > I agree. Do others? > Andrei
A clear() that leaves the object in the initial (not constructed) state is the most sensible choice. This is good for two reasons: 1. it avoids useless resources reallocation and 2. it leaves the destroyed object in a (somewhat) safe and identifiable state. One could easily make his own reset() function that calls clear() and then the constructor, if needed.
