[C++-sig] Determine C++ type held by object?
David Abrahams
dave at boost-consulting.com
Tue Dec 13 23:21:20 CET 2005
Alex Mohr <amohr at pixar.com> writes:
> extract<T>() is awesome for getting C++ objects out of python objects.
> However, extract goes through the converters. I would like a way to
> determine whether an object is holding a particular C++ type without
> going through converters.
What do you mean by "going through converters," and why do you want to
avoid it?
> Anyone know of a way to do this?
>
>
> For those interested, here's why I would like it:
>
> I am wrapping a boost::any-like object.
>
> 1. I want 'any' objects in python that are passed as arguments to
> wrapped C++ functions to convert to their held type if necessary. I
> have this working just fine with a custom converter. Call this
> converter #1.
>
> 2. I want to be able to call wrapped C++ functions that take 'any'
> objects with arbitrary python objects. That is, I want objects of all
> types to convert to 'any'. I have attempted to write this converter.
> Call it converter #2.
>
> Here's the problem. In #1's convertible() function, I have to check if
> the python object is holding an 'any', and if so, get it out and check
> if the type it's holding is a match. I use extract<any> for this.
>
> However, #1's call to extract<any> invokes converter #2. Converter #2
> *always* says it can convert to 'any', and it does so by actually
> constructing an 'any' object holding what it was given.
Why? You only need to check the convertible function. That would be
extract<any>().check()
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
More information about the Cplusplus-sig
mailing list