I have a highly polymorphic c++ function that can be called with nearly 20 diferent types.
In order to expose it to python I’m doing something like this:
#include originalFunctionNamespace.hpp
template<class T>
T foo(T x)
{
return orignalFunctionNamespace::foo(x);
}
but then to call them from python I have to specialize the functions with each type they support:
BOOST_PYTHON_MODULE(module_foo)
{
def("foo", foo<orignalFunctionNamespace::type1>);
def("foo", foo<orignalFunctionNamespace::type2>);
def("foo", foo<orignalFunctionNamespace::type3>);
def("foo", foo<orignalFunctionNamespace::type4>);
def("foo", foo<orignalFunctionNamespace::type5>);
def("foo", foo<orignalFunctionNamespace::type6>);
def("foo", foo<orignalFunctionNamespace::type7>);
...
...
...
def("foo", foo<orignalFunctionNamespace::typeN>);
}
This works, but I can’t stop thinking that there must be a smarter way to do it. Since I have to do it for many many functions, things are getting big and super repetitive.
Any suggestions?
Looks like a possible use for typelist magic, to me.
If you’re using (or can use) the new C++0x standard, you can write something like this:
If you don’t have access to the new standard, you can do the equivalent thing using the old recursive typelists.