Forward Declarations

Forward Declarations

Post by REH » Thu, 08 Apr 2010 02:37:09


Will the next standard address the difficulty of forward declaring
types that aren't simple classes or structs, such as typedefs or
classes instantiated from templates? I mean, it would be nice to
simple do this:

typedef X;

Or, this:

class X;

without having to know the details for X (e.g., whether or not it was
instantiated from a template or what that template looked like).


REH
 
 
 

Forward Declarations

Post by REH » Thu, 08 Apr 2010 02:39:05


Oops, so sorry. Wrong group!

 
 
 

Forward Declarations

Post by Andrew Poe » Thu, 08 Apr 2010 06:17:44


Even in C, you have the problem of the compiler not knowing
anything about X; after all, if it knew, you wouldn't need
to use forward declarations in the first place!

--
Andrew Poelstra
http://www.yqcomputer.com/
 
 
 

Forward Declarations

Post by REH » Thu, 08 Apr 2010 09:55:49

On Apr 6, 5:17m, Andrew Poelstra < XXXX@XXXXX.COM >




Not true. Forward declarations are incomplete types. Without knowing
anything about the type, you can still define pointers to them,
functions that return them, etc. They allow defining interfaces with
opaque types, or to allow header files to utilize types (in a limited
way) without it having to put in a lot of other headers.

REH
 
 
 

Forward Declarations

Post by Wojtek Ler » Thu, 08 Apr 2010 12:33:05


Do you mean you would want to forbid forward declarations of function types?

typedef X;
extern X foo; // Is foo a function or an object?
void *pfoo = &foo; // Is this valid?
typedef int X(void); // Ah, so foo is a function after all, and the
conversion to void* was invalid!


What if you declare a function returning the type, but later it turns out
that the type is a function type or an array type?

typedef X;
extern X foo(int);
typedef int X[]; // Would you make *this* a constraint violation?
 
 
 

Forward Declarations

Post by Andrew Poe » Thu, 08 Apr 2010 13:04:42


While they are incomplete types, they are not completely
unknown types. For example, forward-declaring a struct
tells the compiler that the type is a struct, which in
turn tells the compiler how to represent its pointers
(since the C standard guaranteeds struct pointers to
share the same representation, not true of all types
in general).

And of course, if you forward-declare a function, that
completely defines the interface to said function. But
I don't think we're talking about function declarations.


If you could arbitrarily specify types, like:

type number;

without ever telling the compiler what 'number' means
(is it a float? an integer? a complex-value struct?),
it would make forward-declarations superfluous.

--
Andrew Poelstra
http://www.yqcomputer.com/
 
 
 

Forward Declarations

Post by REH » Thu, 08 Apr 2010 22:15:06


no.
 
 
 

Forward Declarations

Post by REH » Thu, 08 Apr 2010 22:16:10

On Apr 7, 12:04m, Andrew Poelstra < XXXX@XXXXX.COM >


All I am talking about are structs.

REH
 
 
 

Forward Declarations

Post by Wojtek Ler » Thu, 08 Apr 2010 23:20:40


You mean you do *not* mean to include function types?

The standard already allows things like

struct foo;

and

typedef struct foo foo_t;
 
 
 

Forward Declarations

Post by REH » Thu, 08 Apr 2010 23:30:23


As I wrote in an earlier post, I posted here by mistake. It isn't
relevant to C.

REH
 
 
 

Forward Declarations

Post by Eric Sosma » Thu, 08 Apr 2010 23:48:41


Wait just a sec: Didn't you start this whole thread by asking
about forward declarations for (quote) types that aren't simple
classes or structs (endquote)?

But even so: If all you are talking about are structs, C
already has a way to declare them incompletely without going
into detail. (Unions, too.)

--
Eric Sosman
XXXX@XXXXX.COM
 
 
 

Forward Declarations

Post by REH » Thu, 08 Apr 2010 23:51:11


> ait just a sec: Didn't you start this whole thread by asking> > about forward declarations for (quote) types that aren't simple> > classes or structs (endquote)?> >> > ut even so: If all you are talking about are structs,>C
> already has a way to declare them incompletely without goi>g
> into detail. Unions, too.)

Again: As I wrote in an earlier post, I posted here by mistake. It
isn't relevant to C.

REH
 
 
 

Forward Declarations

Post by Eric Sosma » Fri, 09 Apr 2010 00:44:55


Then why keep posting? Sheesh ...

--
Eric Sosman
XXXX@XXXXX.COM
 
 
 

Forward Declarations

Post by REH » Fri, 09 Apr 2010 01:04:20


> hen why keep posting? heesh ...> >

Wait, you are criticizing me? Why are YOU posing? You're the ones who
keep posting to a thread in which I apologized for posting to the
wrong group. You fault me for responding to you in order to inform you
of that fact?

REH
 
 
 

Forward Declarations

Post by Wojtek Ler » Fri, 09 Apr 2010 01:55:04


I was replying to the post where you said "Not true" in response to Andrew
Poelstra's comment starting with "Even in C". If *that* post wasn't meant
to be relevant to C, you should have made it a little clearer.

Or not bothered at all -- why post a response that isn't relevant to the
post you're responding to?