c++ - 模板特化 GCC 的语法错误,但不是 MSVC

标签 c++ templates visual-c++ gcc syntax

以下代码使用 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 的意义。恰恰相反,您没有帮助自己将宏用于任何事情。

关于c++ - 模板特化 GCC 的语法错误,但不是 MSVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8022293/

相关文章:

json - Phoenix : render JSON from a template

c++ - Boost 容器无法使用未定义(但已声明)的类进行编译

exception - 捕捉导出(1);

c++ - 在 C++ 结构中显示垃圾值

c++ - 如何从模板类中的类型名传递创建新的类型名

windows - 为什么Visual Studio 运行时库源代码存放在两个目录中?

c++ - C++ 中的值访问

c++ - 在 .o 文件中本地化函数体 block

c++ - UnitTest++创建cmd窗口,无法关闭

c++ - C++ STL map 如何管理内存,以及如何绕过它