What does the C ++ compiler do with this list of initializers?

When using a list of initializers, for example:

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

The output is in the same order, 3, 1, 6, and finally 4. So, I know that the compiler should use something similar to std::vector<int>, rather than std::set<int>.

Is it guaranteed? Where can I find documents that explain how the compiler should interpret {3, 1, 6, 4}.

+4
source share
2 answers

braced-init-list , std:: initializer_list <int> . , 8.5.4.5. n4140 draft/standard ( ):

std:: initializer_list <E> , N const E, N - .

+6

std::initializer_list<int>. . http://en.cppreference.com/w/cpp/utility/initializer_list.

:

std::initializer_list<T> -, const T.

. , std::vector<int>. , int initializer_list.

+9

Source: https://habr.com/ru/post/1691142/


All Articles