4

In paragraph 8.6.4p4 of N4606, we have:

Within the initializer-list of a braced-init-list, the initializer-clauses, ..., 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.

In this program:

#include <algorithm>
#include <numeric>
#include <iostream>
#include <iterator>
using namespace std;

int main()
{
   int i = 0;
   int a[4] = { ++i, ++i, ++i,    // These are OK.
     accumulate(begin(a), end(a)-1, 0, plus<int>())  // Is this well-defined?
   };
   copy(begin(a), end(a), ostream_iterator<int>(cout, " "));
}

It is clear that accumulate is evaluated after the expressions ++i, and the side-effects of ++i are sequenced and well-defined. But is the initialization of an array element with the result of ++i also a side-effect of an initializer-clause? Does accumulate read out the values 1, 2, and 3, or does it potentially access uninitialized values?

cpplearner
  • 13,776
  • 2
  • 47
  • 72
j6t
  • 9,150
  • 1
  • 15
  • 35

0 Answers0