以下代码使用 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/