我试图将两个重载函数导出到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中的签名之间是否匹配,并且与重载函数的签名不重叠。所以,我仍然不知道问题的根源是什么。
答案 0 :(得分:3)
简而言之,暴露的函数超过了默认的最大值15.如configuration documentation所述,可以定义BOOST_PYTHON_MAX_ARITY
来控制任何函数,成员函数或任何函数的最大允许值。构造函数通过Boost.Python包装和公开。在这种特殊情况下,其中一个重载的arity为16,因此可以在包含boost/python.hpp
之前定义最大值:
#define BOOST_PYTHON_MAX_ARITY 16
#include <boost/python.hpp>
截至撰写本文时,Boost.Python(1.58)并未使用C ++ 11的可变参数模板。相反,如果使用预处理器宏扩展来提供模板特化,并允许用户通过BOOST_PYTHON_MAX_ARITY
宏配置最大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
答案 1 :(得分:1)
由于@bogdan指出函数返回boost :: python :: list有16个参数,默认情况下max boost python arity设置为15.使用#define BOOST_PYTHON_MAX_ARITY 16
增加限制或(更好)考虑包装参数进入结构。