Question

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;
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top