Boost logo

Boost :

From: Kirit Sælensminde (kirit.saelensminde_at_[hidden])
Date: 2007-11-04 21:03:53


Daniel Frey wrote:
> #include <iostream>
> #include <boost/shared_ptr.hpp>
>
> struct X {};
>
> void freeX( X* x )
> {
> // Should not be called with a null pointer
> std::cout << "free " << x << std::endl;
> }
>
> template< typename T >
> struct on_nonzero_t
> {
> typedef void (&F)( T* );
> F f_;
>
> on_nonzero_t( F f ) : f_( f ) {}
> void operator()( T* t ) const { if( t ) f_( t ); }
> };
>
> template< typename T >
> on_nonzero_t< T > on_nonzero( void (&f)( T* ) )
> {
> return on_nonzero_t< T >( f );
> }
>
> void f( X* x )
> {
> std::cout << "x = " << x << std::endl;
> boost::shared_ptr< X > tmp( x, on_nonzero( freeX ) );
> }
>
> int main()
> {
> X x;
> f( &x );
>
> f( 0 );
> }
>
> The naming of the helper might be crap, but anyway, hope you can make
> some use of it...

Would you like to drop this into the Boost cookbook? Or alternatively
on the Wiki (although I'm not sure where on the wiki would be the best
place). Links to both are below.

If not do you mind if I do so? Don't worry about any tidying up, or
explanations if you don't want to - it can all be handled later on.

K

Cookbook: http://www.boostcookbook.com/
Wiki: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl

-- 
http://www.kirit.com/

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