4

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?

quant_dev
  • 6,181
  • 1
  • 34
  • 57
  • 2
    This probably isn't relevant, but shouldn't parameter `a` be passed by reference just like `x`? (`void fun(double (&a)[N]), ...`) – Alan Stokes May 21 '11 at 19:28
  • @Alan Yes, it makes the code work in VS 2010... but WHY? – quant_dev May 21 '11 at 19:30
  • Also, the old code compiled without problems in other compilers. – quant_dev May 21 '11 at 19:41
  • @quant: Without the array reference, the array dimensions don't do anything. So you could call `fun<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. – Ben Voigt May 22 '11 at 01:10
  • Maybe it's a bug in the other compilers? I was hoping for an enlightening quotation from The Standard. The workaround is not that better, because with the reference, I don't have my array on the stack. I guess I'll switch to boost::array (which is also stack based). – quant_dev May 22 '11 at 09:35
  • This may be still broke in Visual Studio 2013. Also see Barry's answer at [Explicit specialization of member function template in source file](https://stackoverflow.com/a/30530378/608639). The simple forward declaration is enough to sidestep an error about already being specialized. – jww Nov 17 '17 at 08:34

1 Answers1

4

Well, I'd say it is most likely legal c++ code as I compile and run it fine 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'm suspecting it's the same bug mentioned here: http://msdn.microsoft.com/en-us/library/cx7k7hcf(v=vs.80).aspx

Particularly:

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

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

matiu
  • 7,469
  • 4
  • 44
  • 48
  • 1
    "Builds in g++" is not equivalent to "is legal C++". – quant_dev May 26 '11 at 09:31
  • 2
    I don't think my answer deserved a thumbs down quant_dev. Read the whole thing before thumbing. I didn't say "is legal c++". – matiu Jun 01 '11 at 13:22
  • Well, my question was "is it legal C++". – quant_dev Jun 05 '11 at 09:22
  • I'm thinking the answer is 'most likely yes'. And the answer to your other question "is this a known problem with Visual Studio C++ 2010" is 'most likely yes, check the link for more info: http://msdn.microsoft.com/en-us/library/h62s5036(v=vs.80).aspx - sorry I couldn't be more specific. – matiu Jun 10 '11 at 00:57
  • "The explicit specialization of a member function outside the class is not valid if the function has already been explicitly specialized via a template class specialization. (C2910)." – matiu Jun 10 '11 at 10:22
  • But I have no template class specializations for Class. It isn't even a template. How can this be applicable here? – quant_dev Jun 10 '11 at 10:55
  • Sorry man. You're totally right. I was misreading the MS bug and applying it to the template function inside the class. It would kind of fit if it were to say "..explicitly specialized via a template **method inside of a** class specialization". Good job on the vote down :) /me resigns – matiu Jun 11 '11 at 01:24
  • 2
    Visual Studio 2008 gives this same error. The fix is to remove `template<>` from the definition, as @matiu's first link indicates in the last bit of sample code. – dash-tom-bang Oct 31 '13 at 07:13