a1ien a1ien - 1 year ago 95
C++ Question

Template function wrapper

I'm trying to wrap any function into a function that takes one argument (state Interpreter). And if I pass the function directly, all works well.
But if I wrap in an additional function, I get a compiler error.
Explain please, what am I doing wrong.

#include <iostream>

template <std::size_t... Is>
struct _indices {
template <template <size_t...> class Receiver>
using Relay = Receiver<Is...>;

template <std::size_t N, std::size_t... Is>
struct _indices_builder : _indices_builder<N-1, N-1, Is...> {};

template <std::size_t... Is>
struct _indices_builder<0, Is...> {
using type = _indices<Is...>;

struct lua_State

template <typename Ret, typename... Args>
struct FunctionWrapperImpl {
template <size_t... Indices>
struct ImplementationNonVoid {
template <Ret (* func)(Args...)> static inline
int invoke(lua_State* state) {
return 1;

using Implementation =
typename _indices_builder<sizeof...(Args)>::type::template Relay<

template <typename ToBeWrapped>
struct Wrapper {

template <typename Ret, typename... Args>
struct Wrapper<Ret (*)(Args...)>:
FunctionWrapperImpl<Ret, Args...>::Implementation {};

int test(int a)
std::cout<< a;
return 5;

typedef int (*lua_CFunction) (lua_State *L);

template <typename T>
lua_CFunction register_func(T fun)
lua_CFunction f =
(&Wrapper<decltype (fun)>::template invoke<T>); // Error
// no matches converting function 'invoke' to type 'lua_CFunction {aka int (*)(struct lua_State*)}'
//do somthing with f
return f;

int main(int argc, char *argv[])
lua_State s;

lua_CFunction t = (&Wrapper<decltype(&test)>::template invoke<&test>); // work
t(&s); // no problem
lua_CFunction t2 = register_func(&test);

return 0;

Full compilation error.

main.cpp: In instantiation of 'int (* register_func(T))(lua_State*) [with T = int (*)(int); lua_CFunction = int (*)(lua_State*)]':
main.cpp:69:40: required from here
main.cpp:58:65: error: no matches converting function 'invoke' to type 'lua_CFunction {aka int (*)(struct lua_State*)}'
lua_CFunction f = (&Wrapper<decltype (fun)>::template invoke<T>);
main.cpp:25:7: note: candidate is: template<int (* func)(int)> static int FunctionWrapperImpl<Ret, Args>::ImplementationNonVoid<Indices>::invoke(lua_State*) [with Ret (* func)(Args ...) = func; long unsigned int ...Indices = {0ul}; Ret = int; Args = {int}]
int invoke(lua_State* state) {

Answer Source

There is significant difference between:

(&Wrapper<decltype (fun)>::template invoke<T>);


(&Wrapper<decltype(&test)>::template invoke<&test>);

in the former you use T which is obviously the type of the function you pass in the latter you provide the pointer to function to the invoke method. As invoke method accepts non-type template parameter, here function pointer, the second code compiles correctly. Remember - you cannot mix non-type template parameters with type template parameters just like you cannot mix type template parameters with template template parameters.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download