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/