From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-08-25 07:29:34
Vladimir Prus wrote:
>> If I have boost::shared_ptr<some_class>, it's possible to cast it to
>> shared_ptr<void>. But it does not appear to be any way to convert
>> pointer to function to pointer to void -- just because there's no
>> implicit conversion from function pointer to void*.
>> So, when I compile the attached program with g++, I get:
>> sp.cpp:22: instantiated from here
>> /home/ghost/Work/boost/boost/shared_ptr.hpp:163: error: invalid
>> conversion from
>> `void (* const)()' to `void*'
>> Is there any way to make it work? What I'm trying to do is:
>> - access a function from DLL -- i.e. get shared_ptr<function_type>
>> - do something with the function
>> - cast the pointer into shared_ptr<void>
>> The last pointer will be stored in various places to keep the DLL in
>> memory until last object which depend on it is destroyed.
>> I think I can keep shared_ptr<function_type> everywhere, but can
>> shared_ptr<void> work somehow?
No, not directly. A pointer to a function cannot be converted to void*, not
even with a reinterpret_cast, and shared_ptr doesn't support
You can, however, hold the shared_ptr<F> itself in a shared_ptr<void>, at
the expense of an additional memory allocation or two.
shared_ptr<void> pv( new shared_ptr<F>(pf) ); // two allocs
shared_ptr<void> pv( static_cast<void*>(0), bind( null_deleter(), pf ) ); //
If you don't need portability and you know that a function pointer and an
object pointer have the same memory layout, you can reinterpret_cast a
shared_ptr<F> to shared_ptr<void>&.
If you use the deleter approach, you can later recover the original
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk