C++结构成员模板函数的显式专用化 - 这是一个Visual Studio问题吗?

Explicit specialization of C++ struct member template functions - is this a Visual Studio problem?

本文关键字:一个 Visual 问题 Studio 成员 结构 函数 专用 C++      更新时间:2023-10-16

我在模板专用化方面遇到了问题,归结为以下代码片段:

#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编译器的已知问题吗?

好吧

,我会说它很可能是合法的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

我怀疑这是这里提到的相同错误:http://msdn.microsoft.com/en-us/library/cx7k7hcf(v=vs.80).aspx

特别:

如果成员函数已通过模板类专用化显式专用化,

则类外部成员函数的显式专用化无效。(C2910)。

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

相关文章: