gpt4 book ai didi

c++ - C++17 会支持更简单的基于范围的 For 循环吗?

转载 作者:IT老高 更新时间:2023-10-28 12:49:55 27 4
gpt4 key购买 nike

从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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com