用boost.python封装模板函数
我正在尝试使用boost.python将以下的C++函数暴露给Python:
template <typename genType>
genType refract(
genType const & I,
genType const & N,
typename genType::value_type const & eta);
然后我得到了这个:
template<typename N>
N reflect(N const & i, N const & n, typename N::value_type const & eta)
{
return glm::N refract(i,n,eta);
}
BOOST_PYTHON_MODULE(foo)
{
def("reflect", reflect<float>);
def("reflect", reflect<double>);
}
在编译时我遇到了以下错误:
错误 C2780: 'void boost::python::def(const char *,F,const A1 &,const A2 &,const A3 &)' : 期望5个参数 - 只提供了2个
我应该怎么包装它呢?
-----编辑------
这个可以正常工作:
template<class T>
T floor(T x)
{
return glm::core::function::common::floor(x);
}
BOOST_PYTHON_MODULE(busta)
{
def("floor", floor<double>);
def("floor", floor<float>);
}
根据参考资料,floor()
的定义如下:
template< typename genType >
genType floor (genType const &x)
我可以把这个编译成一个DLL,然后在Python中导入它并使用floor()。生活感觉真不错……但是……
这个方法不行,我想知道为什么:
template<class genType >
genType reflect (genType i, genType n, genType eta)
{
return glm::core::function::geometric::refract(i, n,eta);
}
BOOST_PYTHON_MODULE(busta)
{
def("reflect", reflect<float>);
}
refract()的定义在这篇文章的开头。
我现在得到的错误是:
1>foo.cpp(37): error C2893: Failed to specialize function template 'genType glm::core::function::geometric::refract(const genType &,const genType &,const genType::value_type &)'
1> With the following template arguments:
1> 'float'
1> foo.cpp(60) : see reference to function template instantiation 'genType
`anonymous-namespace'::reflect<float>(genType,genType,genType)' being compiled
1> with
1> [
1> genType=float
1> ]
1>
1>Build FAILED.
1 个回答
3
这个答案并不是完美的,因为它需要对类型系统进行一些不太规范的使用,并且需要写很多额外的代码来连接不同的部分。
你可以尝试定义一个包装模板,来装饰你的目标类型,这样它就会有必要的类型定义,以满足调用这个函数(reflect)的要求。
这个例子展示了这种方法的一些不足之处。注意,这个 reflect 函数只是进行简单的加法;然而,为了让 C++ 识别模板类型 N 的加法操作符(operator+),这个包装器必须明确地定义它。
#include <iostream>
using namespace std;
template<class N>
N reflect(const N& n, const typename N::value_type& t)
{
return n + t;
}
template<class N>
struct wrapper
{
typedef N value_type;
wrapper(const N& n):_n(n){}
operator N& () { return _n; }
N operator+ (const N& r) const { return _n + r; }
N _n;
};
int main(int,char**)
{
cout << reflect( wrapper<double>(1), 2.0) << endl;
return 0;
}