导出函数时 boost python 模板参数 deduction/substitution 失败

boost python template argument deduction/substitution failed when exporting function

我正在尝试将两个重载函数导出到 Python。所以我首先定义指向这些函数的指针,然后使用它们将函数公开给 Python.

BOOST_PYTHON_MODULE(mylib){

    // First define pointers to overloaded function
    double (*expt_pseudopot02_v1)(double,double,double,const VECTOR&,
                                  int,int,int,double,const VECTOR&,
                                  int,int,int,double,const VECTOR& ) = &pseudopot02;

    boost::python::list (*expt_pseudopot02_v2)(double, double, double, const VECTOR&,
                            int,int,int,double, const VECTOR&,
                            int,int,int,double, const VECTOR&,  int, int ) = &pseudopot02;

    // Now export 
    def("pseudopot02", expt_pseudopot02_v1); // this works fine!
    //def("pseudopot02", expt_pseudopot02_v2); // this one gives the problem!

}

第一个导出功能工作正常。第二个(目前已评论)失败,给出错误:

template argument deduction/substitution failed

它还打印了这个解释:

...../boost_1_50_0/boost/python/make_function.hpp:104:59: note:   mismatched types ‘RT (ClassT::*)(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)const volatile’ and ‘boost::python::list (*)(double, double, double, const VECTOR&, int, int, int, double, const VECTOR&, int, int, int, double, const VECTOR&, int, int)’
     f,default_call_policies(), detail::get_signature(f));
                                                       ^

除了有一些带有函数签名的一般想法外,这并没有告诉我太多信息。因此,对问题的性质以及如何解决问题一无所知。似乎这里也没有讨论过类似的问题。

编辑: 在这里,我提供了要求的最小的、完整的、可验证的代码:

在文件中 libX.cpp

#include <boost/python.hpp>
#include "PP.h"
using namespace boost::python;

#ifdef CYGWIN
BOOST_PYTHON_MODULE(cygX){
#else
BOOST_PYTHON_MODULE(libX){
#endif

// This set will work!
//  double (*expt_PP_v1)(const VECTOR& ) = &PP;
//  boost::python::list (*expt_PP_v2)(const VECTOR&,int) = &PP;

// This one - only the first function (returning double)
// the function returning boost::python::list object causes the error
  double (*expt_PP_v1)(double,double,double,const VECTOR&,int,int,int,double,const VECTOR&,int,int,int,double,const VECTOR&) = &PP;
  boost::python::list (*expt_PP_v2)(double, double, double, const VECTOR&, int,int,int,double, const VECTOR&, int,int,int,double, const VECTOR&,  int, int ) = &PP;

  def("PP", expt_PP_v1);
  def("PP", expt_PP_v2);
}

文件PP.h

#ifndef PP_H
#define PP_H
#include <boost/python.hpp>
using namespace boost::python;

class VECTOR{
  public:
  double x,y,z;
  VECTOR(){ x = y = z = 0.0; }
 ~VECTOR(){  }
  VECTOR& operator=(const double &v){  x=y=z=v;    return *this;  }
};

/*  This set of functions will work
double PP(const VECTOR& R, int is_derivs,VECTOR& dIdR );
boost::python::list PP(const VECTOR& R,int is_derivs);
double PP(const VECTOR& R );
*/

// The following - will not, only the one returning double
double PP(double C0, double C2, double alp, const VECTOR& R,
      int nxa,int nya, int nza, double alp_a, const VECTOR& Ra,
      int nxb,int nyb, int nzb, double alp_b, const VECTOR& Rb,
      int is_normalize, 
      int is_derivs, VECTOR& dIdR, VECTOR& dIdA, VECTOR& dIdB
     );
boost::python::list PP(double C0, double C2, double alp, const VECTOR& R,
                   int nxa,int nya, int nza, double alp_a, const VECTOR& Ra,
                   int nxb,int nyb, int nzb, double alp_b, const VECTOR& Rb,
                   int is_normalize, int is_derivs
                  );

double PP(double C0, double C2, double alp, const VECTOR& R,
      int nxa,int nya, int nza, double alp_a, const VECTOR& Ra,
      int nxb,int nyb, int nzb, double alp_b, const VECTOR& Rb
     );

#endif // PP_H

在文件中 PP.cpp

#include "PP.h"
/*  This set will work
double PP(const VECTOR& R, int is_derivs,VECTOR& dIdR ){  dIdR = 0.0;  return 0.0; }
boost::python::list PP(const VECTOR& R,int is_derivs){
  VECTOR dIdR;  
  double I = PP(R, is_derivs, dIdR);
  boost::python::list res;
  res.append(0.0);   if(is_derivs){  res.append(dIdR); }
  return res;
}

double PP(const VECTOR& R ){  VECTOR dIdR;  double res = PP(R, 0, dIdR);  return res; } 
*/

// The following functions will not always work
double PP(double C0, double C2, double alp, const VECTOR& R,
      int nxa,int nya, int nza, double alp_a, const VECTOR& Ra,
      int nxb,int nyb, int nzb, double alp_b, const VECTOR& Rb,
      int is_normalize, 
      int is_derivs, VECTOR& dIdR, VECTOR& dIdA, VECTOR& dIdB
     ){  dIdR = 0.0;  dIdA = 0.0;  dIdB = 0.0;  return 0.0;  }

boost::python::list PP(double C0, double C2, double alp, const VECTOR& R,
                   int nxa,int nya, int nza, double alp_a, const VECTOR& Ra,
                   int nxb,int nyb, int nzb, double alp_b, const VECTOR& Rb,
                   int is_normalize, int is_derivs
                  ){
  VECTOR dIdA, dIdR, dIdB;
  double I = PP(C0,C2,alp,R, nxa,nya,nza,alp_a,Ra, nxb,nyb,nzb,alp_b,Rb, is_normalize,is_derivs,dIdR,dIdA,dIdB); 

  boost::python::list res;
  res.append(I); 
  if(is_derivs){   res.append(dIdR);   res.append(dIdA);  res.append(dIdB);  }
  return res; 
}

double PP(double C0, double C2, double alp, const VECTOR& R,
      int nxa,int nya, int nza, double alp_a, const VECTOR& Ra,
      int nxb,int nyb, int nzb, double alp_b, const VECTOR& Rb
     ){

  VECTOR dIdR,dIdA,dIdB;
  double res = PP(C0, C2, alp, R, nxa,nya,nza,alp_a,Ra, nxb,nyb,nzb,alp_b,Rb, 1, 0, dIdR, dIdA, dIdB);
  return res;
} 

所以,在我看来,当参数数量很大时,模板的识别就搞砸了。我多次检查 libX.cpp、PP.cpp 和 PP.h 中的签名相互匹配并且不与重载函数的签名重叠。所以,我仍然不知道问题的根源是什么。

正如@bogdan 指出的那样,返回 boost::python::list 的函数有 16 个参数,最大提升 python arity 默认设置为 15。使用 #define BOOST_PYTHON_MAX_ARITY 16 增加限制或(更好) 考虑将参数包装到结构中。

简而言之,暴露的函数超过了默认的最大元数 15。如 configuration documentation 中所述,可以定义 BOOST_PYTHON_MAX_ARITY 来控制任何函数的最大允许元数,成员函数,或通过 Boost.Python 包装和暴露的构造函数。在这种特殊情况下,其中一个重载的元数为 16,因此可以在包含 boost/python.hpp:

之前定义最大元数
#define BOOST_PYTHON_MAX_ARITY 16
#include <boost/python.hpp>

截至撰写本文时,Boost.Python (1.58) 不使用 C++11 的可变参数模板。相反,如果使用预处理器宏扩展来提供模板特化,并允许用户通过 BOOST_PYTHON_MAX_ARITY 宏配置最大数量。


这是一个完整的最小示例 demonstrating 增加最大数量:

#define BOOST_PYTHON_MAX_ARITY 16
#include <boost/python.hpp>

// Functions have 5 parameters per line.

/// @brief Mockup spam function with 14 parameters.
double spam(
  int, int, int, int, int, // 5
  int, int, int, int, int, // 10
  int, int, int, int       // 14
)
{
  return 42;
}

/// @brief Mockup spam function with 16 parameters.
boost::python::list spam(
  int, int, int, int, int, // 5
  int, int, int, int, int, // 10
  int, int, int, int, int, // 15
  int                      // 16
)
{
  boost::python::list list;
  return list;
}

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;

  double (*spam_14)(
    int, int, int, int, int, // 5
    int, int, int, int, int, // 10
    int, int, int, int       // 14
  ) = &spam;

  python::list (*spam_16)(
    int, int, int, int, int, // 5
    int, int, int, int, int, // 10
    int, int, int, int, int, // 15
    int                      // 16
  ) = &spam;

  python::def("spam", spam_14);
  python::def("spam", spam_16);
}

交互使用:

>>> import example
>>> assert 42 == example.spam(*range(14))
>>> assert isinstance(example.spam(*range(16)), list)
>>> print example.spam.__doc__
spam( (int)arg1, (int)arg2, (int)arg3, (int)arg4, (int)arg5,
      (int)arg6, (int)arg7, (int)arg8, (int)arg9, (int)arg10,
      (int)arg11, (int)arg12, (int)arg13, (int)arg14) -> float :

    C++ signature :
        double spam(int,int,int,int,int,
                    int,int,int,int,int,
                    int,int,int,int)

spam( (int)arg1, (int)arg2, (int)arg3, (int)arg4, (int)arg5,
      (int)arg6, (int)arg7, (int)arg8, (int)arg9, (int)arg10,
      (int)arg11, (int)arg12, (int)arg13, (int)arg14, (int)arg15,
      (int)arg16) -> list :

    C++ signature :
        boost::python::list spam(int,int,int,int,int,
                                 int,int,int,int,int,
                                 int,int,int,int,int,
                                 int)

不定义最大数量,同样的代码fails to compile:

/usr/local/include/boost/python/make_function.hpp:104:36: error: no matching
  function for call to 'get_signature'
        f,default_call_policies(), detail::get_signature(f));
                                   ^~~~~~~~~~~~~~~~~~~~~
  ... failed template argument deduction