[C++-sig] std::tr1::shared_ptr as boost::shared_ptr

Piotr Jaroszynski p.jaroszynski at gmail.com
Fri Apr 20 14:10:13 CEST 2007


Hello,

I am exposing a C++ app, which makes heavy use of tr1::shared_ptr, which seems 
to be exactly(?) the same as boost::shared_ptr. I have managed to make most 
of the stuff work just as it would be boost::shared_ptr ( additionally adding 
support for shared_ptr<const T> - thanks to [1] ):
	
namespace my_app_namespace {
	//Make Boost.Python work with std::tr1::shared_ptr<>
	template<typename T_>
	inline T_ * get_pointer(std::tr1::shared_ptr<T_> const & p)
	{
	    return p.get();
	}
	
	//Make Boost.Python work with std::tr1::shared_ptr<const>
	template<typename T_>
	inline T_ * get_pointer(std::tr1::shared_ptr<const T_> const & p)
	{
	    return const_cast<T_*>(p.get());
	}
}

namespace boost
{
    namespace python
    {
        //Make Boost.Python work with std::tr1::shared_ptr<>
        template<typename T_>
        struct pointee<std::tr1::shared_ptr<T_> >
        {
            typedef T_ type;
        };

        //Make Boost.Python work with std::tr1::shared_ptr<const>
        template<typename T_>
        struct pointee<std::tr1::shared_ptr<const T_> >
        {
            typedef T_ type;
        };
    }
}

What is still not working is the implicit conversion of Foo and Derived for 
functions taking std::tr1::shared_ptr<Foo>.
With boost::shared_ptr it just works when you add the bp::bases<Foo> to the 
Derived:
func(boost::shared_ptr<Foo>) ...

bp::class_<Foo> ...
bp::class_<Derived, bp::bases<Foo> > ...
and func can take both Foo and Derived.

But with std::tr1::shared_ptr I have to use:
func(std::tr1::shared_ptr<Foo>) ...

bp::class_<Foo, std::tr1::shared_ptr<Foo> > ...
bp::class_<Derived, std::tr1::shared_ptr<Derived>, bp::bases<Foo> > ...
bp::implicitly_convertible<std::tr1::shared_ptr<Foo>, 
std::tr1::shared_ptr<Derived> >();

How can I make it working just as with boost::shared_ptr?

Btw. I have seen [2] already, but it doesn't seem to cover that.

[1] - 
http://language-binding.net/pyplusplus/troubleshooting_guide/shared_ptr/shared_ptr.html
[2] - 
http://language-binding.net/pyplusplus/troubleshooting_guide/smart_ptrs/smart_ptrs.html

-- 
Best Regards,
Piotr Jaroszynski



More information about the Cplusplus-sig mailing list