
Hi, I just wrote a small helper function to clear a container. Well, OK, we already have that, do we? For a container 'cont': cont.clear(); But this doesn't neccessarily free the memory allocated. The "usual" workaround is: Cont().swap( cont ); But Cont (the type of the container) is sometimes rather large and the code doesn't communicate well what I intended. 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... Regards, Daniel -- Daniel Frey aixigo AG - financial solutions & technology Schloß-Rahe-Straße 15, 52072 Aachen, Germany fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99 eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de

Daniel Frey <daniel.frey@aixigo.de> writes:
Hi,
I just wrote a small helper function to clear a container. Well, OK, we already have that, do we? For a container 'cont':
cont.clear();
But this doesn't neccessarily free the memory allocated. The "usual" workaround is:
Cont().swap( cont );
But Cont (the type of the container) is sometimes rather large and the code doesn't communicate well what I intended.
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); } -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

David Abrahams wrote:
Daniel Frey <daniel.frey@aixigo.de> writes:
Not unless you make it generic:
template< typename T > void reset( T& t ) { using std::swap; T x; swap(x, t); }
It depends on whether or not we expect users to provide swap() as a member function or as a free function for their own classes. I tried to address this topic earlier here: <http://lists.boost.org/MailArchives/boost/msg52183.php> but got no satisfying answer. In fact I would prefer the above generic version, but it's not generic when UDTs provide a member function swap and std::swap kicks in and produces inefficient code. Regards, Daniel -- Daniel Frey aixigo AG - financial solutions & technology Schloß-Rahe-Straße 15, 52072 Aachen, Germany fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99 eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de

Daniel Frey <daniel.frey@aixigo.de> writes:
David Abrahams wrote:
Daniel Frey <daniel.frey@aixigo.de> writes: Not unless you make it generic: template< typename T > void reset( T& t ) { using std::swap; T x; swap(x, t); }
It depends on whether or not we expect users to provide swap() as a member function or as a free function for their own classes.
We (meaning I) expect a free function :-) -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

David Abrahams wrote:
Daniel Frey <daniel.frey@aixigo.de> writes:
David Abrahams wrote:
Not unless you make it generic: template< typename T > void reset( T& t ) { using std::swap; T x; swap(x, t); }
It depends on whether or not we expect users to provide swap() as a member function or as a free function for their own classes.
We (meaning I) expect a free function :-)
Glad to hear that :)) It's just that I was under the impression that this is not the generally agreed on consensus here, but this might be misinterpretation on my side so I'd like to hear others comment on it. John? Thomas? (Asking you directly because you seemed to favor member function in the thread I referenced <http://lists.boost.org/MailArchives/boost/msg52183.php>) Regards, Daniel -- Daniel Frey aixigo AG - financial solutions & technology Schloß-Rahe-Straße 15, 52072 Aachen, Germany fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99 eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de

Daniel Frey <daniel.frey@aixigo.de> writes:
David Abrahams wrote:
Daniel Frey <daniel.frey@aixigo.de> writes:
David Abrahams wrote:
Not unless you make it generic: template< typename T > void reset( T& t ) { using std::swap; T x; swap(x, t); }
It depends on whether or not we expect users to provide swap() as a member function or as a free function for their own classes. We (meaning I) expect a free function :-)
Glad to hear that :)) It's just that I was under the impression that this is not the generally agreed on consensus here, but this might be misinterpretation on my side
I sure hope it is. A member function isn't generic because you can't use it to swap non-classes. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

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

Daryle Walker wrote:
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 ); }
Good catch. I'll keep it in mind when I find the time to prepare a patch. Regards, Daniel -- Daniel Frey aixigo AG - financial solutions & technology Schloß-Rahe-Straße 15, 52072 Aachen, Germany fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99 eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de

Daryle Walker <darylew@hotmail.com> writes:
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!
Uh, right. Not so secret actually; that's why we have value_inititalized: #include <boost/utility/value_init.hpp> template< typename T > void reset( T& t ) { using std::swap; swap(value_inititalized<T>().data(), t); } This will be more efficient than your rewrite.
For most "T", including POD types, you'll give "x" a random bunch of bits
Not most "T"; just for PODs.
, 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.]
They do. See the bottom of http://tinyurl.com/26mpo -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

David Abrahams wrote:
Uh, right. Not so secret actually; that's why we have value_inititalized:
#include <boost/utility/value_init.hpp>
Have I missed the documentation? Anyway, looking at it, I found: template<class T> class value_initialized : private vinit_detail::select_base<T>::type { ... } isn't this missing "typename" after "private"? Regards, Daniel -- Daniel Frey aixigo AG - financial solutions & technology Schloß-Rahe-Straße 15, 52072 Aachen, Germany fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99 eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de

Daniel Frey wrote:
David Abrahams wrote:
Uh, right. Not so secret actually; that's why we have value_inititalized:
#include <boost/utility/value_init.hpp>
Have I missed the documentation? Anyway, looking at it, I found:
Hmmm, well, I think it is not listed in "utility" nor elsewhere... I'll fix that.
template<class T> class value_initialized : private vinit_detail::select_base<T>::type { ... }
isn't this missing "typename" after "private"?
AFAIK base classes do not need 'typename' (because they are neccesarily typenames) Fernando Cacciola SciSoft

Daniel Frey <daniel.frey@aixigo.de> writes:
David Abrahams wrote:
Uh, right. Not so secret actually; that's why we have value_inititalized: #include <boost/utility/value_init.hpp>
Have I missed the documentation? Anyway, looking at it, I found:
template<class T> class value_initialized : private vinit_detail::select_base<T>::type { ... }
isn't this missing "typename" after "private"?
No, typename is prohibited on base classes. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

On Thu, 01 Jul 2004 11:48:25 +0200, Daniel Frey <daniel.frey@aixigo.de> wrote:
Hi,
I just wrote a small helper function to clear a container. Well, OK, we already have that, do we? For a container 'cont':
cont.clear();
But this doesn't neccessarily free the memory allocated.
Does the swap trick necessarily free the memory? -- Genny.

Gennaro Prota wrote:
Does the swap trick necessarily free the memory?
AFAIK, yes. See <http://www.gotw.ca/gotw/054.htm>. Regards, Daniel -- Daniel Frey aixigo AG - financial solutions & technology Schloß-Rahe-Straße 15, 52072 Aachen, Germany fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99 eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de
participants (5)
-
Daniel Frey
-
Daryle Walker
-
David Abrahams
-
Fernando Cacciola
-
Gennaro Prota