Boost Python переносит перегрузку статической функции-члена с аргументом по умолчанию

I have the attached C++ wrapper example for python: The member function (method) is static with default argument. So I use BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS to define the overload function. 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}". If so the static member function needs an instance of C to be called, it looks contradict for me.

On the other hand, I define another static member function without using member function overload, it works for without the "class C {lvalue}" signature. For example:

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

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)

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?

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

}
7 голосов | спросил David 15 Jpm1000000pmThu, 15 Jan 2015 15:19:46 +030015 2015, 15:19:46

0 ответов


Похожие вопросы

Популярные теги

security × 330linux × 316macos × 2827 × 268performance × 244command-line × 241sql-server × 235joomla-3.x × 222java × 189c++ × 186windows × 180cisco × 168bash × 158c# × 142gmail × 139arduino-uno × 139javascript × 134ssh × 133seo × 132mysql × 132