Topic: Can class T have a member 'static T aT' ?


Author: Stephen Friedrich <1friedri@informatik.uni-hamburg.de>
Date: 1996/12/09
Raw View
Is the following legal C++ code?

class T {
   public:
      T();
      virtual ~T();

      static T aT;

      static S aS;
};

A class that 'contains itself' seems a bit strange but since 'aT'
is declared static there is no recursion, so this would be possible ?!

GNU g++ compiles this but doesn't construct other static members
of class type (like aS) before calling T::T().
Is it a bug
- that g++ compiles this at all or
- that aS doesn't get initialized before calling T::T()


======================================================
Stephen Friedrich,  1friedri@informatik.uni-hamburg.de
---
[ 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: aubryl7@caracal.cti.ecp.fr (Ludovic Aubry)
Date: 1996/12/09
Raw View
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.
   have you tried declaring aS before aT ?

--
Ludovic Aubry
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/12/09
Raw View
Stephen Friedrich <1friedri@informatik.uni-hamburg.de> writes:

>Is the following legal C++ code?

Yes, I believe so (except that you omitted the definition of `S').

>class T {
>   public:
>      T();
>      virtual ~T();
>
>      static T aT;
>
>      static S aS;
>};
>
>A class that 'contains itself' seems a bit strange but since 'aT'
>is declared static there is no recursion, so this would be possible ?!

Yes.  Note that the above code only _declares_ `T::aT' and `T::aS';
you will still need definitions for these static members somewhere else,
e.g.

 T T::aT;
 S T::aS;

The types `T' and `S' must be complete at the point of the definitions,
but they do not have to be complete at the point of the declarations.

>GNU g++ compiles this but doesn't construct other static members
>of class type (like aS) before calling T::T().

That behaviour is allowed by the current DWP.

>Is it a bug
>- that g++ compiles this at all or

Nope.
(Incidentally Cfront, Sun C++, and SGI C++ all accept this code too).

>- that aS doesn't get initialized before calling T::T()

Nope, that's not a bug either.

--
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
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: jlilley@empathy.com (John Lilley)
Date: 1996/12/10
Raw View
In article <aubryl7-ya023580000912961026020001@ecpnews.ecp.fr>,
aubryl7@caracal.cti.ecp.fr says...
>
>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.
>   have you tried declaring aS before aT ?

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).
 However, if the definitions are in separate translation units, the order
is implementation-defined (DWP s3.6.2.3).

So within a single translation unit, this is guaranteed to construct aT
before aS:

   T T::aT;
   S T::aS;

john lilley
---
[ 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: James Albert Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1996/12/10
Raw View
fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson) writes:

|>  Stephen Friedrich <1friedri@informatik.uni-hamburg.de> writes:
|>
|>  >Is the following legal C++ code?
|>
|>  Yes, I believe so (except that you omitted the definition of `S').
|>
|>  >class T {
|>  >   public:
|>  >      T();
|>  >      virtual ~T();
|>  >
|>  >      static T aT;
|>  >
|>  >      static S aS;
|>  >};
|>  >
|>  >A class that 'contains itself' seems a bit strange but since 'aT'
|>  >is declared static there is no recursion, so this would be possible ?!
|>
|>  Yes.  Note that the above code only _declares_ `T::aT' and `T::aS';
|>  you will still need definitions for these static members somewhere else,
|>  e.g.
|>
|>   T T::aT;
|>   S T::aS;
|>
|>  The types `T' and `S' must be complete at the point of the definitions,
|>  but they do not have to be complete at the point of the declarations.
|>
|>  >GNU g++ compiles this but doesn't construct other static members
|>  >of class type (like aS) before calling T::T().
|>
|>  That behaviour is allowed by the current DWP.

Allowed, or required?  If the definitions were written as above (in one
file, T::aT first, the ARM and earlier versions of the standard would
have required T::aT to be initialized *BEFORE* T::aS.

--
James Kanze         home:     kanze@gabi-soft.fr        +33 (0)3 88 14 49 00
                    office:   kanze@vx.cit.alcatel.fr   +33 (0)1 69 63 14 54
GABI Software, Sarl., 8 rue des Francs Bourgeois, F-67000 Strasbourg, France
       -- Conseils en informatique industrielle --


[ 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                             ]