【发布时间】:2012-03-25 10:00:27
【问题描述】:
我有这段代码:
template<typename... Args> class function_helper<void(Args...)>{
public:
typedef void(*generic_function)(Args...);
static void call(lua_State* l, generic_function func, Args... args){
func(args...);
}
template<typename... retrieved> static void call(lua_State* l, generic_function func, retrieved... read){
call(l, func, read..., to<typename std::tuple_element<sizeof...(read), std::tuple<Args...> >::type >(l, 1+sizeof...(read)));
}
static int wrapper(lua_State* l){
assert(lua_isuserdata(l, lua_upvalueindex(1)));
call(l, generic_function(lua_touserdata(l, lua_upvalueindex(1))));
return 0;
}
};
目的是有一个免费的函数包装器(带有任意长度的参数列表),以便lua可以通过推送function_helper::wrapper来调用它。
一切正常。我的“担心”是 g++ 不够聪明,无法理解 call 的递归调用可以替换为类似的单个调用
call(luastate, to<type1>(luastate, 1), to<type2>(luastate, 2), to<type3>(luastate, 3), ...);
我的编译器是 g++ 4.6.1。如果您有关于 g++ 4.7 或更新版本的信息,我们也欢迎。
附:
std::tuple 的使用是 g++ 4.6 限制的一种解决方法,它不能直接将 vararg 模板列表解压缩为 vararg 参数或其他东西。
【问题讨论】:
标签: templates g++ compiler-optimization