Boost.Python:\uu init_uu不接受任何参数

2024-09-30 01:29:18 发布

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

我有一个C++值类型Boost.Python它有一个空值的概念。包装器代码的相关部分如下所示:

class_<TCurrency> currency( "TCurrency" )
    .def( init<long>() )
    .def( init<const std::string&>() )
    <...>;

目前,尝试通过将{{CD1>}创建到一个空实例,到^ {CD2>}方法,使C++ cor接受一个const字符串引用,并用一个无效引用调用它。(&arg == NULL

有没有可能在None被传递给构造函数的情况下进行陷阱处理,或者至少在程序崩溃之前抛出一个有意义的异常?在

使用boost1.36和python2.6.2。在


Tags: 代码概念类型stringinitdefcurrencylong
1条回答
网友
1楼 · 发布于 2024-09-30 01:29:18

添加一个init<void*>重载,如果没有使用的话,将传递NULL,但是我不确定这将如何影响角落情况下的其他ctors。如果不使用init<void*>,我也不会得到您提到的那种None-to-string const&const;转换。使用Boost.Python1.37和Python 2.6.2。在

示例:

#include <iostream>
#include <string>

#include <boost/python.hpp>


struct A {
#define BODY { std::cout << __PRETTY_FUNCTION__ << '\n'; }
    A() BODY
    A(long) BODY
    A(std::string const&) BODY
    A(void* p) BODY
#undef BODY
};

BOOST_PYTHON_MODULE(ex) {
using namespace boost::python;
class_<A>("A")
    .def(init<long>())
    .def(init<std::string const&>())
    .def(init<void*>())
;
}
^{pr2}$

如果init<void*>被省略:

>>> ex.A(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
    A.__init__(A, NoneType)
did not match C++ signature:
    __init__(_object*, std::string)
    __init__(_object*, long)
    __init__(_object*)

相关问题 更多 >

    热门问题