首页 文章

模板特化GCC中的语法错误,但不是MSVC

提问于
浏览
1

The following code compiles fine using MSVC 2008. When you build GCC climbs a lot of errors (error after the code). What should be done to solve the error?

#define FORCE_INLINE inline
#define CREF(A) const A&

template <class F>
class RDOFunCalcStd: public RDOFunCalc
{
    ...

    template <int paramCount>
    FORCE_INLINE void calc(CREF(LPRDORuntime) pRuntime);

    template <>
    FORCE_INLINE void calc<1>(CREF(LPRDORuntime) pRuntime)
    {
        m_value = m_pFunction(getParam<F::arg1_type>(pRuntime, 0));
    }

    template <>
    FORCE_INLINE void calc<2>(CREF(LPRDORuntime) pRuntime)
    {
        m_value = m_pFunction(getParam<F::arg1_type>(pRuntime, 0), getParam<F::arg2_type>(pRuntime, 1));
    }
};

GCC provides the following errors:

error: too many template-parameter-lists

error: explicit specialization in non-namespace scope ‘class rdoRuntime::RDOFunCalcStd<F>’

error: variable or field ‘calc’ declared void

error: expected ‘;’ before ‘<’ token

error: expected ‘;’ before ‘template’

error: explicit specialization in non-namespace scope ‘class rdoRuntime::RDOFunCalcStd<F>’

error: variable or field ‘calc’ declared void

error: expected ‘;’ before ‘<’ token

2 回答

  • 5

    作为扩展,MSVC允许在类中专门化成员函数,但这不是标准的 .

    如果您希望专门化成员函数,则应在命名空间级别执行此操作 .

    // note: use "inline" so that the linker merges multiple definitions
    template <class F>
    template <>
    inline void RDOFunCalcStd<F>::calc<1>(LPRDORuntime const& pRuntime)
    {
        m_value = m_pFunction(getParam<typename F::arg1_type>(pRuntime, 0));
    }
    

    此外, FORCE_INLINE 有点错误, inline 是一个提示,而不是编译器的命令,所以你不是强迫任何东西 . 我也没有完全看到 CREF 的观点 . 你没有帮助自己使用宏来做任何事情,恰恰相反 .

  • 0

    通常,GCC会为您提供行号 . 也许您正在使用一些C语言功能,这些功能在最近的GCC中得到了更好的支持 . 你试过GCC 4.6吗?并且可以给GCC提供管理其接受的方言的参数(例如here或更多可能是-std=c++0x) . 我相信最近的GCC(即 g++ 4.6)在语言标准一致性方面做了很多努力 . GCC 4.6甚至可以在错误消息中为您提供列号 .

相关问题