【问题标题】:Exposing a class with a constructor that has an array argument with Boost Python使用 Boost Python 公开具有具有数组参数的构造函数的类
【发布时间】:2014-06-25 17:13:48
【问题描述】:

我正在尝试使用 Boost Python 导出一个如下所示的类:

struct bool_array
{
    bool_array(bool constructor_bool[7])
    {
        for(unsigned int i=0; i < 7; i++)
            bools[i] = constructor_bool[i];
    }

    bool bools[7];
};

我也想公开构造函数,使用以下 Boost 代码:

class_<bool_array>("bool_array", init<bool*>())
    .def_readwrite("bools", &bool_array::bools)
;

问题是我得到这个编译器错误:

error C2440: '=' : cannot convert from 'const bool [7]' to 'bool [7]'

我也试过了

init<bool[7]>

init<bool[]>

无济于事。

我确定我遗漏了一些明显的东西,但我一直无法弄清楚我需要做什么来公开这个类。

谢谢

【问题讨论】:

    标签: python c++ class boost constructor


    【解决方案1】:

    当我对此深思熟虑时,我了解到 boost-python 不支持直接暴露 C 样式数组。相反,我选择使用向量:

    struct bool_array
    {
        bool_array(std::vector<bool> constructor_bool)
        {
            for(unsigned int i=0; i < 7; i++)
                bools.push_back(constructor_bool[i]);
        }
    
         std::vector<bool> bools;
    };
    

    使用以下 boost-python 包装器:

    typedef std::vector<bool> BoolVector;
    bp::class_<BoolVector>("BoolVector")
        .def(bp::vector_indexing_suite<BoolVector>())
    ;
    
    bp::class_<bool_array>("bool_array", bp::init<std::vector<bool>>())
        .def_readwrite("bools", &bool_array::bools)
    ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 2012-10-21
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      相关资源
      最近更新 更多