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! ]
|
|
0
|
|
|
|
Reply
|
Sinuhet
|
6/4/2010 3:33:40 PM |
|
On 4 Jun., 23:33, Sinuhet <sinu...@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! ]
|
|
0
|
|
|
|
Reply
|
ISO
|
6/5/2010 2:50:28 PM
|
|
On 5 juuni, 00:33, Sinuhet <sinu...@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! ]
|
|
0
|
|
|
|
Reply
|
ISO
|
6/6/2010 9:02:54 AM
|
|
|
2 Replies
300 Views
(page loaded in 0.022 seconds)
Similiar Articles: How to start with gdcm - comp.protocols.dicomAggregate initializers of static objects - comp.lang.c++.moderated ... How to start with gdcm - comp.protocols.dicom Aggregate initializers of static objects - comp.lang ... initializer must be constant error bug? - comp.compilers.lcc ...Aggregate initializers of static objects - comp.lang.c++.moderated ... initializer must be constant error bug? - comp.compilers.lcc ... Aggregate initializers of static ... Static const integral data members can be initialized? - comp.lang ...Static const integral data members can be initialized? - comp.lang ... Aggregate initializers of static objects - comp.lang.c++.moderated ... Static const integral data ... nitializing a static vector <> of integers (this static vectorAggregate initializers of static objects - comp.lang.c++.moderated ... // code begin static int ... nitializing a static vector <> of integers (this static ... within a ... undefined references in MikTex 2.9 --- bug? - comp.text.tex ...Aggregate initializers of static objects - comp.lang.c++.moderated ... undefined references in MikTex 2.9 --- bug? - comp.text.tex ... Aggregate initializers of static ... Ordering on Hierarchical Dot Notation - comp.lang.java.programmer ...Aggregate initializers of static objects - comp.lang.c++.moderated ... [10.13] How do I prevent the "static initialization order fiasco"? http://www ... Initialization of reference to non-const - comp.lang.c++.moderated ...It seems to me that allowing initialization of static const float ... this revised version looks perfectly ... Question regarding initialization of non-local POD-Object 19 ... bit fields and default initialization - comp.lang.c++.moderated ...If I write something like this: my_struct object ... Can someone point me the rules for bit fields initialization? ... Notice, however, that any POD with static storage ... tuple and brace initializer - why not - comp.lang.c++.moderated ...I'm getting a better hand at the notion of initializer list, and brace ... Tuple Class (System) Provides static methods for creating tuple objects. ... A tuple is a data ... VHDL-2002 vs VHDL-93 vs VHDL-87? - comp.lang.vhdl... prefix is either a globally static subtype or is an object or ... statement is in the form of an aggregate, and if the locally static ... vs VHDL-87? - comp.lang.vhdl initializer ... Initializers (C++) - Microsoft Corporation: Software, Smartphones ...Initializers invoked using the equal-sign syntax, including aggregate initializers: ... (For more information about initialization of global static objects, see ... Initializers - Microsoft Corporation: Software, Smartphones ...Initializers invoked using the equal-sign syntax, including aggregate initializers: ... (For more information about initialization of global static objects, see ... 7/23/2012 7:54:06 PM
|