使用默认参数Boost Python wrap静态成员函数重载

2024-10-01 17:38:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有Python的附加C++包装示例:成员函数(方法)是静态的,带有默认参数。所以我使用BOOST_PYTHON_MEMBER_FUNCTION_重载来定义重载函数。没有编译错误,但是当我调用静态成员函数时,得到的错误如下:

import boostPythonTest    
boostPythonTest.C.method("string")
---------------------------------------------------------------------------  ArgumentError Traceback (most recent call last)
<ipython-input-4-ab141804179c> in <module>()
----> 1 boostPythonTest.C.method("string")

ArgumentError: Python argument types in
C.method(str) did not match C++ signature:

method(class C {lvalue}, class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >)

method(class C {lvalue}, class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >, int)

method(class C {lvalue}, class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >, int, bool)

我不明白为什么第一个生成的签名是“类C{lvalue}”。如果是这样的话,静态成员函数需要调用一个C实例,这对我来说是矛盾的。在

另一方面,我在不使用成员函数重载的情况下定义了另一个静态成员函数,它在没有“类C{lvalue}”签名的情况下工作。例如:

^{pr2}$

Type: function String Form: Docstring: noDefaultArgMethod( (str)arg1, (int)arg2, (bool)arg3) -> int :

C++ signature :
    int noDefaultArgMethod(

class std::basic_string,class std::allocator >, int, bool)

有谁能帮忙解释一下BOOST_PYTHON_MEMBER_FUNCTION_重载的问题,或者给出一些建议如何使用它作为带有重载的实际静态成员函数?在

#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");

}

Tags: 函数stringinclude静态成员methodclassint
1条回答
网友
1楼 · 发布于 2024-10-01 17:38:59

我相信这句话:

.def("method", (int(C::*)(const std::string&, int, bool))0, method1())

应该是这样的:

^{pr2}$

您应该使用BOOST_PYTHON_FUNCTION_OVERLOADS而不是BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS,因为没有涉及C对象(静态函数指针只是函数指针,它们不是指向成员的指针)。在

还有一个在this wiki上发布的带有重载静态函数的示例,其中相关部分将是:

class X {
    static int returnsum(int m, int x = 10) { return m + x; }
};

BOOST_PYTHON_FUNCTION_OVERLOADS(X_returnsum_overloads, X::returnsum, 1, 2)

BOOST_PYTHON_MODULE(foo)
{
    class_<X>("X", ..)
        .def("returnsum", &X::returnsum,
            X_returnsum_overloads(args("x", "m"), "returnsum's docstring")
            )
        .staticmethod("returnsum")
        ;
}

这似乎正是你想要的。在

相关问题 更多 >

    热门问题