Initialization of numeric (intrinsic type) data members

Discussion in 'C++' started by Lionel B, Jul 17, 2007.

  1. Lionel B

    Lionel B Guest

    This always confuses me. In the example below I would like x to be zero-
    initialised on default construction of a struct A object (there is no
    other initialisation/construction code required for default
    construction). Which form is most appropriate?

    struct A
    {
    double x;

    // first form
    // No user-supplied default constructor

    // second form
    A() {}

    // third form
    A() : x() {}

    // fourth form
    A() : x(0.0) {}
    };

    void foo()
    {
    A a;
    /* do stuff, assuming a.x == 0.0 */
    }

    Could some kind soul also point me to the relevant entry in the standard?

    Thanks,

    --
    Lionel B
     
    Lionel B, Jul 17, 2007
    #1
    1. Advertising

  2. On Jul 17, 3:03 pm, Lionel B <> wrote:
    > This always confuses me. In the example below I would like x to be zero-
    > initialised on default construction of a struct A object (there is no
    > other initialisation/construction code required for default
    > construction). Which form is most appropriate?
    >
    > struct A
    > {
    > double x;
    >
    > // first form
    > // No user-supplied default constructor
    >
    > // second form
    > A() {}
    >
    > // third form
    > A() : x() {}
    >
    > // fourth form
    > A() : x(0.0) {}
    >


    third and forth both will work.
    > };
    >
    > void foo()
    > {
    > A a;
    > /* do stuff, assuming a.x == 0.0 */
    >
    > }
    >
    > Could some kind soul also point me to the relevant entry in the standard?


    12.1 (7) The implicitly-defined default constructor performs the set
    of initializations of the class that would be performed by a user-
    written default constructor for that class with an empty mem-
    initializer-list and an empty function body

    (which means that your first form and second form are identical)

    12.6.2(4) If a given nonstatic data member or base class is not named
    by a meminitializerid
    (including the case where there is no meminitializerlist because the
    constructor has no ctorinitializer), then

    - If the entity is a non-static data member of (possibly cv-qualified)
    class type (or array thereof) or a base class, and the entity class is
    a non-POD class, the entity is default-initialized (8.5). If the
    entity is a non-static data member of a const-qualified type, the
    entity class shall have a user-declared default constructor.

    - Otherwise, the entity is not initialized. If the entity is of const-
    qualified type or reference type, or of a (possibly cv-qualified) POD
    class type (or array thereof) containing (directly or indirectly) a
    member of a const-qualified type, the program is ill-formed.

    According to "otherwise" clause above, x will be left uninitialized by
    the default constructor.

    -N
     
    Neelesh Bodas, Jul 17, 2007
    #2
    1. Advertising

  3. Lionel B

    Lionel B Guest

    On Tue, 17 Jul 2007 10:51:49 +0000, Neelesh Bodas wrote:

    > On Jul 17, 3:03 pm, Lionel B <> wrote:
    >> This always confuses me. In the example below I would like x to be
    >> zero- initialised on default construction of a struct A object (there
    >> is no other initialisation/construction code required for default
    >> construction). Which form is most appropriate?
    >>
    >> struct A
    >> {
    >> double x;
    >>
    >> // first form
    >> // No user-supplied default constructor
    >>
    >> // second form
    >> A() {}
    >>
    >> // third form
    >> A() : x() {}
    >>
    >> // fourth form
    >> A() : x(0.0) {}
    >>
    >>

    > third and forth both will work.


    Right. I actually use the third form in the "real" code (the numeric type
    is in fact a template argument).

    >> };
    >>
    >> void foo()
    >> {
    >> A a;
    >> /* do stuff, assuming a.x == 0.0 */
    >>
    >> }
    >>
    >> Could some kind soul also point me to the relevant entry in the
    >> standard?


    [...]

    Thanks very much,

    --
    Lionel B
     
    Lionel B, Jul 17, 2007
    #3
  4. Lionel B

    James Kanze Guest

    On Jul 17, 12:03 pm, Lionel B <> wrote:
    > This always confuses me. In the example below I would like x to be zero-
    > initialised on default construction of a struct A object (there is no
    > other initialisation/construction code required for default
    > construction). Which form is most appropriate?


    > struct A
    > {
    > double x;


    > // first form
    > // No user-supplied default constructor


    Does nothing...

    > // second form
    > A() {}


    Does nothing...

    > // third form
    > A() : x() {}


    Either "value initializes" or "default initializes" x, according
    to which version of the standard you refer to. In the case of a
    POD type like double, there's no difference, however; both mean
    that x will be "zero initialized" (which is the equivalent of
    "x(0)"; i.e. it initializes the value with the results of
    converting the integral constant 0 to the appropriate type).

    This features was added in the 1998 standard; it wasn't present
    in pre-standard C++, and some very old compilers might not
    implement it. (In pre-standard C++, it did nothing, like the
    two previous versions.)

    > // fourth form
    > A() : x(0.0) {}


    Initializes x with 0.0. If that's what you want, then this is
    the most appropriate form. (The "x()" form might be more
    appropriate, of course, for more complicated types, or in a
    template.)

    > };


    > void foo()
    > {
    > A a;
    > /* do stuff, assuming a.x == 0.0 */
    > }


    > Could some kind soul also point me to the relevant entry in
    > the standard?


    Which version of the standard:)? The different types of
    initialization are described in detail in §8.5, from about
    paragraph 5 on. The details do differ depending on the version
    of the standard, however. (I think the difference is really
    only relevant for class types with non-trivial compiler
    generated constructors, but I wouldn't swear to it.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 18, 2007
    #4
  5. James Kanze wrote:
    > On Jul 17, 12:03 pm, Lionel B <> wrote:
    >> Could some kind soul also point me to the relevant entry in
    >> the standard?

    >
    > Which version of the standard:)? The different types of
    > initialization are described in detail in §8.5, from about
    > paragraph 5 on. The details do differ depending on the version
    > of the standard, however.


    For some freaky reason, in *my* twisted understanding of the
    standardisation process, there is always one and only Standard.
    Why muddy the waters by the terms like "version of the standard"
    when any earlier "version" is *always* superceded by the later
    "version"? Yes, one can always find the Gloekenspiel C++ (or
    however it was called) and try using it, but it's not the matter
    of "version of the standard" but rather of compliance to _the_
    Standard (which BTW has already been in force for ~4 years now).

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 18, 2007
    #5
  6. Lionel B

    James Kanze Guest

    On Jul 18, 2:22 pm, "Victor Bazarov" <> wrote:
    > James Kanze wrote:
    > > On Jul 17, 12:03 pm, Lionel B <> wrote:
    > >> Could some kind soul also point me to the relevant entry in
    > >> the standard?


    > > paragraph 5 on. The details do differ depending on the version
    > > of the standard, however.


    > For some freaky reason, in *my* twisted understanding of the
    > standardisation process, there is always one and only Standard.


    In theory, you're right. But the one today isn't the same as
    the one yesterday, and compilers vary with regards to which one,
    if any, they support.

    > Why muddy the waters by the terms like "version of the standard"
    > when any earlier "version" is *always* superceded by the later
    > "version"?


    Because in practice, it isn't.

    > Yes, one can always find the Gloekenspiel C++ (or
    > however it was called) and try using it, but it's not the matter
    > of "version of the standard" but rather of compliance to _the_
    > Standard (which BTW has already been in force for ~4 years now).


    The official version is C++ 2003. Regretfully, I don't have
    that version handy. And g++ only accepts -std=c++98---it
    doesn't support the changes introduced in 2003. And I would
    imagine that most people would prefer code that won't suddenly
    be broken when the next version of the standard appears
    (promessed before 2009, but I really don't think that's
    possible), so what the current draft says isn't without interest
    either.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 19, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Theese

    Initialization of data members

    Dave Theese, Sep 16, 2003, in forum: C++
    Replies:
    3
    Views:
    298
    Gianni Mariani
    Sep 16, 2003
  2. Replies:
    6
    Views:
    476
    Ron Natalie
    Dec 11, 2005
  3. iluvatar
    Replies:
    8
    Views:
    368
    iluvatar
    Sep 13, 2006
  4. darrel
    Replies:
    4
    Views:
    851
    darrel
    Jul 19, 2007
  5. Shriramana Sharma
    Replies:
    6
    Views:
    279
    Shriramana Sharma
    Apr 5, 2013
Loading...

Share This Page