【问题标题】:Function pointer as template argument and signature函数指针作为模板参数和签名
【发布时间】:2016-08-24 19:00:32
【问题描述】:

我可以将“通用”函数指针作为带有签名的模板参数传递吗?我知道我可以将函数签名传递给模板:

template<typename signature>
struct wrapper;

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

int f(int, double)

wrapper<decltype(f)> w;

我也可以将函数指针作为非类型模板参数传递:

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

   myTemp<f>();

我想做的是这样的

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

这可能吗?函数指针必须作为模板参数传递,不能作为函数参数传递。


我想使用模板来包装 c 函数并使它们可以从 lua 调用。以下代码有效(c++14、gcc、lua-5.3),但可以改进。

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

【问题讨论】:

  • 我在这里每周阅读function pointer as template argument 大约五到七次(如果不是更频繁的话)。确定您找不到任何已经可用的主要信息?
  • 我不知道myTemp() 应该是什么。也许您错过了返回类型?
  • @πάνταῥεῖ 我搜索了函数指针作为模板参数,但我发现的只是前两种情况,而不是我感兴趣的第三种情况。如果您有有关此案例的链接,我将不胜感激。

标签: c++ templates lua function-pointers


【解决方案1】:

c++17 允许:

template <auto value> struct wrapper;

然后是你的专业

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

及用法:

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

在 c++17 之前,你的 wrapper 必须是

template <typename Sig, sig Pf> struct wrapper;

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

这会迫使您重复函数名称(如果您不手动输入其签名)

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

【讨论】:

  • 反正这个问题没有 c1z 标签,所以我想这并不能真正回答它。我错了吗?
  • @skypjack: pre-c++1z,不支持该语法,所以要真正回答OP的问题,不,不可能使用类似的语法。
  • 嗯,那个(不,不可能)确实是一种回应。然后,添加有关即将推出的功能的信息当然会丰富答案。 :-)
  • @Jarod42 非常感谢。这回答了我的问题。如果我的编译器版本已经可以使用 c++17 语法,我会尝试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 2021-12-10
相关资源
最近更新 更多