shared_ptr and pointer to function

I wonder if shared_ptr can hold a pointer to a function. I've just tried the following program: #include <boost/shared_ptr.hpp> using namespace boost; int main() { extern int* object; boost::shared_ptr<int> p(object); extern void (*function)(); boost::shared_ptr<void ()> p2(function); return 0; } The first initialization, of course, work. The second produces a lots of errors. Do I miss something obvious? TIA, Volodya

Vladimir Prus wrote:
I wonder if shared_ptr can hold a pointer to a function.
Yes, it can.
I've just tried the following program:
#include <boost/shared_ptr.hpp> using namespace boost;
int main() { extern int* object; boost::shared_ptr<int> p(object);
extern void (*function)(); boost::shared_ptr<void ()> p2(function);
return 0; }
The first initialization, of course, work. The second produces a lots of errors. Do I miss something obvious?
"Requires: the expression 'delete p' shall be well-formed and its behavior defined." Try a null deleter.

Vladimir Prus wrote:
extern void (*function)(); boost::shared_ptr<void ()> p2(function);
return 0; }
The first initialization, of course, work. The second produces a lots of errors. Do I miss something obvious?
Just to clarify: my original intention is to use shared_ptr to a function together with a custom deleter. I don't really think the above could compile. But when I add: struct null_deleter { template<class T> void operator()(T) const { } }; and change the code to be boost::shared_ptr<void ()> p2(function, null_deleter()) I still get errors. The complete example with the custom deleter is attached. - Volodya

Vladimir Prus wrote:
Vladimir Prus wrote:
extern void (*function)(); boost::shared_ptr<void ()> p2(function);
return 0; }
The first initialization, of course, work. The second produces a lots of errors. Do I miss something obvious?
Just to clarify: my original intention is to use shared_ptr to a function together with a custom deleter. I don't really think the above could compile. But when I add:
struct null_deleter { template<class T> void operator()(T) const { } };
and change the code to be
boost::shared_ptr<void ()> p2(function, null_deleter())
I still get errors.
The complete example with the custom deleter is attached.
Your example works for me on MSVC 7.1. What kinds of errors do you get, and on what compilers?

On Tue, Aug 24, 2004 at 06:35:07PM +0300, Peter Dimov wrote:
Your example works for me on MSVC 7.1. What kinds of errors do you get, and on what compilers?
$ g++34 -I$HOME/include shared_ptr_fun.cc -c /home/jw/include/boost/shared_ptr.hpp: In constructor `boost::shared_ptr<T>::shared_ptr(Y*, D) [with Y = void ()(), D = null_deleter, T = void ()()]': shared_ptr_fun.cc:20: instantiated from here /home/jw/include/boost/shared_ptr.hpp:138: error: invalid conversion from `void (*)()' to `const volatile void*' /home/jw/include/boost/shared_ptr.hpp:138: error: initializing argument 1 of `void boost::detail::sp_enable_shared_from_this(const volatile void*, const volatile void*, const boost::detail::shared_count&)' /home/jw/include/boost/shared_ptr.hpp:138: error: invalid conversion from `void (*)()' to `const volatile void*' /home/jw/include/boost/shared_ptr.hpp:138: error: initializing argument 2 of `void boost::detail::sp_enable_shared_from_this(const volatile void*, const volatile void*, const boost::detail::shared_count&)' jon -- "Immorality: the morality of those who are having a better time." - H.L. Mencken

Jonathan Wakely wrote:
On Tue, Aug 24, 2004 at 06:35:07PM +0300, Peter Dimov wrote:
Your example works for me on MSVC 7.1. What kinds of errors do you get, and on what compilers?
$ g++34 -I$HOME/include shared_ptr_fun.cc -c /home/jw/include/boost/shared_ptr.hpp: In constructor `boost::shared_ptr<T>::shared_ptr(Y*, D) [with Y = void ()(), D = null_deleter, T = void ()()]': shared_ptr_fun.cc:20: instantiated from here /home/jw/include/boost/shared_ptr.hpp:138: error: invalid conversion from `void (*)()' to `const volatile void*'
Fixed, thanks. :-)

Peter Dimov wrote:
Jonathan Wakely wrote:
On Tue, Aug 24, 2004 at 06:35:07PM +0300, Peter Dimov wrote:
Your example works for me on MSVC 7.1. What kinds of errors do you get, and on what compilers?
$ g++34 -I$HOME/include shared_ptr_fun.cc -c /home/jw/include/boost/shared_ptr.hpp: In constructor `boost::shared_ptr<T>::shared_ptr(Y*, D) [with Y = void ()(), D = null_deleter, T = void ()()]': shared_ptr_fun.cc:20: instantiated from here /home/jw/include/boost/shared_ptr.hpp:138: error: invalid conversion from `void (*)()' to `const volatile void*'
Fixed, thanks. :-)
Thanks, Peter! - Volodya
participants (3)
-
Jonathan Wakely
-
Peter Dimov
-
Vladimir Prus