Python: c++扩展,返回多个值

Python: C++ extension returning multiple values

本文关键字:返回 c++ 扩展 Python      更新时间:2023-10-16

我正在为python脚本编写一个c++扩展,并希望返回多个值,就像我们可以在python函数中做的那样。

python中的简单示例:

def test():
    return 0,0

元组似乎是最接近的答案

#include <tuple>
std::tuple<int,int> test(void){
return std::make_tuple(0,0);
}

但是当我编译它时,它抱怨

TypeError: No to_python (by-value) converter found for C++ type: std::tuple<int, int>

谁知道我是否可以使用c++返回多个值?

编辑:

这是我的setup.py文件。

#!/usr/bin/env python
from distutils.core import setup
from distutils.extension import Extension
setup(name="PackageName",
    ext_modules=[
        Extension("foo", ["foo.cpp"],
        libraries = ["boost_python"],
        extra_compile_args = ["-std=c++11"]
        )
    ])

看起来你正在使用boost-python。然后应该使用boost::python::tuple,而不是std::tuple。