C++ 如何将vector::推回Python?

C++ 如何将vector::推回Python?,c++,boost,boost-python,C++,Boost,Boost Python,这个问题相当简单,我会误解一些琐碎的事情。但由于我几个小时都找不到解决方案,请允许我在这里问一个问题 我想做的是使用Boost.Python将std::vector容器类的push_-back方法导出到Python中,该方法的类型是固定的,比如说double 为此,我编写了以下代码: typedef std::vector<double> vector_double; class_<vector_double>("vector_double") .def("app

这个问题相当简单,我会误解一些琐碎的事情。但由于我几个小时都找不到解决方案,请允许我在这里问一个问题

我想做的是使用Boost.Python将std::vector容器类的push_-back方法导出到Python中,该方法的类型是固定的,比如说double

为此,我编写了以下代码:

typedef std::vector<double> vector_double;
class_<vector_double>("vector_double")
    .def("append", &vector_double::push_back);
但编译时不会出现以下错误:

/Users/kenta/experiments/bisite/include/bisite/conservation_score.hpp:25:7: note:
      candidate constructor (the implicit default constructor) not viable:
      requires 0 arguments, but 1 was provided
/Users/kenta/experiments/bisite/src/bisite/pybisite.cpp:90:10: error: no
      matching member function for call to 'def'
        .def("append", &vector_double::push_back);
        ~^~~
/usr/local/include/boost/python/class.hpp:234:11: note: candidate template
      ignored: couldn't infer template argument 'F'
    self& def(char const* name, F f)
          ^
/usr/local/include/boost/python/class.hpp:224:11: note: candidate function
      template not viable: requires single argument 'visitor', but 2 arguments
      were provided
    self& def(def_visitor<Derived> const& visitor)

... and more candidates
类型推断似乎失败了,但我不明白为什么。 当我将vector_double定义为std::vector的子类时,上面的代码编译成功。但出于某种原因,我不想这么做

你能教我解决这个问题的方法吗

我正在使用带有-std=c++11选项的clang++和Boost.pythonv1.56.0


谢谢。

在我看来,问题是,在C++11中有两个版本的。 试着这样做

static_cast<void (vector_double::*)(const double&)>(&vector_double::push_back);

应该注意的是,Boost.Python已经有了,它是专门为公开std::vector而设计的。我认为vector_indexing_套件只按照名称添加了与索引相关的方法,但实际上它还添加了append和extend方法!你的解决方案彻底解决了问题。谢谢但保罗的解决方案是充分和惯用的。