Topic: Forward declarations of typedef'd types...


Author: James.Kanze@dresdner-bank.com
Date: 1999/04/28
Raw View
In article <slrn7i94sh.m68.sbnaran@bardeen.ceg.uiuc.edu>,
  sbnaran@KILL.uiuc.edu wrote:
> [ This is on-topic, but has been beaten to death in this group.
>   Please consult DejaNews. (Followups will be accepted.) --mod ]
>
> On 26 Apr 1999 15:59:52 GMT, Paul <paulp.removethis@ccnet.com> wrote:
>
> >In header1.h I have the following:
> >   typedef vector<string> StringArray;
> >
> >in header2.h I have the following:
> >   void DoSomething(StringArray& stringArray);
>
> You can't forward declare typedefs.
>
> One reason that comes to mind: sizeof(T1*) may be different from
> sizeof(T2*), which makes it impossible to use a forward typedef.
> If T1 and T2 are user types, then sizeof(T1*)==sizeof(T2*), so
> forward class declarations work.
>    class A; // forward class declaration
>    A *const a=0; // ok: sizeof(a) known
> If T1 or T2 are builtin types, then the sizeofs may be different.
>    typedef B; // forward typedef declaration (hypothetical syntax)
>    B *const b=0; // error: sizeof(b) not defined
>
> Are there any other reasons?

Even in C, forward declarations only concerned struct's, and for various
reasons (including this one), all pointers to struct/class must have the
same representation.

The problem with typedef is that it doesn't introduce a new type, only
an alias. So the function DoSomething must be "mangled" as though it was
DoSomething( vector< string >& ).  Which is only possible if the
compiler knows that StringArray is typedef'ed to vector< string >.

--
James Kanze                         mailto: James.Kanze@dresdner-bank.com
Conseils en informatique orientie objet/
                        Beratung in objekt orientierter Datenverarbeitung
Ziegelh|ttenweg 17a, 60598 Frankfurt, Germany  Tel. +49 (069) 63 19 86 27

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: paulp.removethis@ccnet.com (Paul)
Date: 1999/04/26
Raw View
In header1.h I have the following:
   typedef vector<string> StringArray;

in header2.h I have the following:
   void DoSomething(StringArray& stringArray);

I want to forward declare the StringArray type
in header2.h, because I don't want to/can't #include
header1.h from header2.h. I am not sure how to do
this. Nothing I have tried is allowed by the compiler,
and I can't find anything in the Standard regarding
this. Also, I'd rather not forward declare the
StringArray type in header2.h like this:
   typedef vector<string> StringArray;

By doing the above, header2.h is dependent on being
manually synchronized with header1.h, which is bad.

Paul



[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran)
Date: 1999/04/26
Raw View
[ This is on-topic, but has been beaten to death in this group.
  Please consult DejaNews. (Followups will be accepted.) --mod ]

On 26 Apr 1999 15:59:52 GMT, Paul <paulp.removethis@ccnet.com> wrote:

>In header1.h I have the following:
>   typedef vector<string> StringArray;
>
>in header2.h I have the following:
>   void DoSomething(StringArray& stringArray);

You can't forward declare typedefs.

One reason that comes to mind: sizeof(T1*) may be different from
sizeof(T2*), which makes it impossible to use a forward typedef.
If T1 and T2 are user types, then sizeof(T1*)==sizeof(T2*), so
forward class declarations work.
   class A; // forward class declaration
   A *const a=0; // ok: sizeof(a) known
If T1 or T2 are builtin types, then the sizeofs may be different.
   typedef B; // forward typedef declaration (hypothetical syntax)
   B *const b=0; // error: sizeof(b) not defined

Are there any other reasons?

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]