我正在学习 C++,我看到了一个基于范围的 for
循环,如下所示:
for (int i : {1,2,3,4,5})
std::cout << i << ' ';
幕后for循环中的临时{1,2,3,4,5}
是什么?
最佳答案
这里的对象是 std::initializer_list<int>
的一个实例.来自引用(强调我的):
A std::initializer_list object is automatically constructed when:
a braced-init-list is used to list-initialize an object, where the corresponding constructor accepts an std::initializer_list parameter
a braced-init-list is used as the right operand of assignment or as a function call argument, and the corresponding assignment operator/function accepts an std::initializer_list parameter
a braced-init-list is bound to auto, including in a ranged for loop
关于c++ - 以下基于范围的 for 循环内幕后的列表是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54070743/