Topic: Initialization in a template


Author: KJ Klint <"klint<no-spam\]"@telia.com>
Date: 1999/04/19
Raw View
There is no need to revert to T*. Just use: T x = T();, will worl on POD
types as well.
/jk

Marco Dalla Gasperina wrote:

> In comp.lang.c++.moderated I <marcodg@pacifier.com> wrote:
>
> >    template<class T> void foo()
> >    {
> >       T x;
> >       cout << x << endl;
> >    }
>
> >    foo<int>();
>
> The problem is that if T is a builtin, 'x' is not initialized
> which makes it a little inconvenient and a source for potential
> bugs.
>
> However, after reading sections 5.3.4 and 8.5, it turns out that
> you can do this:
>
> T* x = new T();
>
> Which will (supposedly) zero-initialize *x if T is a simple or
> POD type.
>
> This is not to be confused with
>
> T* x = new T;
>
> which does not zero-initialize basic or POD types.  And to think
> that I believed that those two new expressions were equivalent...
> Maybe that's a possible guru question?

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: John Panzer <jpanzer@atweb.com>
Date: 1999/04/19
Raw View
Marco Dalla Gasperina wrote:

> In comp.lang.c++.moderated I <marcodg@pacifier.com> wrote:
> >    template<class T> void foo()
> >    {
> >       T x;
> >       cout << x << endl;
> >    }
> >    foo<int>();
>
> The problem is that if T is a builtin, 'x' is not initialized
> which makes it a little inconvenient and a source for potential
> bugs.
>
> However, after reading sections 5.3.4 and 8.5, it turns out that
> you can do this:
>
> T* x = new T();
>
> Which will (supposedly) zero-initialize *x if T is a simple or
> POD type.

What about auto variables like
T x = T();
?

If T is a builtin, presumably this would generate the same code as "T x =
0;".  If T is a class with a constructor and copy constructor, could we in
general expect the compiler to optimize away the copy constructor?  (I just
checked, gcc does appear to do this.)

John Panzer
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Stein Somers <stein.somers@comsof.com@telenet-ops.be>
Date: 1999/04/20
Raw View
David R Tribble wrote:
> The only way to guarantee that all the parts of an object are
> initialized is to write a (default) constructor for its class
> which explicitly initializes every member of the object.

There is another way: avoid builtin types for member variables and
template class actual parameters.  Wrap (template) classes around int
and its allies.  In legacy code, uou don't need to change function
parameters, and you don't want to for virtual functions since it would
break virtual overrides elsewhere.

It's a pitty there are no standard C++ wrappers for the C builtin's.
Some people (like me) think that this is a flaw of the standard library.

Stein

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Marco Dalla Gasperina <marcodg@pacifier.com>
Date: 1999/04/18
Raw View
In comp.lang.c++.moderated I <marcodg@pacifier.com> wrote:

>    template<class T> void foo()
>    {
>       T x;
>       cout << x << endl;
>    }

>    foo<int>();

The problem is that if T is a builtin, 'x' is not initialized
which makes it a little inconvenient and a source for potential
bugs.

However, after reading sections 5.3.4 and 8.5, it turns out that
you can do this:

T* x = new T();

Which will (supposedly) zero-initialize *x if T is a simple or
POD type.

This is not to be confused with

T* x = new T;

which does not zero-initialize basic or POD types.  And to think
that I believed that those two new expressions were equivalent...
Maybe that's a possible guru question?

thanks,
marco


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: David R Tribble <dtribble@technologist.com>
Date: 1999/03/26
Raw View
Marco Dalla Gasperina wrote:
> Please consider the following:
>
>    template<class T> void foo()
>    {
>       T x;
>       cout << x << endl;
>    }
>
>    foo<int>();
>
> I had once remembered reading (probably incorrectly) that
> 'x' would be initialized to 0 because of a special case
> of basic types being instantiated in templates.  It turns
> out that on the compilers I use that is not the case and
> the variable 'x' turns out to be garbage.

There is no way to implicitly default-initialize a non-static
variable or member having built-in type.  Period.  Some people
(like me) think that this is a flaw of the language.

> Of course, if T is a class-type then the default constructor is
> called.

This is the only way to guarantee that an object will be
initialized.  Unfortunately, objects containing members of built-in
types won't get initialized by the object's default initializer
either.  Some people (like me) think that this is a flaw of the
language.

The only way to guarantee that all the parts of an object are
initialized is to write a (default) constructor for its class
which explicitly initializes every member of the object.  But
remember that if you forget to initialize a member having
built-in type, it won't get initialized.  Some people (like me)
think that this is a flaw of the language.

The only safe approach is to always provide a default constructor
for your classes which explicitly initializes every member of the
class.  Just be sure to update the code when you add new members
to the class.

-- David R. Tribble, dtribble@technologist.com --
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Marco Dalla Gasperina <marcodg@pacifier.com>
Date: 1999/03/18
Raw View
Please consider the following:

   template<class T> void foo()
   {
      T x;
      cout << x << endl;
   }

   foo<int>();

I had once remembered reading (probably incorrectly) that
'x' would be initialized to 0 because of a special case
of basic types being instantiated in templates.  It turns
out that on the compilers I use that is not the case and
the variable 'x' turns out to be garbage.  Of course, if
T is a class-type then the default constructor is called.

One way to fix this is to use

     T x(T());

which correctly initializes 'x' whether or not T is a
basic type... assuming T has an accessible copy-ctor
which can bind to a const reference.

This also makes the code fragments:
     cout << T();
and
     T x; cout << x;
not equivalent which is counter intuative at the least.

My understanding is cloudy on this issue, but better
cloudy than wrong.

Can anyone spare some enlightenment?

thanks,
marco


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]