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

发布于 2024-11-09 10:10:11 字数 1280 浏览 0 评论 0原文

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

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彻夜缠绵 2024-11-16 10:10:11

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文