C++ 类主体外的模板定义:VC2010 vs GCC 4.9
我可以用GCC4.9编译这段代码C++ 类主体外的模板定义:VC2010 vs GCC 4.9,c++,visual-studio-2010,templates,arguments,definition,C++,Visual Studio 2010,Templates,Arguments,Definition,我可以用GCC4.9编译这段代码 struct Space2 { static const int Size = 2; }; struct Space3 { static const int Size = 3; }; template <typename Space> struct A { void f(int a[Space::Size]); //error C2244 }; template <typename Space> void A<Sp
struct Space2 {
static const int Size = 2;
};
struct Space3 {
static const int Size = 3;
};
template <typename Space>
struct A {
void f(int a[Space::Size]); //error C2244
};
template <typename Space>
void A<Space>::f(int a[Space::Size]) {
}
int main() {
typedef Space2 Space;
int a[Space::Size];
A<Space> m;
m.f(a);
return 0;
}
但我在VC2010编译器中有一个错误:
error C2244: 'A<Space>::f': unable to match function definition to an existing declaration
main.cpp(11): see declaration of 'A<Space>::f' definition
'void A<Space>::f(int [Space::Size])'
existing declarations
'void A<Space>::f(int [Space::Size])'
为什么?
另外,当我把f的定义放到A中时,这段代码在GCC4.9和VC2010中都编译得很好 看起来像MSVC中的一个bug。在VS2013中运行良好。避免在旧的工具版本上被卡住,这些C++中有很多变化。如果从参数DECL中删除尺寸大小的话,它也可以正常工作。然而,它仍然会吐出int&a[Space::Size],这让我有些吃惊。可能与此有关: