我有python附加的C包装器示例:成员函数(方法)是静态的,带有默认参数 . 所以我使用BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS来定义重载函数 . 没有编译错误,但是当我调用静态成员函数时,我得到了如下错误:
import boostPythonTest
boostPythonTest.C.method("string")
------------------------------------------------- -------------------------- ArgumentError Traceback(最近一次调用最后一次)
<module>()中的<ipython-input-4-ab141804179c>
----> 1 boostPythonTest.C.method(“string”)
ArgumentError:Python中的参数类型
C.method(str)与C签名不匹配:
方法(类C ,类std :: basic_string <char,struct
std :: char_traits <char>,类std :: allocator <char >>)
方法(类C ,类std :: basic_string <char,struct
std :: char_traits <char>,类std :: allocator <char >>,int)
方法(类C ,类std :: basic_string <char,struct
std :: char_traits <char>,类std :: allocator <char >>,int,bool)
我不明白为什么第一个生成的签名是“class C ” . 如果是这样,静态成员函数需要调用C的实例,它看起来与我相矛盾 .
另一方面,我定义了另一个静态成员函数而没有使用成员函数重载,它适用于没有“class C ”签名 . 例如:
boostPythonTest.C.noDefaultArgMethod("string", 0, True)
类型:函数字符串形式:Docstring:noDefaultArgMethod((str)arg1,(int)arg2,(bool)arg3) - > int:C签名:
int noDefaultArgMethod(
class std :: basic_string,class std :: allocator>,int,bool)
任何人都可以用BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS帮助解释这个问题,或者给出一些建议如何将它用作具有重载的真实静态成员函数?
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/args.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/class.hpp>
#include <boost/python/overloads.hpp>
#include <boost/python/return_internal_reference.hpp>
#include <boost/python/register_ptr_to_python.hpp>
#include <boost/python/object/class.hpp>
using namespace boost::python;
class C {
public:
static int method(const std::string &arg1, int arg2 = 0, bool arg3 = true) {
return 1;
};
static int noDefaultArgMethod(const std::string &arg1, int arg2 = 0, bool arg3 = true) {
return 10;
};
};
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(method1, C::method, 1, 3)
BOOST_PYTHON_MODULE(boostPythonTest)
{
class_<C>("C")
.def("method", (int(C::*)(const std::string&, int, bool))0, method1())
.staticmethod("method")
.def("noDefaultArgMethod", &C::noDefaultArgMethod)
.staticmethod("noDefaultArgMethod");
}
1 回答
我相信这一行:
应该是这样的:
你应该使用
BOOST_PYTHON_FUNCTION_OVERLOADS
而不是BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS
,因为没有涉及C
对象(静态函数指针只是函数指针,它们不是指向成员的指针) .在this wiki上发布了一个带有重载静态函数的示例,相关部分将是:
这似乎正是你想要的 .