Topic: Recursive structures and static


Author: seurer@rchland.ibm.com (Bill Seurer)
Date: 1997/07/23
Raw View
The following is OK:

  class SomeClass {
      public:
        int a,b,c,d;
        static SomeClass recursive;
  } c;

by 9.2.2 and 9.4.2.2 of the standard and in fact compiles fine with our
compiler.  However, someone asked me if expressions such as

  void P ()
  {
      c.recursive.recursive.recursive.a = 1;
      c.recursive.recursive.b = 1;
  }

were OK (they work fine) and if I could point out where the standard
allows this.  I don't see anything that doesn't allow it and it looks
fine semantically.  It would seem to be implicitly OK by the normal
semantics but I couldn't find anything that explicitly allows it.
Did I just miss something?
--

Bill Seurer     ID Tools and Compiler Development      IBM Rochester, MN
BillSeurer@vnet.ibm.com                            BillSeurer AT aol.com
http://members.aol.com/BillSeurer  (replace " AT " with "@" to email me)
SPAM us: L7c@lWV.com JvI@NLw.com bsP@ltZ.com tmQ@vsP.com WTC@mcD.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: 1997/07/23
Raw View
seurer@rchland.ibm.com (Bill Seurer) writes:

 >The following is OK:
 >
 >  class SomeClass {
 >      public:
 >        int a,b,c,d;
 >        static SomeClass recursive;
 >  } c;
 >
 >by 9.2.2 and 9.4.2.2 of the standard

Yep.

 >However, someone asked me if expressions such as
 >
 >  void P ()
 >  {
 >      c.recursive.recursive.recursive.a = 1;
 >      c.recursive.recursive.b = 1;
 >  }
 >
 >were OK

Yep, that's OK.

 >... and if I could point out where the standard
 >allows this.  I don't see anything that doesn't allow it and it looks
 >fine semantically.  It would seem to be implicitly OK by the normal
 >semantics

Correct.

 >but I couldn't find anything that explicitly allows it.

I doubt if it is stated explicitly, because there's no need.
The standard can't enumerate all possible legal programs --
there's too many of them! -- so some things must be left implicit.

Proving the validity of this code is a quite simple inference: `c' is
an expression of type `SomeClass', and `recursive' is a static member
of `SomeClass' (whose type happens to be `SomeClass'), hence by 5.2.5
[expr.ref], `c.recursive' is a valid expression whose type is `SomeClass'.
Then, since `c.recursive' is an expression of type `SomeClass', by the
same reasoning `c.recursive.recursive' is a valid expression of type
`SomeClass'.  And similarly for `c.recursive.recursive.recursive'.

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