[C++-sig] boost.python: implicit type conversion
Stefan Seefeld
seefeld at sympatico.ca
Tue Dec 2 20:32:57 CET 2008
Mihail Konstantinov wrote:
>> Roman Yakovenko wrote:
>>
>
>
>> It is not a good idea. Python string is immutable. May be you should
>> consider custom converter.
>> http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/faq.html#custom_string
>>
>
> I don't see why it is not a good idea. I am nowhere accessing any string derived classes in a mutable fashion, am I?
> Also I have no option to change the class hierarchy. The code fragments I am posting are simplified from a large project that I try to pythonify, reduced to what I think is the cause of the problems. I want to avoid modifying the underlying project under all circumstances.
>
You are attempting to make Python aware of the inheritance relationship
between std::string and your own class. That is what doesn't work, not
your own deriving from std::string.
In order to make 'bases<std::string>' work, you would have to export the
std::string class to Python first. But as that isn't what you actually
want, you may as well remove the 'bases<std::string>' from your code,
and instead rely on (implicit) conversion.
>
>>> Can I modify the declaration inside BOOST_PYTHON_MODULE so that the implicit
>>>
>> conversion is recognized and the above call boost_ext.printme(b) succeeds?
>>
>> Yes: http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/implicit.html
>>
>
> This looks very helpful. But I don't understand the C++ module definition example on that page. It ends with
>
>
>> implicitly_convertible<X,int>();
>> implicitly_convertible<int,X>();
>> }
>>
> I see that an int can be implicitly converted to X. But how can an X be converted to an int? I can't believe that boost.python searches all implemented functions to find that make_x is capable of converting an int to X?
>
I believe it simply expects an 'operator int ()' to be available in that
case.
Regards,
Stefan
--
...ich hab' noch einen Koffer in Berlin...
More information about the Cplusplus-sig
mailing list