www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - `enum x;` - what is it?

reply Victor Porton <porton narod.ru> writes:
This declaration does compile:

enum x;

But what is it? Is it an equivalent of

enum x { }

?

What in the specification allows this looking a nonsense

enum x;

?
Aug 19 2020
next sibling parent FeepingCreature <feepingcreature gmail.com> writes:
On Wednesday, 19 August 2020 at 14:06:16 UTC, Victor Porton wrote:
 This declaration does compile:

 enum x;

 But what is it? Is it an equivalent of

 enum x { }

 ?

 What in the specification allows this looking a nonsense

 enum x;

 ?
It's an enum type whose members we don't know. So we can't declare "x var;" but we can declare "x* var;". It's the enum version of "struct SomeExternCStruct;".
Aug 19 2020
prev sibling next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 8/19/20 10:06 AM, Victor Porton wrote:
 This declaration does compile:
 
 enum x;
 
 But what is it? Is it an equivalent of
 
 enum x { }
 
 ?
 
 What in the specification allows this looking a nonsense
 
 enum x;
 
 ?
I use it as a symbol for UDAs. enum required; struct S { required int x; } which can then easily be found. What is it? I have no idea. I'm just using the name. I think it's treated as a forward declaration. Kind of like void foo(); -Steve
Aug 19 2020
prev sibling parent reply Victor Porton <porton narod.ru> writes:
On Wednesday, 19 August 2020 at 14:06:16 UTC, Victor Porton wrote:
 This declaration does compile:

 enum x;

 But what is it? Is it an equivalent of

 enum x { }

 ?

 What in the specification allows this looking a nonsense

 enum x;

 ?
Oh, found: "An empty enum body (For example enum E;) signifies an opaque enum - the enum members are unknown." But what this "unknown" does mean? How "unknown" differs from "none" in this context? The specification is unclear. It does not define the meaning of unknown. I will submit a bug report.
Aug 19 2020
parent FeepingCreature <feepingcreature gmail.com> writes:
On Wednesday, 19 August 2020 at 14:43:22 UTC, Victor Porton wrote:
 On Wednesday, 19 August 2020 at 14:06:16 UTC, Victor Porton 
 wrote:
 This declaration does compile:

 enum x;

 But what is it? Is it an equivalent of

 enum x { }

 ?

 What in the specification allows this looking a nonsense

 enum x;

 ?
Oh, found: "An empty enum body (For example enum E;) signifies an opaque enum - the enum members are unknown." But what this "unknown" does mean? How "unknown" differs from "none" in this context? The specification is unclear. It does not define the meaning of unknown. I will submit a bug report.
It means exactly what it says. The compiler doesn't know what members are in the enum. So you can't declare a variable of it, you can't use it directly.. you can p much only use it in pointers.
Aug 19 2020