C++ 的显式专业化结构成员模板函数 - 这是 Visual Studio 问题吗?
我在模板专业化方面遇到问题,可以归结为以下代码片段:
#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 给出以下错误消息:
error C2910: 'Class::fun' : cannot be explicitly specialized
error C2910: 'Class::fun' : cannot be explicitly specialized
编辑:当我将函数更改为自由函数时,错误消息更改为
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 编译器的已知问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我想说它很可能是合法的 C++ 代码,因为我编译并运行它很好:
我怀疑它与这里提到的错误相同: http://msdn.microsoft.com/en-us/library/cx7k7hcf(v=vs.80).aspx
特别是:
来自 http://msdn.microsoft.com/en -us/library/h62s5036(v=vs.80).aspx
Well, I'd say it is most likely legal c++ code as I compile and run it fine with:
I'm suspecting it's the same bug mentioned here: http://msdn.microsoft.com/en-us/library/cx7k7hcf(v=vs.80).aspx
Particularly:
from http://msdn.microsoft.com/en-us/library/h62s5036(v=vs.80).aspx