【问题标题】:is g++ smart enough to optimize recursive calls of template functions?g++ 是否足够智能以优化模板函数的递归调用?
【发布时间】: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


    【解决方案1】:

    我在#gcc @freenode 中问过。他们说是的,除非你使用函数局部变量的指针。但既然没有,答案应该是肯定的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多