Ok, engaging lawyer mode. ISO C99 standard says, in 6.5 (2):

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.
Failing the above rule, behavior is undefined. However, regarding initializer lists, read this, this question, and this this report. Various C and C++ standards differ in their wording, and guarantees they make. Coming back to C99, 6.7.8 (19) says:
The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject; ...
But then, 6.7.8 (23)
The order in which any side effects occur among the initialization list expressions is unspecified.

This example code is valid in C99 (no undefined behaviour), yet what it prints is unspecified.

#include <stdio.h> void meh(int t[]) { int i = 0; int foo[] = { [1] = ++i, [2] = ++i, [0] = ++i }; for (i = 0; i < 3; i++) { t[i] = foo[i]; } } int main(void) { int x[3]; meh(x); printf("%d %d %d\n", x[0], x[1], x[2]); }

For perl, flexibility in constructing and manipulating lists, e.g. using iterators (with their side effects) and so on, is essential quality of the language. There is no undefined behavior here. This thing quacks like a bug, it is a bug. How much speed-up do you think this buggy optimization is worth?


In reply to Re^5: Why does the first $c evaluate to the incremented value ... (bug) by oiskuu
in thread Why does the first $c evaluate to the incremented value in [$c, $c += $_] ? by smls

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.