[C++-sig] Re: implicit_convertible extension

David Abrahams dave at boost-consulting.com
Wed Jul 16 18:07:27 CEST 2003

Stefan Seefeld <stefan.seefeld at orthosoft.ca> writes:

> hi there,
> I'm browsing through the documentation as well as
> the mailing list archive to find the best way to
> convert between a C++ type (boost::posix_time::time_duration)
> and a python type (double).
> Conversion should happen implicitely, i.e. I have a bunch
> of functions taking a time_duration argument, which I want
> to be able to call in python passing simple doubles.

You mean floats, I think.  That's the name of the Python
floating-point type.

> Looking at the documentation for implicit_convertible,
> I find the requirements a bit restrictive.
> I think it may be convenient to be able to provide converter
> functions that do the conversion even if the two types themselfs
> are totally unaware of each other (i.e. no cast operators, no
> special constructors required).

That's what explicit converter registration is for.  You want an
rvalue from-python converter for time_duration which accepts Python

This is an undocumented feature, but if you investigate,
http://www.boost.org/libs/python/doc/v2/faq.html#question2 will tell
you how to go about doing it.

> Does this make any sense ? Does something like this already exist ?

Sorta.  See above.

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list