模板专门化GCC的语法错误,但MSVC没有

Syntax error at template specialization GCC, but not MSVC

本文关键字:MSVC 没有 错误 语法 专门化 GCC      更新时间:2023-10-16

下面的代码使用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));
    }
};

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

作为扩展,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的意义。使用宏做任何事情都没有帮助自己,恰恰相反。

通常,GCC会给出行号。也许您正在使用一些c++语言特性,这些特性在最近的GCC中得到了更好的支持。你试过GCC 4.6吗?并且GCC可以被赋予参数(像这里或者更可能是-std=c++0x)来管理它所接受的方言。我相信最近的GCC(即g++ 4.6)在语言标准一致性方面做了很多努力。GCC 4.6甚至可以在错误消息中给出列号。

相关文章: