【发布时间】:2015-05-28 17:38:03
【问题描述】:
我正在尝试将两个重载函数导出到 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 中的签名是否相互匹配,并且不与重载函数的签名重叠。所以,我仍然不知道问题的根源是什么。
【问题讨论】:
-
我想你(几乎)回答了你自己的问题:你的第二个重载有太多参数。尝试设置
BOOST_PYTHON_MAX_ARITY。 -
哦,这很有趣!谢谢你,博格丹。对我来说一点也不明显,在这个宏中硬编码了这样一个限制
标签: python c++ boost boost-python