指向静态成员函数的指针是“无效的”作为g ++的模板参数 [英] pointer to static member function is "invalid" as a template argument for g++

查看:179
本文介绍了指向静态成员函数的指针是“无效的”作为g ++的模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的一些实际代码,处理暴露C ++类到Lua,示例:

This is some of my actual code that deals with exposing C++ classes to Lua, exemplified:

#include <lua5.1/lua.hpp>
#include <tuple>

//in actual code, lots of specializations of these C++<=>Lua's stack helper functions. Here it's just a sample.
template<typename T> T to(lua_State*, int);
template<> int to(lua_State* l, int i){
    return lua_tointeger(l, i);
}
template<typename T> void push(lua_State*, T);
template<> void push(lua_State* l, int val){
    lua_pushinteger(l, val);
}

//in actual code, placed in a header
template<typename T, T> class function_proxy{
    static_assert(sizeof(T)!=sizeof(T), "Error: function_proxy works with functions (duh)");
};

template<typename Return, typename... Args, Return(*func)(Args...)> class function_proxy<Return(*)(Args...), func>{
    static Return call(lua_State* l, Args... args){
        return func(args...);
    }
    template<typename... retrieved> static Return call(lua_State* l, retrieved... read){
        return call(l, read..., to<typename std::tuple_element<sizeof...(read), std::tuple<Args...> >::type >(l, 1+sizeof...(read)));
    }
public:
    static int wrapper(lua_State* l){
        push(l, call(l));
        return 1;
    }
};

//in actual code, inner class of a template class in another header
template<typename CT, CT> class member_helper{
    static_assert(sizeof(CT)!=sizeof(CT), "Error: member_helper works with members of T (duh)");
};
//Just one of the actual partial specializations, to combine constness and the return of void or non-void
template<typename Class, typename Return, typename... Args, Return(Class::*fun)(Args...)> struct member_helper<Return(Class::*)(Args...), fun>{
    static Return as_free(Class& obj, Args... args){
        return (obj.*fun)(args...);
    }
    static int worker(lua_State* l, Class& obj, bool is_const, bool write){
        if(write) throw "Cannot write a member function.";
        //ERROR HERE: template argument 2 is invalid. Not very helpful message.
        lua_pushcclosure(l, function_proxy<decltype(&as_free), &as_free>::wrapper, 0);
        return 1;
    }
};

struct Test{
    int test(int arg){ return arg*3; }
};

int test_as_free(int arg){ return arg*3; }

int main(){
    lua_State* l=luaL_newstate();
    Test t;
    //works fine
    lua_pushcclosure(l, function_proxy<decltype(&test_as_free), &test_as_free>::wrapper, 0);
    //does not work
    member_helper<decltype(&Test::test), &Test::test>::worker(l, t, false, false);
}

当获取指针 function_proxy< decltype (& as_free),& as_free> :: wrapper ,尽管在​​ main 中做了一个非常相似的事情。我不认为获得自由函数的指针,而不是静态成员函数之间有任何区别。这是这个 g ++错误的一个实例(请注意,我使用 -std = c ++ 0x ,似乎该错误在C ++ 11中已修复)

The code fails when getting the pointer function_proxy<decltype(&as_free), &as_free>::wrapper, even though a very similar thing is done in main. I don't think that there's any difference between getting the pointer of a free function rather than a static member function. Is this an instance of this g++ bug (note that I compile with -std=c++0x and it appears that the bug was fixed in C++11)?

推荐答案

这是 g ++错误,已被确认。

这篇关于指向静态成员函数的指针是“无效的”作为g ++的模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆