Compiler error when using specialized specialization in Visual C ++

I have the following cpp code:

#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; } 

When I try to compile this using Visual Studio 2013, I get the following error:

 ..\check2\main.cpp(5) : error C2589: '::' : illegal token on right side of '::' ..\check2\main.cpp(5) : error C2059: syntax error : '::' 

The program compiles fine if I do not include the foo<float> specialization. The code also compiles fine , including specialization under gcc 4.8.4, which indicates some problems with the Visual C ++ compiler.

Is the code correct and should it compile? Is there a workaround for Visual C ++?

+6
source share
2 answers

Instead of specializations, I overload without the keyword "template", for example:

 template<typename T> void foo(T value) { } void foo(float value) { } 

I use them in gcc and Visual Studio 2012.

-1
source

Omitting the parameter when calling foo<float>(); , you put the compiler in the puzzle. Compiler simultaneously concludes that specialized function is correct, because you explicitly say <float> , and not the one because there is no option. Then the compiler reaches the general version, but cannot, because there is a specialized one. Even the HAL9000 could not understand that one unless it was built with gcc. VC ++ does not handle the situation correctly. Probably a mistake, not "by design."

The workaround for Visual C ++ is to use overload:

 template<typename T> void foo(T value) { } template<typename T> void foo() { foo(std::numeric_limits<T>::infinity()); } 

And name it as usual foo<float>();

+3
source

All Articles