Boost.Python 多返回参数

csn*_*ate 6 c++ python boost

我有一个 C++ 函数,它从它的参数返回多个值。

void Do_Something( double input1, double input2, double input3,
    double& output1, double& output2 )
{
    ...
    output1 = something;
    output2 = something;
}
Run Code Online (Sandbox Code Playgroud)

我想使用 Boost.Python 包装这个函数。我想出了一个使用 lambdas 的解决方案,但它有点乏味,因为我有很多函数在它们的参数中有多个返回值。

BOOST_PYTHON_MODULE( mymodule )
{
    using boost::python;
    def( "Do_Something", +[]( double input1, double input2, double input3 )
    {
        double output1;
        double output2;
        Do_Something( input1, input2, input3, output1, output2 );
        return make_tuple( output1, output2 );
    });
}
Run Code Online (Sandbox Code Playgroud)

有没有更好的\自动方法来使用 Boost.Python 来完成这个任务?

小智 5

改进可能是:

boost::python::tuple Do_Something(double input1, double input2,
                                  double input3) {
    // Do something
    ...
    return boost::python::make_tuple(output1, output2);
}

BOOST_PYTHON_MODULE(mymodule) {
    def("Do_Something", Do_Something);
}
Run Code Online (Sandbox Code Playgroud)

  • 请补充您的问题,说明改进之处。 (2认同)