Mit Boost :: Python funktioniert der normale Mechanismus für Wrapping-Funktionen korrekt, wenn C ++ - Funktionen zurückgegeben werden void
. Leider hat der normale Mechanismus auchEinschränkungen, insbesondere in Bezug auf die Funktion, die es unterstützt. Also muss ich boost :: python :: raw_function verwenden, um meine Funktion zu umbrechen, aber es kompiliert nicht, wenn meine Funktion zurückkehrt void
. Hier ist ein einfacher Testfall:
#include <boost/python.hpp>
#include <boost/python/raw_function.hpp>
void entry_point(boost::python::tuple args, boost::python::dict kwargs) { }
BOOST_PYTHON_MODULE(module)
{
boost::python::def("entry_point", boost::python::raw_function(&entry_point));
}
Welche gibt den Fehler:
/ usr / local / include / boost / python / raw_funktion.hpp: In der Memberfunktion 'PyObject * boost :: python :: detail :: raw_dispatcher :: operator () (PyObject *, PyObject *) [mit F = void (*) (boost :: python :: tuple, boost :: python :: dict)] ':
/usr/local/include/boost/python/object/py_function.hpp:94: instanziiert von 'PyObject * boost :: python :: objects :: full_py_function_impl :: operator () (PyObject *, PyObject *) [mit Caller = boost :: python :: detail :: raw_dispatcher, Sig = boost :: mpl :: vector1] '
void.cpp: 8: von hier aus instanziiert
/usr/local/include/boost/python/raw_function.hpp:36: Fehler: ungültige Verwendung von void-Ausdruck
Für den Moment kann ich das umgehen, indem ich meine Funktion einen Dummy-Wert zurückgebe, aber das ist etwas unbefriedigend. Haben andere Leute dieses Problem?
Antworten:
2 für die Antwort № 1Ich denke, so ist das raw_function()
funktioniert. Es erwartet, dass Ihre Funktion ein Python-Objekt zurückgibt.
In Python werden Sie am nächsten zu einer Funktion zurückkehren void
ist eine Funktion, die zurückkehrt None
. Ich denke, diese Herangehensweise wäre in Ihrem Fall am besten (und nicht einmal so hässlich):
#include <boost/python.hpp>
#include <boost/python/raw_function.hpp>
using namespace boost::python;
namespace
{
object entry_point(tuple args, dict kwargs)
{
return object();
}
}
BOOST_PYTHON_MODULE(foo)
{
def("entry_point", raw_function(&entry_point));
}