boost::python string-convertible properties
Posted
by Checkers
on Stack Overflow
See other posts from Stack Overflow
or by Checkers
Published on 2010-04-26T06:17:23Z
Indexed on
2010/04/26
6:23 UTC
Read the original article
Hit count: 266
I have a C++ class, which has the following methods:
class Bar {
...
const Foo& getFoo() const;
void setFoo(const Foo&);
};
where class Foo
is convertible to std::string
(it has an implicit constructor from std::string
and an std::string
cast operator).
I define a Boost.Python wrapper class, which, among other things, defines a property based on 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 the conversion without too much boilerplate of wrapper functions? (I already have all the conversion functions in class Foo
, so duplication is undesirable.
© Stack Overflow or respective owner