Robin Robin - 3 months ago 19
C++ Question

Function pointer as template argument and signature

Can I pass a "general" function pointer as an template argument with it's signature? I know that I can pass the function signature to a template:

template<typename signature>
struct wrapper;

template<typename RT, typename... ATs>
struct wrapper<RT (ATs...)> {};

int f(int, double)

wrapper<decltype(f)> w;


I also can pass an function pointer as a non-type template argument:

template<int (*pF)(int, double)> myTemp() {
pf(1, 1.0);
}

myTemp<f>();


What I would like to do is something like this

template<typename RT (*pF)(typename ATs...)>


Is this possible? The function pointer must be passed as a template argument and must not be passed as a function parameter.




I want to use the template to wrap c functions and make them callable from lua. The following code works (c++14, gcc, lua-5.3), but could be improved.

#include <iostream>
#include <type_traits>

extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}

using namespace std;

int add(int i, int j) {
cout << "adding " << i << " to " << j << "." << endl;
return i + j;
}

int sub(int i, int j) {
cout << "subtracting " << j << " from " << i << "." << endl;
return i - j;
}

// ****************************

template<typename signature>
struct wrapper;

template<typename RT, typename... ATs>
struct wrapper<RT (ATs...)> {

template<RT (*pF)(ATs...)>
void reg(lua_State *L, const char*n) {
auto lw = [](lua_State *L) -> RT {
lua_pushnumber(L, call<0>(pF, L));
return 1;
};
lua_pushcfunction(L, lw);
lua_setglobal(L, n);
}

template<int i, typename... ETs>
static
typename std::enable_if<i != sizeof...(ATs), RT>::type
call(RT (*f)(ATs...), lua_State *L, ETs... Es) {
auto arg = lua_tonumber(L, i+1);
return call<i+1>(f, L, Es..., arg);
}

template<int i, typename... ETs>
static
typename std::enable_if<i == sizeof...(ATs), RT>::type
call(RT (*f)(ATs...), lua_State *L, ETs... Es) {
return f(Es...);
}

};

#define regLua(L, fct, str) wrapper<decltype(fct)>().reg<fct>(L, str)

int main() {
lua_State *L = luaL_newstate();
luaL_openlibs(L);

luaL_dostring(L, "print(\"Hello World!\")");

// Ugly: add must be passed two times! Not a very userfriendly syntax.
wrapper<decltype(add)>().reg<add>(L, "add");
// Looks better, but uses a macro...
regLua(L, sub, "sub");
// optimal (but possible??):
// wrap<sub>(L, "sub");

luaL_dostring(L, "print(\"add:\", add(3, 5))");
luaL_dostring(L, "print(\"sub:\", sub(3, 5))");

lua_close(L);

return 0;
}

Answer

c++17 would allow

template <auto value> struct wrapper;

and then your specialization

template<typename RT, typename... ATs, RT (*pF)(ATs...)>
struct wrapper<pF> {
    static void reg(lua_State *L, const char* n) {
        auto lw = [](lua_State *L) {
            lua_pushnumber(L, call(L, std::index_sequence_for<ATS...>()));
            return 1;
        };
        lua_pushcfunction(L, lw);
        lua_setglobal(L, n);
    }

    template<std::size_t ... Is>
    static
    RT call(lua_State *L, std::index_sequence<Is...>) {
        return pF(lua_tonumber(L, 1 + Is)...);
    }
};

And usage:

wrapper<&add>::reg(L, "add");

Before c++1z, your wrapper has to be

template <typename Sig, sig Pf> struct wrapper;

template<typename RT, typename... ATs, RT (*pF)(ATs...)>
struct wrapper<Rt (*)(ATS...), pF> {
    // Code
};

which forces you to repeat the function name (if you don't manually type its signature)

wrapper<decltype(&add), &add>::reg(L, "add");
Comments