模板专业化 GCC 出现语法错误,但 MSVC 没有语法错误

发布于 2024-12-14 00:59:05 字数 1247 浏览 2 评论 0原文

以下代码使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心荒芜 2024-12-21 00:59:06

作为扩展,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 的意义。使用宏并不能帮助你自己做任何事,恰恰相反。

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.

// 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));
}

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 of CREF either. You're not helping yourself using macros for anything, quite the contrary.

安人多梦 2024-12-21 00:59:06

通常,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 (ie g++ 4.6) made a lot of efforts on language standard conformance. And GCC 4.6 can even give you column numbers in error messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文