Topic: forward declare typedef?


Author: "Sean Palmer" <spalmer@player1.com>
Date: 1998/02/27
Raw View
I think it would be nice to be able to forward declare (or even just legally
re-declare) typedefs.  I'm sure C programmers would appreciate this due to C
forcing you to typedef a struct in order to not have to use the struct
keyword to access the struct namespace.  This doesn't apply to just C either
though the need for it is most apparent there.
Unfortunately then there is no way to forward declare that typedef
identifier (though you can forward declare the struct itself and use the
struct keyword)

like this:

struct  X;

typedef struct X x;  //illegal, X is incomplete

typedef struct X x;  //redeclaration is illegal also, this will cause
                               //an error below

x an_x;  //there doesn't seem to be a way to do this without a #define

struct X an_x;  //this works though

typedef struct X {
  int a;
} x;


If you could forward declare the typedef, you could do code like this:

typedef x;

x* an_x;

typedef struct X {
  int a;
} x;

and if you could redeclare a typedef, you could do code like this:

struct X;

typedef struct X x;

x* an_x;

typedef struct X {
  int a;
} x;

Either way you could get the job done.




[ 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: clamage@Eng.sun.com (Steve Clamage)
Date: 1998/02/28
Raw View
In article 0@dnews.pacificnet.net, "Sean Palmer" <spalmer@player1.com> writes:
>I think it would be nice to be able to forward declare (or even just legally
>re-declare) typedefs.  I'm sure C programmers would appreciate this due to C
>forcing you to typedef a struct in order to not have to use the struct
>keyword to access the struct namespace.  This doesn't apply to just C either
>though the need for it is most apparent there.
>Unfortunately then there is no way to forward declare that typedef
>identifier (though you can forward declare the struct itself and use the
>struct keyword)

I don't really understand what you are getting at, but in any event
this is a C++ discussion group, and nothing put into the C++
standard affects the C standard.

>
>like this:
>
>struct  X;
>
>typedef struct X x;  //illegal, X is incomplete

It is perfectly legal in both C and C++. You are limited in what you
can do with the struct and typedef identifiers, but you can
declare pointers. For example, here is a complete translation unit:
 struct X;
 typedef struct X x; /* in C++ could just say "typedef X x;"
 x* px = 0;
If you have a C or C++ compiler that rejects the code, complain to
the vendor.

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