#include <array>
#include <algorithm>
template<typename Type, unsigned int ArraySize>
class Vector
{
public:
std::array<Type, ArraySize> arr;
Vector(){ std::fill(arr.begin(), arr.end(), 0); }
Vector(const std::array<Type, ArraySize>& input) : arr(input){}
};
#include <iostream>
int main()
{
Vector<double, 4> a2{{1, 2, 3, 4}};
std::cout << a2.arr[0];
std::cout << a2.arr[1];
std::cout << a2.arr[2];
std::cout << a2.arr[3];
}
此代码在调试和 Release模式下的 Visual Studio 2013 中符合良好要求,但在编译时 IntelliSense 给出此错误:
IntelliSense: no instance of constructor "CHL::Vector::Vector [with Type=double, ArraySize=4U]" matches the argument list argument types are: ({...})
我的问题是这是 C++ 中的有效代码吗?如果是的话,我怎样才能阻止 IntelliSense 用这个错误污染我的错误列表。
最佳答案
Intellisense 需要三对大括号:
class Vector {
Vector( //1 for initialization of vector
std::array<...> //1 for initialization and 1 for internal array
);
};
但是,该语言允许省略大括号,这意味着只需两个即可。我不确定为什么编译器会捕获到这一点,而 Intellisense 不会,但如果您使用 CTP,可能会像上次一样,直到真正发布时,编译器更改才反射(reflect)在 Intellisense 中。
关于c++ - 接受 std::array 的复制构造函数中的初始值设定项列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20171518/