Topic: Can class T have a member 'static T aT
Author: stephen.clamage@eng.sun.com (Steve Clamage)
Date: 1996/12/10 Raw View
In article ya023580000912961026020001@ecpnews.ecp.fr,
aubryl7@caracal.cti.ecp.fr (Ludovic Aubry) writes:
>In article <32AB5278.41C67EA6@informatik.uni-hamburg.de>, Stephen Friedrich
><1friedri@informatik.uni-hamburg.de> wrote:
>
>>Is the following legal C++ code?
>>
>>class T {
>> public:
>> T();
>> virtual ~T();
>>
>> static T aT;
>>
>> static S aS;
>>};
>>
>>GNU g++ compiles this but doesn't construct other static members
>>of class type (like aS) before calling T::T().
>>Is it a bug
>
> I thought constructors where called in order of their declaration, so
>calling aT::T before S::aS should be normal.
No, objects defined in the same translation unit are initialized in the
order of their definition, which is not necessarily related to the order
of their declaration. Example:
extern T i; // declaration of i before declaration of j
extern T j;
T j(1); // definition of j before definition of i
T i = j; // OK, j defined first
In addition, the T::aT and T::aS objects could be defined in different
translation units, in which case the order of their initialization is
not specified.
---
Steve Clamage, stephen.clamage@eng.sun.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 ]
[ 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: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/12/10 Raw View
stephen.clamage@eng.sun.com (Steve Clamage) writes:
>objects defined in the same translation unit are initialized in the
>order of their definition
and jlilley@empathy.com (John Lilley) writes:
>The order of construction is determined by the placement of the
>*definitions* of the static members, not the declarations within the
>containing class. Within a single translation unit (source file), the
>static members are constructed in the order of definition (DWP s3.6.2.1).
Perhaps it is an oversight, rather than a deliberate omission, but
section 3.6.2/1 in the Nov 96 working paper refers to "objects
of namespace scope with static storage duration"; it does not
mention objects of _class scope_ with static storage duration
(i.e. static members).
As far as I can tell, the current wording of the draft leaves
the order of initialization of static members unspecified.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
[ 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 ]