"Default construction" of built-in types?

Discussion in 'C++' started by DaKoadMunky, May 29, 2004.

  1. DaKoadMunky

    DaKoadMunky Guest

    I recently came across some code in a template that default constructed an
    object of type T to pass to another function...

    SomeFunction(T());

    The code that instantiates that template specifies T as an int.

    Proper program behavior relies on that "default constructed" int being zero.

    That lead me to the following example...

    int main()
    {
    int i; //Uninitialized! Expected for automatic of built-in type

    int j = int(); //Initialized to zero! Not sure what to expect

    return 0;
    }

    Is such "default construction" of built-in types standard C++?

    char() == 0
    int()==0

    etc...

    Thanks
     
    DaKoadMunky, May 29, 2004
    #1
    1. Advertising

  2. "DaKoadMunky" <> wrote...
    > I recently came across some code in a template that default constructed an
    > object of type T to pass to another function...
    >
    > SomeFunction(T());
    >
    > The code that instantiates that template specifies T as an int.
    >
    > Proper program behavior relies on that "default constructed" int being

    zero.

    "Default-initialised" is the proper term. And, yes, it is supposed to be
    initialised to zero.

    >
    > That lead me to the following example...
    >
    > int main()
    > {
    > int i; //Uninitialized! Expected for automatic of built-in type
    >
    > int j = int(); //Initialized to zero! Not sure what to expect


    What do you mean by the second part of the comment?

    >
    > return 0;
    > }
    >
    > Is such "default construction" of built-in types standard C++?


    Yes.

    Victor
     
    Victor Bazarov, May 29, 2004
    #2
    1. Advertising

  3. DaKoadMunky

    DaKoadMunky Guest

    >> int j = int(); //Initialized to zero! Not sure what to expect
    >
    >What do you mean by the second part of the comment?


    Comment could have been extended to say "on other compilers, platforms, etc.."
    given that I didn't know if the initialization to zero was standard.
     
    DaKoadMunky, May 29, 2004
    #3
  4. DaKoadMunky

    Alan Johnson Guest

    Victor Bazarov wrote:
    > "DaKoadMunky" <> wrote...
    >
    >>I recently came across some code in a template that default constructed an
    >>object of type T to pass to another function...
    >>
    >>SomeFunction(T());
    >>
    >>The code that instantiates that template specifies T as an int.
    >>
    >>Proper program behavior relies on that "default constructed" int being

    >
    > zero.
    >
    > "Default-initialised" is the proper term. And, yes, it is supposed to be
    > initialised to zero.
    >
    >
    >>That lead me to the following example...
    >>
    >>int main()
    >>{
    >> int i; //Uninitialized! Expected for automatic of built-in type
    >>
    >> int j = int(); //Initialized to zero! Not sure what to expect

    >
    >
    > What do you mean by the second part of the comment?
    >
    >
    >> return 0;
    >>}
    >>
    >>Is such "default construction" of built-in types standard C++?

    >
    >
    > Yes.
    >
    > Victor
    >
    >


    I didn't believe you, so I looked it up, and sure enough, that is correct.

    To the OP:
    The standard says that any object whose initializer is () is
    "value-initialized", and for POD types, "value-initialized" is defined
    as being "zero-initialized". Refer to Section 8.5 for more info.

    Alan
     
    Alan Johnson, May 29, 2004
    #4
  5. "Default construction" of NON-built-in types?

    "Alan Johnson" <> wrote:

    > The standard says that any object whose initializer is () is
    > "value-initialized", and for POD types, "value-initialized" is defined
    > as being "zero-initialized". Refer to Section 8.5 for more info.


    Ah-ha! I ran into trouble at work with a struct which
    I had instantiated inside a function like so:

    MyStruct Blat;

    The struct had an int member which I expected to be
    initialized to 0, but it actually had an initial value
    of 512 !

    So you're saying if I had instantiated it like this, instead:

    MyStruct Blat ();

    The numerical members would be initialized to 0 ?

    --
    Cheers,
    Robbie Hatley
    Tustin, CA, USA
    email: lonewolfintj at pacbell dot net
    web: home dot pacbell dot net slant earnur slant






    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Robbie Hatley, Jun 28, 2004
    #5
  6. DaKoadMunky

    Phlip Guest

    Re: "Default construction" of NON-built-in types?

    > So you're saying if I had instantiated it like this, instead:
    >
    > MyStruct Blat ();
    >
    > The numerical members would be initialized to 0 ?
    >
    > --
    > Cheers,
    > Robbie Hatley


    That sez "Blat is a function that returns MyStruct".

    You need thus:

    MyStruct Blat = MyStruct ();

    or

    MyStruct Blat (MyStruct ());

    This otherwise silly system exists so STL containers can default-construct
    primitives without leaking garbage into them.

    int x;

    The act of using 'x' is now undefined for most operations except assignment.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jun 28, 2004
    #6
  7. Re: "Default construction" of NON-built-in types?

    * "Robbie Hatley" <lonewolfintj at pacbell dot net>:
    >
    > "Alan Johnson" <> wrote:
    >
    > > The standard says that any object whose initializer is () is
    > > "value-initialized", and for POD types, "value-initialized" is defined
    > > as being "zero-initialized". Refer to Section 8.5 for more info.

    >
    > Ah-ha! I ran into trouble at work with a struct which
    > I had instantiated inside a function like so:
    >
    > MyStruct Blat;
    >
    > The struct had an int member which I expected to be
    > initialized to 0, but it actually had an initial value
    > of 512 !
    >
    > So you're saying if I had instantiated it like this, instead:
    >
    > MyStruct Blat ();
    >
    > The numerical members would be initialized to 0 ?


    With a compiler that conforms in this respect, yes.

    It is not a feature you can rely on, though.

    Please stop changing the subject line, as it messes up the threading in
    most newsreaders.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jun 28, 2004
    #7
  8. Re: "Default construction" of NON-built-in types?

    * Phlip:
    > > So you're saying if I had instantiated it like this, instead:
    > >
    > > MyStruct Blat ();
    > >
    > > The numerical members would be initialized to 0 ?

    >
    > That sez "Blat is a function that returns MyStruct".


    Yes -- in my earlier replier I didn't recognize that... :-(

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jun 28, 2004
    #8
  9. Re: "Default construction" of NON-built-in types?

    "Phlip" <> wrote:

    > > So you're saying if I had instantiated it like this, instead:
    > >
    > > MyStruct Blat ();
    > >
    > > The numerical members would be initialized to 0 ?
    > >
    > > --
    > > Cheers,
    > > Robbie Hatley

    >
    > That sez "Blat is a function that returns MyStruct".


    OOPS! Right, that's a prototype that says "I'll be
    defining a function Blat later that takes no arguments and
    returns a MyStruct". I really missed that one.

    > You need thus:
    >
    > MyStruct Blat = MyStruct ();
    >
    > or
    >
    > MyStruct Blat (MyStruct ());


    I don't like those.

    The first one calls two constructors instead of just one:

    MyStruct Blat = MyStruct (); // Default + Assignment

    As for the second one:

    MyStruct Blat (MyStruct ());

    That compiles, but yields a function, not an object.
    I think you you just declared a function which returns
    a MyStruct, and takes as an argument a nameless pointer
    to a nameless function which takes no arguments and
    returns a MyStruct. Not a very useful concept. :)

    I think I like the approach I ended up using, after all:

    MyStruct
    {
    MyStruct () : asdf(0), qwer(0) {}
    int asdf;
    int qwer;
    };

    int main(void)
    {
    MyStruct Blat; // Zeros members; calls only one constructor
    }


    --
    Cheers,
    Robbie Hatley
    Tustin, CA, USA
    email: lonewolfintj at pacbell dot net
    web: home dot pacbell dot net slant earnur slant






    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Robbie Hatley, Jun 28, 2004
    #9
  10. DaKoadMunky

    tom_usenet Guest

    Re: "Default construction" of NON-built-in types?

    On Sun, 27 Jun 2004 23:48:33 -0700, "Robbie Hatley" <lonewolfintj at
    pacbell dot net> wrote:

    >> You need thus:
    >>
    >> MyStruct Blat = MyStruct ();
    >>
    >> or
    >>
    >> MyStruct Blat (MyStruct ());

    >
    >I don't like those.
    >
    >The first one calls two constructors instead of just one:
    >
    > MyStruct Blat = MyStruct (); // Default + Assignment


    You meant default and copy. But all compilers I know of elide the
    temporary (as is allowed by the standard) and therefore don't make the
    copy.

    >
    >As for the second one:
    >
    > MyStruct Blat (MyStruct ());
    >
    >That compiles, but yields a function, not an object.


    You need:

    MyStruct Blat ((MyStruct()));

    which is getting silly...

    >I think I like the approach I ended up using, after all:
    >
    >MyStruct
    >{
    > MyStruct () : asdf(0), qwer(0) {}
    > int asdf;
    > int qwer;
    >};
    >
    >int main(void)
    >{
    > MyStruct Blat; // Zeros members; calls only one constructor
    >}


    Yup, that's the best plan, but in generic code where you want default
    initialization but don't know the type, you should probably do:

    T t = T();

    Tom
    --
    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
     
    tom_usenet, Jun 28, 2004
    #10
  11. DaKoadMunky

    Phlip Guest

    Re: "Default construction" of NON-built-in types?

    tom_usenet wrote:

    > >As for the second one:
    > >
    > > MyStruct Blat (MyStruct ());
    > >
    > >That compiles, but yields a function, not an object.

    >
    > You need:
    >
    > MyStruct Blat ((MyStruct()));
    >
    > which is getting silly...


    I suspect you both have that one wrong.

    MyStruct Blat ((MyStruct())); can't be different from MyStruct Blat
    (MyStruct()); or MyStruct Blat ((((((((MyStruct()))))))));. Many types can
    take extra parens.

    MyStruct() is an rvalue, so the compiler sees Blat's input argument, not a
    type for a function.

    BTW use T t = T(), just as a style guideline.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jun 28, 2004
    #11
  12. Re: "Default construction" of NON-built-in types?

    Phlip wrote in news:uoWDc.1647$Z% in
    comp.lang.c++:

    > tom_usenet wrote:
    >
    >> >As for the second one:
    >> >
    >> > MyStruct Blat (MyStruct ());
    >> >
    >> >That compiles, but yields a function, not an object.

    >>
    >> You need:
    >>
    >> MyStruct Blat ((MyStruct()));
    >>
    >> which is getting silly...

    >
    > I suspect you both have that one wrong.
    >
    > MyStruct Blat ((MyStruct())); can't be different from MyStruct Blat
    > (MyStruct()); or MyStruct Blat ((((((((MyStruct()))))))));. Many types
    > can take extra parens.
    >


    (int a); // not a legal declaration.
    int (a); // legal.


    > MyStruct() is an rvalue, so the compiler sees Blat's input argument,
    > not a type for a function.
    >
    > BTW use T t = T(), just as a style guideline.


    Yup.

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Jun 28, 2004
    #12
  13. DaKoadMunky

    Xenos Guest

    Re: "Default construction" of NON-built-in types?

    "tom_usenet" <> wrote in message
    news:...
    > On Sun, 27 Jun 2004 23:48:33 -0700, "Robbie Hatley" <lonewolfintj at
    > pacbell dot net> wrote:
    >
    > >> You need thus:
    > >>
    > >> MyStruct Blat = MyStruct ();
    > >>
    > >> or
    > >>
    > >> MyStruct Blat (MyStruct ());

    > >
    > >I don't like those.
    > >
    > >The first one calls two constructors instead of just one:
    > >
    > > MyStruct Blat = MyStruct (); // Default + Assignment

    >
    > You meant default and copy. But all compilers I know of elide the
    > temporary (as is allowed by the standard) and therefore don't make the
    > copy.


    It was always my understanding that, even though this looks like an
    assignment, it is not. It is still just a constructor call in another form.
    Any talk of the compiler optimizing away the assignment is moot.

    DrX
     
    Xenos, Jun 28, 2004
    #13
  14. Re: "Default construction" of NON-built-in types?

    In message <cbpkg7$>, Xenos
    <> writes
    >
    >"tom_usenet" <> wrote in message
    >news:...
    >> On Sun, 27 Jun 2004 23:48:33 -0700, "Robbie Hatley" <lonewolfintj at
    >> pacbell dot net> wrote:
    >>
    >> >> You need thus:
    >> >>
    >> >> MyStruct Blat = MyStruct ();
    >> >>
    >> >> or
    >> >>
    >> >> MyStruct Blat (MyStruct ());
    >> >
    >> >I don't like those.
    >> >
    >> >The first one calls two constructors instead of just one:
    >> >
    >> > MyStruct Blat = MyStruct (); // Default + Assignment

    >>
    >> You meant default and copy. But all compilers I know of elide the
    >> temporary (as is allowed by the standard) and therefore don't make the
    >> copy.

    >
    >It was always my understanding that, even though this looks like an
    >assignment, it is not. It is still just a constructor call in another form.
    >Any talk of the compiler optimizing away the assignment is moot.
    >

    Maybe so, but talk of the compiler optimizing away the
    _copy-construction_ (12.8.15) is perfectly valid ;-).

    --
    Richard Herring
     
    Richard Herring, Jun 29, 2004
    #14
    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. muser

    built in types

    muser, Nov 26, 2003, in forum: C++
    Replies:
    4
    Views:
    382
    muser
    Nov 27, 2003
  2. Howard
    Replies:
    3
    Views:
    447
    Siemel Naran
    Jul 22, 2004
  3. Ook
    Replies:
    10
    Views:
    571
  4. Alona
    Replies:
    2
    Views:
    662
  5. Bjarke Hammersholt Roune

    Default construction of arrays of PODs using new

    Bjarke Hammersholt Roune, Apr 14, 2010, in forum: C++
    Replies:
    6
    Views:
    456
    James Kanze
    Apr 15, 2010
Loading...

Share This Page