Topic: Forward decl of enum types


Author: "Marco Dalla Gasperina" <marcodg@hp-vcd.vcd.hp.com>
Date: 1997/01/30
Raw View
Hello,

The following code compiles on the compilers I am using
but it doesn't seem right...



enum X;

struct Foo {
    X x;
};

Foo foo;

enum X { a,b,c,d };

void bar() { foo.x = a; }




Reading the April draft it says

|  The underlying type of an enumeration is an integral type, not  gratu
|  itously larger than int,4) that can represent  all  enumerator  values

So how does the compiler know how big Foo::x is?  I'm guessing the compilers
I'm using (MS) hardwire enums to be 'like ints' even though the it looks
like they can be any size.

I ask this because this would be a way to break some compile time
dependencies but I won't do it if its not legal (even though it
it seems to work).

thanks,
marco
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: stephen.clamage@eng.sun.com (Steve Clamage)
Date: 1997/01/31
Raw View
In article 4777450f@vcsmarcodg2, "Marco Dalla Gasperina"
<marcodg@hp-vcd.vcd.hp.com> writes:
>
>The following code compiles on the compilers I am using
>but it doesn't seem right...
>
>enum X;
>
>struct Foo {
>    X x;
>};
>
>Foo foo;
>
>enum X { a,b,c,d };
>
>void bar() { foo.x = a; }

It isn't right, and the compiler should give you a diagnostic.

Enums cannot be forward declared, and the locution
 enum X;
is not valid syntax. (A compiler might allow it as a nonstandard
extension, but should warn you about it.)

As you noted in your posting, enums can be of different sizes, so if
a compiler (nonstandardly) allows forward declarations, either it
always uses the longest integer size for enums, or it sometimes
generates bad code. (If ints and longs are the same size, and if
no longer integer size is supported, it wouldn't be unreasonable
always to use the same size for enums.)
---
Steve Clamage, stephen.clamage@eng.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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1997/01/31
Raw View
"Marco Dalla Gasperina" <marcodg@hp-vcd.vcd.hp.com> writes:

|>  The following code compiles on the compilers I am using
|>  but it doesn't seem right...
|>
|>
|>
|>  enum X;
|>
|>  struct Foo {
|>      X x;
|>  };
|>
|>  Foo foo;
|>
|>  enum X { a,b,c,d };
|>
|>  void bar() { foo.x = a; }

It isn't.  The compiler can, of course, support this as an extension,
but it is required to issue a diagnostic.

|>  Reading the April draft it says
|>
|>  |  The underlying type of an enumeration is an integral type, not  gratu
|>  |  itously larger than int,4) that can represent  all  enumerator  values
|>
|>  So how does the compiler know how big Foo::x is?  I'm guessing the compilers
|>  I'm using (MS) hardwire enums to be 'like ints' even though the it looks
|>  like they can be any size.

The size is implementation defined, with the exception that it cannot be
gratuously bigger than an int.  So if int's and long's have the same
size, the compiler can "know", provided that it always uses this size.

If int's and long's aren't the same size, it can't know, since if all of
the enum values will fit in an int, the enum cannot be bigger than an
int, and if they can't, it must be.

--
James Kanze      home:     kanze@gabi-soft.fr        +33 (0)1 39 55 85 62
                 office:   kanze@vx.cit.alcatel.fr   +33 (0)1 69 63 14 54
GABI Software, Sarl., 22 rue Jacques-Lemercier, F-78000 Versailles France
     -- Conseils en informatique industrielle --
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]