Prev: Is it valid to assign a reference from a ternary expression (i.e. operator ?:)
Next: Is it valid to assign a reference from a ternary expression (i.e. operator ?:)
From: Sinuhet on 4 Jun 2010 06:33 Hello, lets start with the code. // code begin static int data[] = {1, 2, 3, std::accumulate(data, data+4, 0)}; // code end The question is, is this definition without undefined/unspecified behaviour? Paragraph 3.6.2 states that for static objects there are two phases of initialization - static and dynamic, and that static phase (which consists of zeroing and initialization by constant expression) takes place before dynamic (where is done everything else). However, in paragraph 8.5.1 section 14 the standard says "otherwise, it is unspecified whether the initialization of members with constant expressions takes place during the static phase or during the dynamic phase of initialization." So no help here. Then there are sequence points, but this is where I'm not sure. I would say that each initializer in a list is full-expression, therefore there is sequence point after each initializer and the first three elements of the array should be initialized before std::accumulate is called. Is this reasoning right? Thanks in advance. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 5 Jun 2010 05:50 On 4 Jun., 23:33, Sinuhet <sinu...(a)yes.spam.mail.cz> wrote: > Hello, > lets start with the code. > > // code begin > > static int data[] = {1, 2, 3, std::accumulate(data, data+4, 0)}; > > // code end > > The question is, is this definition without undefined/unspecified behaviour? > > Paragraph 3.6.2 states that for static objects there are two phases of > initialization - static and dynamic, and that static phase (which > consists of zeroing and initialization by constant expression) takes > place before dynamic (where is done everything else). However, in > paragraph 8.5.1 section 14 the standard says "otherwise, it is > unspecified whether the initialization of members with constant > expressions takes place during the static phase or during the dynamic > phase of initialization." So no help here. > > Then there are sequence points, but this is where I'm not sure. I would > say that each initializer in a list is full-expression, therefore there > is sequence point after each initializer and the first three elements of > the array should be initialized before std::accumulate is called. > > Is this reasoning right? I believe that your example is well-formed and well-defined in regard to the initialization of data. And since 3.6.2 also guarantees that any implementation that decides for moving dynamic initialization into static initialization it must ensure that the value results, it should also be portable. HTH & Greetings from Bremen, Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Öö Tiib on 6 Jun 2010 00:02
On 5 juuni, 00:33, Sinuhet <sinu...(a)yes.spam.mail.cz> wrote: > // code begin > > static int data[] = {1, 2, 3, std::accumulate(data, data+4, 0)}; > > // code end > > The question is, is this definition without undefined/unspecified behaviour? Typo? Did you mean: static int data[] = {1, 2, 3, std::accumulate(data, data+3, 0)}; The original code feels like either a black magic or a bug. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |