当我尝试编译 CUDA 的 cudafe++ 工具生成的以下(损坏的)代码时,Visual Studio 抛出错误 C2244。这是正确的行为吗? GCC 似乎并不关心签名不匹配。
代码:
template<int Size>
class MyClass {
public:
MyClass(const int data[Size]);
};
template<int Size>
MyClass<Size> ::MyClass(const int data[]) {}
void func(MyClass<4> input) {}
输出:
test2.cpp(9) : error C2244: 'MyClass<Size>::MyClass' : unable to match function definition to an existing declaration
test2.cpp(5) : see declaration of 'MyClass<Size>::MyClass'
definition
'MyClass<Size>::MyClass(const int [])'
existing declarations
'MyClass<Size>::MyClass(const int [Size])'
最佳答案
我很确定这是不正确的。
int foo(const int []);
int foo(const int [4]);
int foo(const int *);
应该声明相同的函数。话虽如此,您可能想要的是:
template<int Size>
class MyClass {
public:
MyClass(const int (&data)[Size]);
};
template<int Size>
MyClass<Size> ::MyClass(const int (&data)[Size]) {}
它将仅接受正确大小的数组。
关于c++ - Visual C++ 关心函数参数中的数组大小。那是对的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37393820/