- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将两个重载函数导出到 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 中的签名相互匹配并且不与重载函数的签名重叠。所以,我仍然不知道问题的根源是什么。
最佳答案
简而言之,公开的函数超过了默认的最大数量 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
关于python - 导出函数时boost python模板参数推导/替换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30513482/
运行以下代码片段,我没有收到任何错误,并且得到了预期的结果。但是,由于第二个模板实例化是不明确的 ( both type specifiers are references ),我担心这可能不是定义的
考虑以下示例: #include struct A {}; template void f() { static_assert(std::is_same_v); // #1 A&
从 DH 协商中派生的 secret 派生出一个比方说 128 位 AES key 的正确(可接受的)方法是什么? 使用前 128 位 对 secret 进行哈希处理并使用前 128 位 使用一些更复
从 DH 协商中派生的 secret 派生出一个比方说 128 位 AES key 的正确(可接受的)方法是什么? 使用前 128 位 对 secret 进行哈希处理并使用前 128 位 使用一些更复
我对编写模板元编程比较陌生,这可能是我找不到解决这个问题的原因。问题是这样的:我正在开发一个数学库,它有很多函数,比如确定整数或 std::initializer_list 的质数,将整数更改为罗马数
我在 Android Oreo 源代码中阅读了一些我不太理解的代码。 首先,类IOMXNode有一个函数: class IOMXNode : public IInterface { public: +
有很多关于模板参数推导的讨论和澄清,特别是引用折叠和“通用引用”。本题通过相关细节:How does auto deduce type? ,而 Scott Meyers 的这篇论文更详细,可能会提供更
我将 Java 中的许多假设带到了我对 C++ 的学习中,这似乎再次难倒了我。我没有足够的词汇量来 Eloquent 地说出我希望从以下程序中看到什么,所以我只展示它并说出我希望看到的内容: #inc
对于下面的程序,Clang 5 (trunk) 报告 IsNoexcept 不可推导,而 GCC 7.1 会出现段错误。 标准(草案)对此有何评论?这是编译器 QOI 问题吗? static_asse
我最近发现,在 lambda 中按值捕获 const 对象意味着 labmda 主体(即 lambda 的数据成员)内的变量也是 const. 例如: const int x = 0; auto fo
我是否有机会推断出 PHP Closure 参数类型信息?考虑这个例子: 5, 'b' => 10]); } else { call_user_func($closure, 5, 10);
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
对于上述 svm 的拉格朗日函数,我可以得到如下的偏导数: 但是,我不明白如何将它们插入拉格朗日以导出对偶形式? W可以被替换,但是b去哪里了? 有人可以解释一下并给出详细步骤吗? 最佳答案 你的拉格
我正在寻找一些算法、程序或函数来推断变量的创建方式,只要我提供其他变量即可。我认为计算机程序员会称之为“反编译”,而架构师会称之为“逆向工程”,但我想我不知道统计学家会怎么调用它......或者是否有
这就是我的简单类的样子。 template class A { T first; T second; public: A(T f, T s) : first(f), second(s) {}; te
这个问题在这里已经有了答案: Is it possible to figure out the parameter type and return type of a lambda? (5 个答案)
我有一个函数需要两个 std::function s 作为参数。第二个函数的参数与第一个函数的结果类型相同。 我写了一个这样的函数模板: template void examplFunction(st
O'reilly Optimizing SQL Statments Book的Explaining MySQL Explain章节,最后有这个问题。 The following is an examp
举例 template void function(T&& arg) 有人可以详细解释它是如何结束函数签名变成左值的 T& 和传入的右值的 T&& 吗?我知道不知何故(需要标准行)T -> T& 在
我正在开发用于 EMV 交易的软件,但我面临着雇佣我的公司的文档严重缺乏的问题。 其中之一是关于用于生成 ARQC 的 MKD(在第一个 GENERATE AC 期间)。我从消息请求中知道IAD如下:
我是一名优秀的程序员,十分优秀!