首页 > 解决方案 > 使用 boost python 元组,如何循环元组项?

问题描述

使用 boost python 元组,如何循环元组项?

>> more dummy.cpp 
#include <iostream>

#include <boost/python.hpp>

void doStuffs(boost::python::tuple & t) {
  std::cout << "tuple " << std::endl;
  //for (auto i : t) std::cout << i << std::endl; // How to loop over tuple items : does NOT compile ?
}

BOOST_PYTHON_MODULE(dummy) {
  Py_Initialize();    
  def("doStuffs", doStuffs);
}

>> make
g++ -I/usr/include/python2.7 -o dummy.so -shared -fPIC dummy.cpp -lboost_python -lpython2.7

我得到:

>> python
Python 2.7.17 (default, Oct 19 2019, 23:36:22) 
[GCC 9.2.1 20191008] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dummy
>>> dummy.doStuffs((1, 2., 'a'))
tuple 

我需要得到的地方:

>> python
Python 2.7.17 (default, Oct 19 2019, 23:36:22) 
[GCC 9.2.1 20191008] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dummy
>>> dummy.doStuffs((1, 2., 'a'))
tuple 
1
2.
a

在 C++ 中,如何遍历元组项并打印它们?

更新

令人惊讶的是,它编译:

>> more dummy.cpp 
#include <iostream>

#include <boost/python.hpp>

void doStuffs(boost::python::tuple & t) {
  std::cout << "tuple " << std::endl;
  auto t0 = t[0];
  //std::cout << t0 << std::endl;
  //for (auto i : t) std::cout << i << std::endl; // How to loop over tuple items : does NOT compile ?
}

BOOST_PYTHON_MODULE(dummy) {
  Py_Initialize();

  def("doStuffs", doStuffs);
}

>> make
g++ -I/usr/include/python2.7 -o dummy.so -shared -fPIC dummy.cpp -lboost_python -lpython2.7

但这不会:

>> more dummy.cpp 
#include <iostream>

#include <boost/python.hpp>

void doStuffs(boost::python::tuple & t) {
  std::cout << "tuple " << std::endl;
  auto t0 = t[0];
  std::cout << t0 << std::endl;
  //for (auto i : t) std::cout << i << std::endl; // How to loop over tuple items : does NOT compile ?
}

BOOST_PYTHON_MODULE(dummy) {
  Py_Initialize();

  def("doStuffs", doStuffs);
}

>> make
g++ -I/usr/include/python2.7 -o dummy.so -shared -fPIC dummy.cpp -lboost_python -lpython2.7
dummy.cpp: In function ‘void doStuffs(boost::python::tuple&)’:
dummy.cpp:8:16: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
    8 |   std::cout << t0 << std::endl;
In file included from /usr/include/boost/python/proxy.hpp:9,
                 from /usr/include/boost/python/object_attributes.hpp:10,
                 from /usr/include/boost/python/object.hpp:10,
                 from /usr/include/boost/python/class.hpp:15,
                 from /usr/include/boost/python.hpp:18,
                 from dummy.cpp:3:
/usr/include/boost/python/object_operators.hpp:105:1: note: candidate 1: ‘typename boost::python::api::enable_binary<L, R, boost::python::api::object>::type boost::python::api::operator<<(const L&, const R&) [with L = std::basic_ostream<char>; R = boost::python::api::proxy<boost::python::api::item_policies>; typename boost::python::api::enable_binary<L, R, boost::python::api::object>::type = boost::python::api::object]’
  105 | BOOST_PYTHON_BINARY_OPERATOR(<<)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/iostream:39,
                 from dummy.cpp:1:
/usr/include/c++/9/ostream:174:7: note: candidate 2: ‘std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]’
  174 |       operator<<(bool __n)
      |       ^~~~~~~~
dummy.cpp:8:19: error: no match for ‘operator<<’ (operand types are ‘boost::iterators::enabled<true>::base<boost::python::api::object>::type’ {aka ‘boost::python::api::object’} and ‘&lt;unresolved overloaded function type>’)
    8 |   std::cout << t0 << std::endl;
      |   ~~~~~~~~~~~~~~~~^~~~~~~~~~~~

所以问题出在 << 运算符上?boost::python::tuple 上缺少强制转换运算符?似乎这里没有定义运算符https://www.boost.org/doc/libs/1_38_0/libs/python/doc/v2/tuple.html

标签: pythonc++boosttuples

解决方案


解决方案 :

代码必须使用lenand extract

>> more dummy.cpp 
#include <iostream>

#include <boost/python.hpp>

void doStuffs(boost::python::tuple & t) {
  std::cout << "tuple " << std::endl;
  for (size_t i = 0; i < len(t); i++) {
    std::string s = boost::python::extract<std::string>(boost::python::str(t[i]));
    std::cout << s << std::endl;
  }
}

BOOST_PYTHON_MODULE(dummy) {
  def("doStuffs", doStuffs);
}

编译:

>> make
g++ -I/usr/include/python2.7 -o dummy.so -shared -fPIC dummy.cpp -lboost_python

现在回到python:

>> python
Python 2.7.17 (default, Oct 19 2019, 23:36:22) 
[GCC 9.2.1 20191008] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dummy
>>> dummy.doStuffs((1, 1., 'a'))
tuple 
1
1.0
a
>>> 

推荐阅读