简体   繁体   English

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

[英]Boost Python wrap static member function overload with default argument

I have the attached C++ wrapper example for python: The member function (method) is static with default argument. 我有python的附加C ++包装器示例:成员函数(方法)是静态的,带有默认参数。 So I use BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS to define the overload function. 所以我使用BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS来定义重载函数。 There is no compilation error, however when I call the static member function I got the error as follows: 没有编译错误,但是当我调用静态成员函数时,我得到了如下错误:

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) 

I don't understand why the first generated signature is "class C {lvalue}". 我不明白为什么第一个生成的签名是“class C {lvalue}”。 If so the static member function needs an instance of C to be called, it looks contradict for me. 如果是这样,静态成员函数需要调用C的实例,它看起来与我相矛盾。

On the other hand, I define another static member function without using member function overload, it works for without the "class C {lvalue}" signature. 另一方面,我定义了另一个静态成员函数而没有使用成员函数重载,它适用于没有“class C {lvalue}”签名。 For example: 例如:

boostPythonTest.C.noDefaultArgMethod("string", 0, True)

Type: function String Form: Docstring: noDefaultArgMethod( (str)arg1, (int)arg2, (bool)arg3) -> int : 类型: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) class std :: basic_string,class std :: allocator>,int,bool)

Could anyone help explain the problem with BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS, or give some suggestion how to use it as real static member function with overloading? 任何人都可以用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");

}

I believe this line: 我相信这一行:

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

should look like this: 应该是这样的:

.def("method", &C::method, method1())

And you should use BOOST_PYTHON_FUNCTION_OVERLOADS instead of BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS , since there's no C object involved (static function pointers are just function pointers, they're not pointer-to-members). 你应该使用BOOST_PYTHON_FUNCTION_OVERLOADS而不是BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS ,因为没有涉及C对象(静态函数指针只是函数指针,它们不是指向成员的指针)。

There is also an example posted on this wiki with overloading static functions, where the relevant section would be: 此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")
        ;
}

That seems like exactly what you want. 这似乎正是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM