使用提升蟒蛇元组,如何循环元组项目?
With boost python tuple, how to loop over tuple items?
使用 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 ‘<unresolved overloaded function type>’)
8 | std::cout << t0 << std::endl;
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~~
所以问题出在<<运算符上?在提升时缺少施法运算符::p ython::元组?这里似乎没有定义运算符 https://www.boost.org/doc/libs/1_38_0/libs/python/doc/v2/tuple.html
解决方案:
代码必须使用len
和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
>>>
相关文章:
- C++:TypeDef使用元组
- Pybind11:将元组列表从Python传递到C++
- 重载元组索引运算符-C++
- 在C++中,如何通过几种类型从元组中选择多个元素
- 将fold表达式与std::一起用于两个元组
- std::ranges::elements_view,用于自定义类似元组的数据
- 将元组的向量转换/构造为堆
- 专用于 std 元组的模板,而无需用户执行remove_cvref
- 将元组的向量构造成堆
- 元组由 Swig 生成的 Python 包装器返回,用于C++向量
- 将元组类型扩展为可变参数模板?
- 时间复杂度 当具有复合数据类型(如元组或对)时?
- 类内部和外部静态 constexpr 元组之间的差异
- 可变参数模板与使用元组在参数中添加不同的数据对
- 使用提升蟒蛇元组,如何循环元组项目?
- 为什么我在循环提升时得到意外的索引::hana::元组
- 在循环中组合元组
- 复制地图<双精度,元组<双精度,双精度>>映射<双精度,双精度>没有循环?
- 模板在元组中循环
- 循环遍历类模板中的元组,插入vector元素