C ++结构成员模板函数的显式专业化-这是Visual Studio的问题吗? [英] 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;
}
它可以在Cygwin g ++ 4.3.4中编译并正常工作,显示0.2 0.4
,还可以在 Comeau Online上编译编译器.但是,Visual Studio C ++ 2010 Express给出以下错误消息:
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
当我将功能更改为自由功能时,错误消息更改为
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
因此,有两个问题: 1.我的代码是否合法C ++ 2.如果是这样,这是Visual Studio C ++ 2010编译器的已知问题吗?
So, two questions: 1. is my code legal C++ 2. if so, is this a known problem with Visual Studio C++ 2010 compiler?
推荐答案
好吧,我会说这很可能是合法的c ++代码,因为我可以通过以下方式对其进行编译和运行:
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
特别是:
如果已经通过模板类专门化对函数进行了专门化,则在类外部对成员函数进行显式专门化是无效的. (C2910).
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).
来自 http://msdn.microsoft. com/en-us/library/h62s5036(v = vs.80).aspx
这篇关于C ++结构成员模板函数的显式专业化-这是Visual Studio的问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!