【发布时间】:2016-06-05 14:47:04
【问题描述】:
我有以下 cpp 代码:
#include <iostream>
#include <limits>
// C2589 when compiling with specialization, fine when compiling without
template<typename T>
void foo(T value = std::numeric_limits<T>::infinity() )
{
}
// this specialization causes compiler error C2589 above
template<>
void foo<float>( float value )
{
}
int main()
{
foo<float>();
return 0;
}
当我尝试使用 Visual Studio 2013 编译它时,我收到以下错误:
..\check2\main.cpp(5) : error C2589: '::' : illegal token on right side of '::'
..\check2\main.cpp(5) : error C2059: syntax error : '::'
如果我不包含专业化foo<float>,程序编译得很好。该代码在 gcc 4.8.4 下也可以很好地编译包括专业化,这表明 Visual C++ 编译器存在问题。
代码是否正确,是否应该编译?是否有针对 Visual C++ 的解决方法?
【问题讨论】:
-
我在 VS2015 上也有同样的错误,但它编译 here.
-
@Ben:好点子。我不知道这个网站。我不知道他们使用的是哪个编译器,但由于它编译成功,我假设它是 gcc。我很困惑为什么它用 gcc 编译但用 VC++ 失败。
标签: c++ templates visual-c++