0

I am writing a C++ extension for python script and want to return multiple values like what we can do in python function.

Simple Example in python:

def test():
    return 0,0

tuple seems to be the closest answer

#include <tuple>

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

But when I compile it, it complains that

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

Anyone knows if I could return multiple values using C++?

EDIT:

This is my setup.py file.

#!/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"]
        )
    ])
3
  • If you want to return a specific data type, like int or float, you can use array or std::vector of that particular type. Commented Sep 9, 2016 at 21:33
  • Python C Extension sucks... try ctypes... it's much better and widely compatible and not python version dependent, but needs more experience on C/C++. Commented Sep 9, 2016 at 21:42
  • How are you building the python C++ extension? I don't recognize the source of that error. Commented Sep 10, 2016 at 1:46

1 Answer 1

1

It seems you're using boost-python. Then should use boost::python::tuple, not std::tuple. See the examples on this page.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.