c++ - 关于 std::array fill 的设计决策

标签 c++ c++11

C++11 中的

std::array 是一个有用的类,它通过 C 堆栈数组提供 C++ 容器接口(interface)。

但为什么 std::array 没有大多数容器都有的典型填充构造函数?相反,它有一个方法 fill

std::array 在这方面在 STL 容器中是独一无二的吗?

最佳答案

摘自第 23.3.2.1 节:

An array is an aggregate (8.5.1) that can be initialized with the syntax array a = { initializer-list };

如果它像 std::vector 那样工作,它就不再是 POD。另外来自同一部分:

The conditions for an aggregate (8.5.1) shall be met.

这些条件是:

An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equalinitializers for non-static data members (9.2), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

关于c++ - 关于 std::array fill 的设计决策,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18155239/

相关文章:

c++ - 使用模板化 comp 函数实现 unique_copy - 类型推断

c++ - 在不同的窗口和/或对话框中显示 QLineEdit 的输入?

c++ - Visual Studio 强制桌面 API

c++ - 使用可变参数模板进行扩展

c++ - 从 `map` 中删除不在 `set` 中的元素

c++ - 多次传递输入参数的完美转发

c++ - 调用非默认构造函数作为成员初始化

c++ - =默认忽略访问说明符?

c++ - C++中 vector 初始化的性能

c# - 来自 C# : C++ function (in a DLL) returning false, 的 C++,但 C# 认为这是真的!