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

e.p*_*kov 1 c++ syntax gcc templates visual-c++

下面的代码使用MSVC 2008编译得很好.当你构建GCC时会遇到很多错误(代码后出错).应该怎么做才能解决错误?

#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));
    }
};
Run Code Online (Sandbox Code Playgroud)

GCC提供以下错误:

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
Run Code Online (Sandbox Code Playgroud)

Mat*_* M. 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));
}
Run Code Online (Sandbox Code Playgroud)

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