用boost.python包装模板函数 我试图用Boosi.python公开以下的C++函数到Python: template <typename genType> genType refract( genType const & I, genType const & N, typename genType::value_type const & eta);

用boost.python包装模板函数 我试图用Boosi.python公开以下的C++函数到Python: template <typename genType> genType refract( genType const & I, genType const & N, typename genType::value_type const & eta);,c++,python,templates,boost,word-wrap,C++,Python,Templates,Boost,Word Wrap,我可以将其构建为一个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("ref

我可以将其构建为一个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>);
}
模板
GentypeReflect(GentypeI、GentypeN、GentypeETA)
{
返回glm::core::function::geometric::refract(i,n,eta);
}
BOOST_PYTHON_模块(busta)
{
def(“反射”,反射);
}
折射()定义在这篇文章的顶部

我现在得到的错误是:

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>foo.cpp(37):错误C2893:未能专门化函数模板“genType glm::core::function::geometric::refract(const genType&,const genType&,const genType::value_type&)”
1> 使用以下模板参数:
1> “浮动”
1> cpp(60):参见函数模板实例化“genType”的参考
`正在编译匿名命名空间“::reflect(genType,genType,genType)”
1> 与
1>          [
1> genType=浮动
1>          ]
1>
1> 生成失败。

这不是一个完美的答案,因为它需要滥用类型系统并编写大量额外的粘合代码。
您可以尝试定义一个包装器模板来修饰您的目标类型,使其具有必要的typedef来满足调用函数(reflect)

这个例子说明了这种方法的缺点。注意,这个reflect函数执行一个简单的加法;但是,对于C++,为了识别模板+类型的操作符+,包装器必须显式定义它。p>
#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;
}
#包括
使用名称空间std;
模板
N反映(常量N&N,常量typename N::value\u type&t)
{
返回n+t;
}
模板
结构包装器
{
类型定义N值_类型;
包装器(常数N&N):\N(N){}
运算符N&({return}
N运算符+(常数N&r)常数{return\u N+r;}
N N ;;
};
int main(int,char**)
{

首先,你怎么能期望类型
float
有一个类型定义的名称
value\u type
?@Dark这确实是一个问题,但是你认为这是唯一的问题吗?我不知道。我不明白这个函数试图做什么。到目前为止,你所做的只是将
value\u type
问题移到一个级别无论是<代码> GeType < /C> >,它不能是浮动的或是双的。它需要嵌套类型名<代码> ValueSype类型< /COD>。通常这意味着<代码> Guangype 必须是标准C++中的容器或迭代器。
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>);
}
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.
#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;
}