Topic: forward declarations and static?


Author: heinz@hwg.muc.de (Heinz Wrobel )
Date: Sun, 17 Jul 94 13:31:19 GMT
Raw View
Is it true that the language does not have a construct to make forward
declaration possible of some data that is supposed to be static, i.e. file
scope internal linkage?

/*------------------------------------------------------------------------*/
/* Should be the forward declaration */
extern int i;

/* Some use of i */

int i;
/*------------------------------------------------------------------------*/

works, but i has external linkage then.

The code below is not what I want as the first line is not a declaration.

/*------------------------------------------------------------------------*/
/* Should be the forward declaration */
static int i;

/* Some use of i */

static int i;
/*------------------------------------------------------------------------*/

Now, having forward declarations like this might be considered ugly, I
know. But I'd like to know if there is a way to get this effect. A second
problem might be if there should be a way, if there isn't.

--
Heinz Wrobel        Edotronik GmbH: heinz@edohwg.adsp.sub.org
                    Private Mail:   heinz@hwg.muc.de
My private FAX: +49 89 850 51 25, I prefer email




Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Mon, 18 Jul 1994 01:37:41 GMT
Raw View
In article <heinz.0nif@hwg.muc.de> heinz@hwg.muc.de (Heinz Wrobel   ) writes:
>Is it true that the language does not have a construct to make forward
>declaration possible of some data that is supposed to be static, i.e. file
>scope internal linkage?

Yes.  This is a real problem, and one that I have been meaning to bring
to the standardization committee's attention for some time.

In ANSI C, there is this concept of ``tenative definitions'' of file-scope
objects, but in C++ there is no such concept.  Thus, there is no way for
a C++ programmer to ``forward declare'' something which is supposed to
have internal (i.e. `static') linkage.  The implication is that static
initialization of mutually referential structures cannot be done in C++
if the programmer wants the objects in question to have internal linkage.
(It *can* be done in ANSI/ISO C however.)

Example:

 struct S1;
 struct S2 { struct S1 *p; };
 struct S1 { struct S2 *p; };

 static struct S1 obj1; /* tenative definition in C */
 static struct S2 obj2 = { &obj1 };
 static struct S1 obj1 = { &obj2 };

This code is perfectly valid in ANSI/ISO C, but is illegal in C++ (because
in C++, this code contains two definitions of `obj1', which is not allowed).

The only solution in C++ is to make the objects have external linkage.
That may not be want you want, but in C++ you're stuck with it. :-(

P.s.  Note that it was decided (VERY recently, i.e. at the Waterloo meeting
of X3J16/WG21) that expressions which could be used as static initializers
in ANSI/ISO C will be treated as static initializers in C++ also.  Thus,
whereas the draft C++ standard formerly indicated that any non-integral
initializer (such as a static floating-point expression like `3.4+3.5' or
an address constant like `&foo') would be treated as a DYNAMIC initializer
(and initialized in the second initialization pass, after all of the integral
static initializations had already taken effect) it will in future say that
any expression which was considered to be a ``constant expression'' in C
will be treated as an ``early'' static initializer in C++ also.  I mention
this only because it may be relevant to the proper interpretation of the
above example.

--

-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- domain addr: rfg@netcom.com ----------- Purveyors of Compiler Test ----
---- uucp addr: ...!uunet!netcom!rfg ------- Suites and Bullet-Proof Shoes -