2015-01-15 15 views
7

Ich habe das beigefügte C++ - Wrapper-Beispiel für Python: Die Member-Funktion (Methode) ist statisch mit Standard-Argument. Also verwende ich BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS, um die Überlastfunktion zu definieren. Es gibt keine Kompilierung-Fehler, aber wenn ich die statische Member-Funktion nenne ich den Fehler bekam wie folgt:Boost Python Wrap statische Member-Funktion Überladung mit Standard-Argument

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) 

Ich verstehe nicht, warum die erste erzeugte Signatur ist „Klasse C { lvalue} ". Wenn die statische Elementfunktion eine Instanz von C benötigt, die aufgerufen werden soll, sieht das für mich widersprüchlich aus.

Auf der anderen Seite, ich definiere eine andere statische Member-Funktion ohne Verwendung der Memberfunktion Überladung, es funktioniert für ohne die "Klasse C {lvalue}" Signatur. Zum Beispiel:

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

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

C++ signature : 
    int noDefaultArgMethod(

Klasse std :: basic_string, Klasse std :: Allocator>, int, bool)

Könnte jemand das Problem mit BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS helfen, zu erklären, oder geben Sie so ich Vorschlag, wie man es als reale statische Mitgliedsfunktion mit Überlastung benutzt?

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

} 

Antwort

4

Ich glaube, diese Zeile:

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

Und sollten Sie BOOST_PYTHON_FUNCTION_OVERLOADS anstelle von BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS, da es keine C Objekt beteiligt (statische Funktionszeiger:

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

sollte wie folgt aussehen sind nur Funktionszeiger, sie sind nicht Zeiger zu Mitgliedern.

Es ist auch ein Beispiel für this wiki mit Überlastung statischen Funktionen geschrieben, wo der entsprechende Abschnitt sein würde:

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

Das scheint genau wie das, was Sie wollen.

+0

Das ist großartig. Vielen Dank. – David

Verwandte Themen