【发布时间】:2010-04-26 06:17:23
【问题描述】:
我有一个 C++ 类,它有以下方法:
class Bar {
...
const Foo& getFoo() const;
void setFoo(const Foo&);
};
其中Foo 类可转换为std::string(它具有来自std::string 的隐式构造函数和std::string 转换运算符)。
我定义了一个 Boost.Python 包装类,除其他外,它定义了一个基于前两个函数的属性:
class_<Bar>("Bar")
...
.add_property(
"foo",
make_function(
&Bar::getFoo,
return_value_policy<return_by_value>()),
&Bar::setFoo)
...
我还将课程标记为可与std::string 相互转换。
implicitly_convertible<std::string, Foo>();
implicitly_convertible<Foo, std::string>();
但在运行时我仍然会在尝试访问此属性时遇到转换错误:
TypeError: No to_python (by-value) converter found for C++ type: Foo
如何在没有太多包装函数样板的情况下实现转换? (我已经拥有Foo类中的所有转换函数,所以重复是不可取的。
【问题讨论】:
标签: c++ python boost boost-python