[英]Explicit specialization of C++ struct member template functions - is this a Visual Studio problem?
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 . 它编译并正常工作,在Cygwin g ++ 4.3.4中显示
0.2 0.4
,并在Comeau Online编译器中编译 。 However, Visual Studio C++ 2010 Express gives the following error message: 但是,Visual Studio C ++ 2010 Express提供以下错误消息:
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? 那么,有两个问题:1。我的代码是合法的C ++ 2.如果是这样,这是Visual Studio C ++ 2010编译器的已知问题吗?
Well, I'd say it is most likely legal c++ code as I compile and run it fine with: 好吧,我说它很可能是合法的c ++代码,因为我编译并运行良好:
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 我怀疑它是这里提到的同一个bug: 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).
(C2910)。
from http://msdn.microsoft.com/en-us/library/h62s5036(v=vs.80).aspx 来自http://msdn.microsoft.com/en-us/library/h62s5036(v=vs.80).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.