
On 7/1/04 8:48 AM, "David Abrahams" <dave@boost-consulting.com> wrote:
Daniel Frey <daniel.frey@aixigo.de> writes: [SNIP]
I thus wrote the following small helper:
template< typename T > void reset( T& t ) { T().swap( t ); }
Would this make a good addition to boost.utility? Any comments welcome...
Not unless you make it generic:
template< typename T > void reset( T& t ) { using std::swap; T x; swap(x, t); }
Wouldn't that lead to undefined behavior as-is? There's a dirty little secret in C++ that unmentioned construction is _not_ the same as default construction for most types! For most "T", including POD types, you'll give "x" a random bunch of bits, which cannot be a source of an assignment (only an assignment destination). Unmentioned construction implies default construction only if "T" is: 1. A class type with a default constructor explicitly written 2. A class type that gets an automatic default constructor because it has a base or member on this list 3. An array of a type on this list I think that any other type (built-ins, enums, POD structs) needs the explicit default constructor sequence: template < typename T > void reset( T & t ) { using std::swap; T x = T(); swap( x, t ); } [I'm not sure that any books or, worse, the standard, explicitly mention this.] -- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT hotmail DOT com