[C++-sig] Is there a way to automatically convert a smart_ptr to a held_type (try 2)

Alex Mohr amohr at pixar.com
Wed Sep 6 23:45:10 CEST 2006


> The problem for me was that the rvalue converter when passed a
> non-None could not do the placement new created of the ptr<Base>
> because the constructor is protected.  The library I am wrapping
> requires all objects to be created using static factory methods:
> 
> ref_ptr<Base> obj = Class.create()

But the converter's not trying to create a new *object* -- just a new 
smart pointer.  If you can write code that obtains a correct new smart 
pointer from a raw object pointer, then just fill in that code in the 
right place in the converter and it should work.  The only other thing 
that's required is that the smart pointer objects be copy-constructible, 
which I would think they ought to be.  Otherwise they're very strange.

> So there is something going on in the implicit conversion for the
> smart pointers that just handles things better.
> 
> I ended up getting the code to work though by making it so the rvalue
> convertible method return NULL if p != NonType.  This ends up meaning
> that the rvalue converter is only used in the case of passing None,
> but it works for me now.

Sure -- that sounds like it will be okay given your situation.

> Thanks for your help.  I will let you know if anything else crops up
> but I think I am starting to understand this part of boost.python much
> better now so hopefully I will be able to fix it myself. :)

No problem.

Alex



More information about the Cplusplus-sig mailing list