C ++ template explicit template functions specialization - is this a Visual Studio problem?

I have a problem with the specialization of the template, which boils down to the following snippet:

#include <iostream> struct Class { template <unsigned int N> static void fun(double a[N], double (&x)[N+1]); }; template <> inline void Class::fun<1u>(double a[1u], double (&x)[2u]) { x[0] += 0.2; } template <> inline void Class::fun<2u>(double a[2], double (&x)[3]) { x[0] += 0.4; } int main(void) { double x[1] = {0}; double a[2] = {0, 1}; double b[3] = {0, 0, 1}; Class::fun<1>(x, a); Class::fun<2>(a, b); std::cout << a[0] << " " << b[0] << std::endl; return 0; } 

It compiles and works correctly, displaying 0.2 0.4 , in Cygwin g ++ 4.3.4, and also compiles in the Comau Online compiler . However, Visual Studio C ++ 2010 Express gives the following error message:

 error C2910: 'Class::fun' : cannot be explicitly specialized error C2910: 'Class::fun' : cannot be explicitly specialized 

EDIT: when I changed the function as a free function, the error message changed to

 error C2912: explicit specialization; 'void fun<1>(double [],double (&)[2])' is not a specialization of a function template 

So, two questions: 1. my code is finished C ++ 2. if so, is this a known issue with the Visual Studio C ++ 2010 compiler?

+4
source share
1 answer

Well, I would say that this is the most likely legitimate C ++ code when I compile and run it with:

 g++ -ansi -gstabs+ -Wall -o fun fun.cpp g++ -std=c++98 -gstabs+ -Wall -o fun fun.cpp g++ -std=c++0x -gstabs+ -Wall -o fun fun.cpp 

I suspect the error indicated here is: http://msdn.microsoft.com/en-us/library/cx7k7hcf(v=vs.80).aspx

In particular:

An explicit specialization of a member function outside the class is not valid if the function has already been explicitly specialized using the specialization of the template class. (C2910).

from http://msdn.microsoft.com/en-us/library/h62s5036(v=vs.80).aspx

+4
source

All Articles