Boost.Python 将 python 元组转换为 `std::tuple` 或 `std::pair`

Boost.Python converting python tuple to `std::tuple` or to `std::pair`

我正在尝试用 Boost.Python 包装 class Cube,它具有以下形式的签名:

void insert( const std::pair< int, int>& x){ ... do something .. }

如果我将这个方法总结如下:

.def("append", &Cube::insert, with_custodian_and_ward<1,2>())

我在 python 中收到以下错误:

ArgumentError: Python argument types in
    cube.append(cube, list)
 did not match C++ signature:
     append(Cube {lvalue}, std::__1::pair<unsigned long, unsigned long>)

当我输入时:

cube.append((1,2))

我不确定如何正确地将 python 类型转换为 c++ 类型。我也找不到合适的例子。

我认为最简单的方法是将您的 python 对象显式转换为 C++ std::pair:

.def("append", FunctionPointer([] (Cube& self, const bp::object& obj)
  {
    self.insert(std::make_pair(bp::extract<int>(obj[0]),bp::extract<int>(obj[1])));
  }))