Boost :: python string-convertible properties

I have a C ++ class that has the following methods:

class Bar {
...
    const Foo& getFoo() const;
    void setFoo(const Foo&);
};

where the class Foocan be converted to std::string(it has an implicit constructor from std::stringand a translation operator std::string).

I define a Boost.Python wrapper class, which, among other things, defines a property based on the previous two functions:

class_<Bar>("Bar")
    ...
    .add_property(
        "foo",
        make_function(
            &Bar::getFoo,
            return_value_policy<return_by_value>()),
        &Bar::setFoo)
    ...

I also mark the class as convertible to / from std::string.

implicitly_convertible<std::string, Foo>();
implicitly_convertible<Foo, std::string>();

But at runtime, I still get a conversion error trying to access this property:

TypeError: No to_python (by-value) converter found for C++ type: Foo

How to achieve conversion without too many wrapper function templates? (I already have all the conversion functions in the class Foo, so duplication is undesirable.

+5
1
+2

All Articles