I have a problem with template specialization 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 Comeau 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 to be 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. is my code legal C++ 2. if so, is this a known problem with Visual Studio C++ 2010 compiler?
a
be passed by reference just likex
? (void fun(double (&a)[N]), ...
) – Vasilikivasilisfun<2>(x, b); fun<1>(a, a);
. So yes this looks like a bug in VC++, but you have a workaround that's actually better on all compilers. – Cassiopeia