指向静态成员函数的指针"invalid"为 G++ 的模板参数
pointer to static member function is "invalid" as a template argument for g++
这是我的一些实际代码,用于向Lua公开C++类,例如:
#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 中修复)?
这是一个 g++ 错误,并且已被承认。
我还没有完全理解它,但是您确定希望类是decltype(&Test::test)并返回为&Test::test吗?对我来说,它看起来应该是:
member_helper<Test, int>::worker
但是,通过添加一个接一个类型的间接寻址来从工作线到非工作线呢?
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 在派生函数中指定void*参数
- 视图中的参数推导失败:take_while
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 使用指向成员的指针将成员函数作为参数传递
- 没有名称的C++模板参数
- 如何将enable-if与模板参数和参数包一起使用