I want to pass a function value as a template parameter to a function. Currently the best I managed to do is :
template< typename F, F f >
void pass()
{
...
}
...which is used:
pass< decltype(&func), &func >();
What I would really like is to have:
pass< &func >();
Is there any way to achieve this without macros? Basically to pass both the type and the value at the same time? The compiler obviously has all the information needed for that...
The solution must work with variable parameters and return types. The function value is used at compile time, so it cannot be passed as an argument.
C++11 solutions welcome.
Edit: use case - I'm generating bindings at compile-time, where I need to create a C++ function for each passed function. The use case of this code looks (simplified) more or less like this:
template < typename F, F f >
int function_wrapper( lua_State* L )
{
return dispatcher<typename return_type<F>::type>::call( L, 1, f );
}
void register_native_function( lua_Function f, const char* name )
{
// binding call using pure C function f
}
template < typename F, F f >
void register_function( const char* name )
{
register_native_function( function_wrapper< F, f >, name );
}
Please note that I need to create a compile-time function wrapper, so I need the pass function value at compile time. There are binding solutions that allow binding at runtime, but they always require boilerplate code compared to hand-written bindings. I'm aiming to achieve a hand-written performance here.
I believe shortening this is currently impossible. A year ago, the C++ committee looked at http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3601.html to fix this, and they encouraged the authors to pursue it further after C++14 was released.