模板专业化 GCC 出现语法错误,但 MSVC 没有语法错误
以下代码使用 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为扩展,MSVC 允许在类中专门化成员函数,但这不是标准。
如果您希望专门化成员函数,则应该在命名空间级别进行。
另外,
FORCE_INLINE
有点错误,inline
是一个提示,而不是对编译器的命令,所以你没有强迫任何东西。我也不太明白CREF
的意义。使用宏并不能帮助你自己做任何事,恰恰相反。MSVC allows, as an extension, to specialize member functions right within the class, however this is not Standard.
If you wish to specialize member functions, you should do it at namespace level.
Also,
FORCE_INLINE
is a bit erroneous,inline
is a hint, not an order to the compiler, so you're not forcing anything. And I dont quite see the point ofCREF
either. You're not helping yourself using macros for anything, quite the contrary.通常,GCC 会为您提供行号。也许您正在使用一些 C++ 语言功能,这些功能在最新的 GCC 中得到了更好的支持。你尝试过 GCC 4.6 吗?并且可以为 GCC 提供参数(例如 此处 或更可能
-std=c++0x
) 控制它接受的方言。我相信最近的GCC(即g++
4.6)在语言标准一致性方面做了很多努力。 GCC 4.6 甚至可以在错误消息中为您提供列号。Usually, GCC gives you line numbers. And perhaps you are using some C++ language features which are better supported in very recent GCC. Did you try GCC 4.6? And GCC can be given arguments (like here or more probably
-std=c++0x
) governing the dialect it is accepting. I believe that recent GCC (ieg++
4.6) made a lot of efforts on language standard conformance. And GCC 4.6 can even give you column numbers in error messages.