Boost logo

Boost :

From: Bohdan (gejrlaug_at_[hidden])
Date: 2003-06-06 11:57:40


IMHO a very useful addon to your library can be routine
to convert class to function which calls class destructor.

<code>
template < typename T >
    void destruct( void * buf )
    {
        reinterpret_cast<T*>(buf)->~T();
    }

    typedef void (*destruct_fn_type)( void * buf );

    //like following, but compile time
    template < typename T>
         destruct_fn_type destruct_fn()
         {
                return &destruct<T>;
         }

//ex:
        //no virtual functions, no runtime polymorphism
        void * ptr = new MyClass;
        destruct_fn_type df = destruct_fn<MyClass>();

        ....
        df(ptr);
</code>

regards,
bohdan


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