作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
从C++11开始,我们可以这样写:
vector<int> v{1, 2, 3, 4};
for (auto x : v)
{
cout << x << endl;
}
根据Essentials of Modern C++ Style ,下面的代码很快也会在 C++ 中合法化:
vector<int> v{1, 2, 3, 4};
for (x : v)
{
cout << x << endl;
}
此功能会在 C++17 或 C++20 中提供吗?
最佳答案
没有。这被委员会杀死了more than two years ago ,主要是因为担心阴影可能导致混淆:
std::vector<int> v = { 1, 2, 3, 4 };
int x = 0;
for(x : v) {} // this declares a new x, and doesn't use x from the line above
assert(x == 0); // holds
反对意见在这个过程中出现得太晚了,以至于 Clang 和 GCC 都已经实现了该功能,但该功能被整个委员会拒绝了。这些实现最终被取消:Clang GCC
关于c++ - C++17 会支持更简单的基于范围的 For 循环吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41299237/
我是一名优秀的程序员,十分优秀!