【问题标题】:Pybind11 Type ErrorPybind11 类型错误
【发布时间】:2016-05-11 12:07:33
【问题描述】:

我最近一直在尝试用 C++ 编写一个函数,将双精度向量转换为字符串向量。我想从 python 解释器运行它,所以我使用 Pybind11 来接口 C++ 和 Python。这是我目前所拥有的,

#include <pybind11/pybind11.h>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>


std::string castToString(double v) {
    std::string str = boost::lexical_cast<std::string>(v);
    return str;
}

std::vector<std::vector<std::string> > num2StringVector(std::vector<std::vector<double> >& inputVector) {


    //using namespace boost::multiprecision;


    std::vector<std::vector<std::string> > outputVector;

    std::transform(inputVector.begin(), inputVector.end(), std::back_inserter(outputVector), [](const std::vector<double> &iv) {
        std::vector<std::string> dv;
        std::transform(iv.begin(), iv.end(), std::back_inserter(dv), &castToString);
        return dv;
    });

    return outputVector;
}


namespace py = pybind11;

PYBIND11_PLUGIN(num2String) {
    py::module m("num2String", "pybind11 example plugin");

    m.def("num2StringVector", &num2StringVector, "this converts a vector of doubles to a vector of strings.");
    m.def("castToString", &castToString, "This function casts a double to a string using Boost.");

    return m.ptr();
}

现在它编译成一个共享库,使用命令行中的以下命令:

c++ -O3 -shared -std=gnu++11 -I ../include `python-config --cflags --ldflags --libs` num2StringVectorPyBind.cpp -o num2String.so -fPIC -lquadmath

../include 是 pybind11 包含的位置。编译后启动python并使用,

import num2String
values = [[10, 20], [30, 40]]
num2String.num2StringVector(values)

我收到以下错误,“函数参数不兼容。支持以下参数类型”。然后它给了我一个可能类型的列表,这很奇怪,因为我只是想使用一个向量作为我的函数参数,根据 pybind11 的文档,这是一个受支持的数据类型: http://pybind11.readthedocs.io/en/latest/basics.html#supported-data-types

是不是我有一个向量向量(二维向量)并且不受支持?

【问题讨论】:

    标签: python c++ vector casting pybinding


    【解决方案1】:

    您只需多写一行代码即可完成这项工作:

    #include <pybind11/stl.h>
    

    根据the documentation

    开箱即用支持以下基本数据类型(有些可能需要包含额外的扩展标头)。

    在包含pybind11/stl.h之前:

    >>> import num2String
    >>> num2String.num2StringVector([[1, 2], [3, 4, 5]])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: Incompatible function arguments. The following argument types are supported:
        1. (std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >) -> std::vector<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::allocator<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >
    

    包含pybind11/stl.h 并重新编译共享库后:

    >>> import num2String
    >>> num2String.num2StringVector([[1, 2], [3, 4, 5]])
    [['1', '2'], ['3', '4', '5']]
    

    【讨论】:

      猜你喜欢
      • 2021-09-03
      • 2021-10-18
      • 2023-01-29
      • 2018-05-09
      • 1970-01-01
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多