简体   繁体   English

如何导出向量 <T> :: push_back返回Python?

[英]How can I export vector<T>::push_back to Python?

This problem would be fairly simple and I would misunderstand something trivial. 这个问题很简单,我会误解一些琐碎的东西。 But since I couldn't find a solution for hours, please let me ask a question here. 但是由于我几个小时都找不到解决方案,请在这里让我问一个问题。

What I want to do is exporting the push_back method of the std::vector<T> container class with some fixed type T (say double ) to Python using Boost.Python. 我想做的是使用Boost.Python将std::vector<T>容器类的push_back方法(带有一些固定类型T (例如double ))导出到Python。

To do that, I wrote the following code: 为此,我编写了以下代码:

typedef std::vector<double> vector_double;
class_<vector_double>("vector_double")
    .def("append", &vector_double::push_back);

But this doesn't compile with the following error: 但这不会与以下错误一起编译:

/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

The type deduction (?) seems to fail but I can't figure out why. 类型推导(?)似乎失败了,但我不知道为什么。 When I defined vector_double as a subclass of std::vector<double> , the code above compiled successfully. 当我将vector_double定义为std::vector<double>的子类时,以上代码已成功编译。 But I don't want to do that for some reason. 但由于某些原因,我不想这样做。

Can you teach me a solution to this problem? 您能教我解决这个问题的方法吗?

I'm using clang++ with -std=c++11 option and Boost.Python v1.56.0. 我正在使用带有-std=c++11选项和Boost.Python v1.56.0的clang ++。

Thank you. 谢谢。

It seems to me, that problem is, that there is two version of push_back in C++11. 在我看来,问题在于C ++ 11中有两个版本的push_back Try to do it this way 尝试这样做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM