C++ 编译器如何处理这个初始化列表?

What does a C++ compiler do with this initializer list?

当使用这样的初始化列表时:

for (int i : {3, 1, 6, 4})
{
    std::cout << "i=" << i << std::endl;
}

输出顺序相同,3、1、6,最后是 4。所以我知道编译器必须使用类似于 std::vector<int> 而不是 std::set<int> 的东西。

这有保障吗?我在哪里可以找到解释编译器必须如何解释 {3, 1, 6, 4}.

的文档

您正在创建 std::initializer_list<int> 的实例。有关详细信息,请参阅 http://en.cppreference.com/w/cpp/utility/initializer_list

来自该页面:

An object of type std::initializer_list<T> is a lightweight proxy object that provides access to an array of objects of type const T.

保证输出顺序。然而,这并不是因为编译器创建了 std::vector<int>。这是有保证的,因为在 initializer_list.

下面有一个 int 的数组

你的range based loop is using the braced-init-list as a range expression which constructs a temporary object of type std::initializer_list<int>. The order is guaranteed because the 8.5.4.5. paragraph of the n4140 draft / standard状态(强调我的):

An object of type std::initializer_list<E> is constructed from an initializer list as if the implementation allocated a temporary array of N elements of type const E, where N is the number of elements in the initializer list.