Topic: implicit instantiation of static data members
Author: bill@gibbons.org (Bill Gibbons)
Date: 1997/03/18 Raw View
In article <5fpent$res$1@erawan.cognex.com>, Michael R Cook
<mcook@cognex.com> wrote:
> If I have a static data member in a template, and I don't actually reference
> that static data member anywhere, but I implicitly instantiate other members
> of the template, will the static data member get implicitly instantiated, too?
>
> My understanding of the standard says No, but the static data member has a
> constructor that does some important things, and basically it's a bug (in my
> case) that the data member is not getting instantiated. I'm wondering if it's
> a bug in the compiler, or a bug in my design.
Static data members are instantiated only when used or when explicitly
instantiated - just like member functions.
X3J16/WG21 has recognized that this point is not entirely clear in the
working paper and will probably clarify it.
(This was even less clear before because the working paper required that
all static data members be defined, even if they were not used. (The
application of this rule to templates was not described). That
restriction has been removed. Now static data members need be defined
only if they are used, just like nonvirtual member functions. )
Note that this is important because initializing the member might have
side effects. Since class templates are often instantiated even when
there are no uses of the instantiated type at all (this happens a lot in
overload resolution), it would have been common to have a type cause
run-time side-effects in a program in which the type was not even used.
That would have been unfortunate.
-- Bill Gibbons
--
Bill Gibbons
bill@gibbons.org
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Michael R Cook <mcook@cognex.com>
Date: 1997/03/07 Raw View
If I have a static data member in a template, and I don't actually reference
that static data member anywhere, but I implicitly instantiate other members
of the template, will the static data member get implicitly instantiated, too?
My understanding of the standard says No, but the static data member has a
constructor that does some important things, and basically it's a bug (in my
case) that the data member is not getting instantiated. I'm wondering if it's
a bug in the compiler, or a bug in my design.
--
Michael Cook <mcook@cognex.com> <http://ftp.cognex.com/~mcook/>
Telephone: +1 508 650 3251 Fax: +1 508 650 3336
Cognex Corporation, One Vision Drive, Natick, Massachusetts 01760-2059
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]