【发布时间】:2016-08-04 02:05:07
【问题描述】:
假设提供了一个包装类
class Example
{
public:
Example()
{
std::cout << "hello\n";
}
Example(const Example& e)
{
std::cout << "copy\n";
counter++;
}
~Example()
{
std::cout << "bye\n";
}
Example& count()
{
std::cout << "Count: " << counter << std::endl;
return *this;
}
static int counter;
};
int Example::counter = 0;
暴露给 python 使用
using namespace boost::python;
class_<Example>("Example", init<>())
.def("count", &Example::count, return_value_policy<copy_non_const_reference>());
现在如果我执行以下 python 代码
obj=Example()
obj.count().count()
我明白了
hello
Count: 0
copy
Count: 1
copy
bye
这意味着 boost python 正在使用复制构造函数。
我的问题:
- 为什么要调用复制构造函数?
-
如果我使用 boost::noncopyable,则不会调用复制构造函数。但是,在这种情况下,我无法执行我的 python 代码,因为它抱怨 to_python 转换器(见下文)。有没有办法解决这个问题?
TypeError: No to_python (by-value) converter found for C++ type: class Example
【问题讨论】:
标签: python boost boost-python