模板专门化GCC的语法错误,但MSVC没有
Syntax error at template specialization GCC, but not 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
作为扩展,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甚至可以在错误消息中给出列号。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 如果没有malloc,链表实现将失败
- 为什么 gcc 编译这个而 msvc 没有
- 正在解码MSVC 32位版本的程序集(作业).没有手术做什么
- 没有'str'成员在 GCC 和 Clang 'std::basic_ostream<char>',但 MSVC 没有问题
- 如何使用CLion和MSVC编译器工具链编译wxWidgets Hello World应用程序而没有错误?
- 尝试使用 MSVC 从源代码构建 qt 创建器:无法打开包含文件:"stddef.h":没有这样的文件或目录
- GCC/Clang上的模板错误,但在MSVC上没有错误
- 有没有办法禁用 MSVC 输出中的".pdata"/"ExceptionDir"和"UNWIND_INFO"?
- GCC称将功能与多个继承过载时,称其为模棱两可,但Clang和MSVC没有
- 在 MSVC 上,为什么没有出现 std::string 的警告"unreferenced formal parameter"?
- 创建两个或多个std::向量时没有bad_alloc,它们一起超过了内存(MSVC)
- 有没有一种简单的方法可以将库文件包含在MSVC中
- MSVC编译器有没有像"#pragma ignore this sourcefile"这样的东西?
- 为什么gcc和msvc允许const对象没有用户提供的构造函数?这是个bug吗
- 在GCC中,Clang和MSVC有没有办法符合C++98而不是C++03
- MSVC错误C2371 "redefinition; different basic types",除了我没有看到我在哪里重新定义任何东西
- 模板专门化GCC的语法错误,但MSVC没有
- 是否有可能使用编译与MSVC没有Visual Studio
- 比较int和unsigned引用时警告,但比较g++/msvc的const(无ref)时没有警告