#include required to use initializer list in range-based for?

前端 未结 2 1352
眼角桃花
眼角桃花 2021-01-17 11:40

The final C++11 standard includes provisions for range-based for to \"just work\" for native arrays without having to include or any other head

相关标签:
2条回答
  • 2021-01-17 12:27

    GCC 7.1 produces the following error if <initializer_list> is not included:

    error: deducing from brace-enclosed initializer list requires #include <initializer_list>
         for (auto i : { 1, 2, 3, 4, 5 })
                                       ^
    

    To see this error one should omit <iostream>, because including <iostream> will also include <initializer_list>.

    0 讨论(0)
  • 2021-01-17 12:31

    I'd say yes. According to §6.5.4[stmt.ranged]/1, the statement

    for (auto i : { 1, 2, 3, 4, 5 })
        ...
    

    is just equivalent to

    auto&& __range = { 1, 2, 3, 4, 5 };
    ...
    

    and that means an initializer_list<int> is used, and the <initializer_list> header needs to be included.

    0 讨论(0)
提交回复
热议问题