std::initializer_list and order of evaluation of the elements [duplicate]

风格不统一 提交于 2019-12-11 02:08:13

问题


Is the comma (,) a sequence point in std::initializer_list?


example: is this UB or not:

#include <vector>

int main() 
{
    auto nums = []
    {
        static unsigned x = 2;
        return ( x++ % 2 ) + 1;
    };

    std::vector< int > v{ nums(), nums(), nums(), nums(), nums() };
    // not sure if this is different: (note the additional brackets)
    // std::vector< int > v({ nums(), nums(), nums(), nums(), nums() });
    for( auto i : v )
    {
        std::cout << i;
    }

    return 0;
}

回答1:


According to C++11 § 8.5.4 [dcl.init.list] paragraph 4:

4 Within the initializer-list of a braced-init-list, the initializer-clauses, including any that result from pack expansions (14.5.3), are evaluated in the order in which they appear. That is, every value computation and side effect associated with a given initializer-clause is sequenced before every value computation and side effect associated with any initializer-clause that follows it in the comma-separated list of the initializer-list.

As far as I know GCC 4.8.1 has a bug relative to evaluation of initializers. I described it here

http://cpp.forum24.ru/?1-3-0-00000063-000-0-0-1378892425

Though the text is written in Russion but it can be simply translated in English by using for example google translate.



来源:https://stackoverflow.com/questions/20266153/stdinitializer-list-and-order-of-evaluation-of-the-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!