[C++-sig] Something like boost::any ... custom conversions?

David Abrahams dave at boost-consulting.com
Thu Nov 3 23:35:00 CET 2005


Alex Mohr <amohr at pixar.com> writes:

> Suppose I have a type that is much like boost::any.  If I have an 
> instance of this in python (say holding an int), then I want to be able 
> to call wrapped c++ code which takes arguments of held types (say 
> foo(int)) by passing my any-like object directly.
>
> That is, I want my any-like object to try to convert to the desired c++ 
> type by checking to see if it's holding the desired c++ type.

You can do it for specific held types just by registering the
appropriate from_python converters:

> Can anyone point me in a general direction of where to look to do this 
> sort of thing?  I've tried looking at the conversion information in the 
> reference manual, and I've looked at container_conversions.h, but I have 
> to admit that things are still muddy for me.
>
> The lvalue_from_pytype stuff seems like it might be in the right 
> direction, but I don't really want lvalue conversion.  I only care about 
> passing these things as rvalues.

In general, if you can get ahold of an lvalue, it's usually a good
idea to do it anyway.  The bigger issue is that you don't have the
Python type object associated with your any-like class at compile
time.  But yeah, it's in the right direction.

I suggest you look at
http://news.gmane.org/find-root.php?message_id=%3c1026918209.17167.2.camel%40hunsmac.mit.edu%3e
and follow the links there.  It's probably about time to document this
stuff for real and pull it out of the detail:: namespaces.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Cplusplus-sig mailing list