开发者

Wrapping a template function with boost.python

I'm trying to expose the following c++ function to python using boost.python:

template <typename genType> 
    genType refract(
        genType const & I, 
        genType const & N, 
        typename genType::value_type const & eta);

and what I got is this:

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>);
    开发者_StackOverflowdef("reflect", reflect<double>);
}

and I have the following error when compiling:

error C2780: 'void boost::python::def(const char *,F,const A1 &,const A2 &,const A3 &)' : expects 5 arguments - 2 provided

How should I wrap it?

-----edit------

This works:

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>);
}

from the reference, floor() definition is the folowing:

template< typename genType >
genType floor (genType const &x)

I can build this as a DLL, then import it in python and use floor() from there. Life feels so nice...but..

This won't work, and I would like to understand why:

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() definition is on the top of this post.

the error I get now is this:

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.


This is not the perfect answer since it requires abusing the type system and writing plenty of additional glue code.
You could try defining a wrapper template to decorate your target type such that it would have the necessary typedefs to satisfy the calling function (reflect).

This example demonstrates the failings of such an approach. Notice that this reflect function performs a simple addition; however, for C++ to recognize the operator+ for the templated type N, the wrapper must explicitly define it.

#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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜