c++ - 为什么std::vector和std::valarray初始化构造函数不同?

标签 c++ vector language-lawyer c++-standard-library valarray

我刚刚被以下事情打伤了。
如果要使用常量std::vector初始化n元素的X,请执行以下操作:

std::vector<double> v(n, X);
但是,如果我需要使用常量std::valarray初始化n元素的X,则需要交换大小并初始化常量:
std::valarray<double> va(X, n);
在我看来,这就像一个任意的“陷阱”。
在对std::vectorstd::valarray进行标准化时,确定填充构造函数的参数顺序时,标准委员会是否提供技术原因或某些设计依据?

最佳答案

因为它们不是来自同一个地方:vector来自STL库,而valarray不是(我无法确定它的来源,但似乎与Fortran有很强的联系)。
引用比耶恩本人的话:

Most of the time, work on each of these components progressed in isolation from work on the others. There was no overall design or design philosophy.
[...]
Basically, the committee failed to contain “design by committee” so whereas the STL reflects a clear philosophy and coherent style, most of the other components suffered. Each represents its own style and philosophy, and some (such as string) manage simultaneously to present several.


(摘自“在现实世界中为现实世界发展一种语言:C++ 1991-2006”。)
因此,我想说的是,传统的C++语言是这样的:“事物就是它们的样子,为实现标准化而对其进行更改会破坏很多事情,所以让我们保持足够的孤独”。

关于c++ - 为什么std::vector和std::valarray初始化构造函数不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62793616/

相关文章:

c++ - 在具有固定常数离散化的给定网格上使用 C++ 进行数值积分

c - C 标准库中的头文件可以包含另一个头文件吗?

c++ - 类模板的成员模板的成员模板的显式模板函数特化是否有效?

c++ - C++ 编译器如何决定调用这些函数中的哪一个?

c++ - boost 记录器刷新到文件

c++ - 现代 OpenGL 2D 和 3D 绘图问题

c++ - map.insert : "Invalid arguments" error with pair<enum, vector <*>>

java - 如何在 Java 中过滤数组?

c++ - 保存数据指针

c++ - 无法理解 [basic.link]/6 C++14 示例中的声明 #3