2

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; } 
12
  • 2
    There are no "sequence points" any more in C++11. However, the order of evaluation is guaranteed and it is indeed ordered, but I'll search for a duplicate first before answering. Commented Nov 28, 2013 at 12:28
  • @DyP - thanks. I didn't find duplicate, although I'm pretty sure I've seen similar question.. I'm almost sure, the order is guaranteed, but not 100%. Commented Nov 28, 2013 at 12:29
  • 1
    [dcl.init.list]/4 "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." Commented Nov 28, 2013 at 12:30
  • 1
    @Kiril: Yeah, it's a notion of "sequenced-before" and "sequenced-after", now. Commented Nov 28, 2013 at 12:32
  • 1
    Also search for the [c++11] tag ;) To answer the question in the code comment: No, it's not different. It's list-initialization in both cases, so the same rules apply wrt sequencing. The second one could have a different meaning (in a general case), but is has the same meaning here: Call the initializer_list constructor of vector with an initializer_list constructed via list-initialization. Commented Nov 28, 2013 at 12:39

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.