Topic: [Q] Problem with understanding standard
Author: osv@newcom.kiae.su (Sergey Ostashenko )
Date: Fri, 6 Jan 1995 13:03:20 GMT Raw View
Hi!
I'm having problems with defining recursive types in C++. While trying
to solve (or rather to understand) them I encountered with the text
(ARM, section 7.1.3, page 107):
"A 'typedef-name' that names a class is a 'class-name' (9.1). The synonym
may not be used after a 'class', 'struct' or 'union' prefix ... etc.
For example:
struct S {
S();
~S();
};
typedef struct S T;
S a = T(); // ok
struct T * p; // error
"
I wonder what is the motivation behind this, I mean the prohibiting
to use the synonym after 'struct', 'class' or 'union'?
Could someone who knows explain this?
In GNU g++ the feature is implemented inconsistently. The code below
compiles okay without an error
struct S {};
typedef struct S T;
struct T * p;
while the following fails to compile
struct T * p;
struct S {};
typedef struct S T;
t.cc:3: conflicting types for `struct S'
t.cc:1: previous declaration as `struct T'
This same error occured when I tried to define a type
which refers to a list of instances of itself and class template List
was used (Is it a good idea to have class template for generic list?):
class Filter {
...
class FilterList *p;
...
};
typedef List<Filter> FilterList;
This forces me to write something like
class FilterList : public List<Filter> {
.....
// ctors and dtors are not inherited :-(
};
Maybe there is some sort of ambiguity which will arise if one was to
allowed to write 'class' before typedef-ed names? Or maybe this all
is just g++ bugs? What do you think?
Thanks,
Sergey Ostashenko
KIAE, Moscow, Russia
email: osv@kiae.su
Author: Viktor Yurkovsky <n4mation@panix.com>
Date: 8 Jan 1995 15:30:01 GMT Raw View
osv@newcom.kiae.su (Sergey Ostashenko ) wrote:
To answer a part of your question:
><snip>
>
> In GNU g++ the feature is implemented inconsistently. The code below
> compiles okay without an error
>
> struct S {};
> typedef struct S T;
> struct T * p;
>
> while the following fails to compile
>
> struct T * p;
> struct S {};
> typedef struct S T;
>
> t.cc:3: conflicting types for `struct S'
> t.cc:1: previous declaration as `struct T'
>
This should error always as the name T is already used and re-declaring
as a typedef should fail as it is in the same namespace.
Do svidania
-------------------------------
n4mation@panix.com |
Data In Formation Inc. |
|
Victor Yurkovsky |
|
Compiler maker |
|
Special discounts for |
weddings and funerals. |
_______________________________|