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. 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). Then, instead of saying implicitly_convertible<X, double>(); implicitly_convertible<double, X>(); I would expect to say X double_to_X(double); double X_to_double(const X &); ... implicitly_convertible<X, double>(X_to_double); implicitly_convertible<double, X>(double_to_X); Does this make any sense ? Does something like this already exist ? Thanks, Stefan
Stefan Seefeld <stefan.seefeld@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 floats. 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 www.boost-consulting.com
--- Stefan Seefeld <stefan.seefeld@orthosoft.ca> wrote:
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).
I believe currently the only solution is to use custom rvalue converters. Here is an example: http://mail.python.org/pipermail/c++-sig/2003-May/004133.html The recipe is a bit tedious but very powerful.
Then, instead of saying
implicitly_convertible<X, double>(); implicitly_convertible<double, X>();
I would expect to say
X double_to_X(double); double X_to_double(const X &);
...
implicitly_convertible<X, double>(X_to_double); implicitly_convertible<double, X>(double_to_X);
Does this make any sense ? Does something like this already exist ?
Very interesting idea. But I am not sure how the "convertible" test could work (see the custom rvalue converter example.) Ralf __________________________________ Do you Yahoo!? SBC Yahoo! DSL - Now only $29.95 per month! http://sbc.yahoo.com
participants (3)
-
David Abrahams -
Ralf W. Grosse-Kunstleve -
Stefan Seefeld