std::array<T, N>的大小是否保证等于T[N]的大小?

Is the size of std::array<T, N> guaranteed to be equal to the size of T[N]?

这段代码似乎有效(所以它编译得很好),我在这里问的是:是否保证 sizeof(std::array) 与 sizeof(equivalent_Carray) 相同?

struct MyClass {
  std::array<float, 4> arr;  
  float carr[4];

  std::array<float, 4> cfunction() {
    std::array<float, sizeof(carr) / sizeof(float)> out;
    return out;
  }

  std::array<float, 4> function() {
    // Is this guaranteed to be the same as sizeof(carr) / sizeof(float)??
    std::array<float, sizeof(arr) / sizeof(float)> out;
    std::cout << sizeof(arr);
    return out;
  }
};

int main()
{
    MyClass obj;
    obj.function();
}

没有

我们通过 std::array<T, N> 获得的唯一保证是:

  • size()N;
  • &a[n] == &a[0] + n 所有 0 <= n < N;
  • 数组是一个集合 (8.5.1),可以使用语法

    进行初始化
    array<T, N> a = { initializer-list };
    

没有什么可以阻止实现者添加尾随成员,尽管我不知道为什么会这样做。