Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-07-01 07:48:18


Daniel Frey <daniel.frey_at_[hidden]> 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

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk