复制构造中接受 std::array 的初始器列表

Initilizer list in copy construct that accepts an std::array

本文关键字:array 列表 std 复制      更新时间:2023-10-16
#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];
}

此代码在调试和发布模式下在 Visual Studio 2013 中符合要求,但在我编译时,智能感知会给出此错误:

智能感知:构造函数"CHL::Vector::Vector [type=double, ArraySize=4U]"的实例与 参数列表 参数类型为:({...}(

我的问题是C++中的有效代码吗?如果是,我如何阻止 IntelliSense 用此错误污染我的错误列表。

智能感知需要三对大括号:

class Vector {
    Vector( //1 for initialization of vector
        std::array<...> //1 for initialization and 1 for internal array
    );
};

但是,该语言允许大括号省略,这意味着只需两个就可以了。我不确定为什么编译器会捕获这一点而 Intellisense 不会,但是如果您使用的是 CTP,它可能就像上次一样,编译器更改直到真正发布才反映在 Intellisense 中。