C++结构成员模板函数的显式专用化 - 这是一个Visual Studio问题吗?
Explicit specialization of C++ struct member template functions - is this a Visual Studio problem?
我在模板专用化方面遇到了问题,归结为以下代码片段:
#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
相关文章:
- 试图在visual studio上用C++创建一个桌面应用程序
- visual是否可以在c++中创建一个接收无限数量相同类型(或至少相当数量)参数的函数
- 使用Visual Studio在虚幻引擎中创建一个新的类c ++给了我太多的错误
- 在 Visual Studio 中构建一个没有 C/C++ 运行时的静态库
- 无法从 Visual Studio C++ 中的另一个类访问一个类
- Visual Studio Debug 只在一个线程中
- 如何创建一个windows visual Studio C++/CX NOT UWP
- 在Visual Studio中,与std::async一起使用时不调用"thread_local"变量"析构函数,这是一个错误吗?
- Visual Studio 2017 STL 可视化工具失败了一个 std::map<MyIntrusivePtr, std::tuple<....> >
- Visual Studio 2017 C++,如何定义一个"环境变量"'Additional Library Directory'?
- 如何在Visual Studio C++中声明一个二维字符串数组?
- 从命令行构建一个.sln / .vcxproj项目,并使用Visual C 的免费版本构建
- 在另一个文件中定义函数后,Visual Studio 无法识别该函数
- 使用命令行构建一个Visual Studio C 项目
- 让C++程序构建一个Visual C++项目,而无需使用system
- 在visual c++2010项目中包含一个visual c++6.0头文件
- 交叉编译从linux到windows的一个visual studio项目
- 我可以用xbuild在Linux上编译一个Visual c++项目吗?
- C++结构成员模板函数的显式专用化 - 这是一个Visual Studio问题吗?
- 如何建立一个Visual Studio 2010项目,为Slickedit 17构建64位扩展dll ?