Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 函数指针作为模板参数和签名_C++_Templates_Lua_Function Pointers - Fatal编程技术网

C++ 函数指针作为模板参数和签名

C++ 函数指针作为模板参数和签名,c++,templates,lua,function-pointers,C++,Templates,Lua,Function Pointers,我可以传递一个带有签名的“通用”函数指针作为模板参数吗?我知道我可以将函数签名传递给模板: template<typename signature> struct wrapper; template<typename RT, typename... ATs> struct wrapper<RT (ATs...)> {}; int f(int, double) wrapper<decltype(f)> w; 模板 结构包装器; 模板 结构包装

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

template<typename signature>
struct wrapper;

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

int f(int, double)

wrapper<decltype(f)> w;
模板
结构包装器;
模板
结构包装器{};
整数f(整数,双精度)
包装纸w;
我还可以将函数指针作为非类型模板参数传递:

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

   myTemp<f>();
模板myTemp(){
pf(1,1.0);
}
myTemp();
我想做的是这样的事情

   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;
}
#包括
#包括
外部“C”{
#包括
#包括
#包括
}
使用名称空间std;
整数相加(整数i,整数j){
coutc++17允许:

template <auto value> struct wrapper;
强制您重复函数名(如果您不手动键入其签名)

包装器:reg(L,“添加”);
我每周在这里阅读大约五到七次(如果不是更频繁的话)。你确定找不到任何已经可用的基本信息吗?我没有得到
myTemp()
应该是什么。也许你错过了一个返回类型?@πάνταῥεῖ 我搜索了函数指针作为模板参数,但我只找到了前两种情况,而不是我感兴趣的第三种情况。如果你有关于这种情况的链接,我将不胜感激。无论如何,这个问题没有c1z标记,所以我想这并不能真正回答它。我错了吗?@skypjack:pre-c++1z,这种语法不受支持,所以要真正回答OP的问题,不,不可能使用类似的语法这确实是一个回答。然后,添加关于即将推出的功能的信息,当然丰富了答案。:-@Jarod42非常感谢。这回答了我的问题。如果我的编译器版本中已经提供了c++17语法,我会尝试。
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");
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");