non-integral enums?

Unknown W. Brackets unknown at simplemachines.org
Sun Feb 26 14:48:47 PST 2006


Exactly.  But there's also that tiny last bit of your last sentence.  In 
other words, this does not compile:

enum X
{
	Y
}

int main()
{
	int i;
	X x;

	x = i;

	return 0;
}

But this does:

static struct X
{
	const int Y = 1;
}

int main()
{
	int i;
	typeof(X.Y) x;

	x = i;

	return 0;
}

You are very correct that this is just about the only difference between 
enums and namespaced constants.  But this is, in my mind, a desirable 
difference, as much as you may marginalize it.

-[Unknown]


> Actually any enum without an identifier just introduces constants into 
> the current scope, as per the previously cited examples.  So in essence, 
> an enum is just a collection of constants (of identical type) and an 
> optional namespace to contain them, and a special rule that enum 
> namespaces can be used as types.
> 
> -- Chris Nicholson-Sauls
> 
> Unknown W. Brackets wrote:
>> Actually, isn't it more like a typedef (but not completely) and a 
>> namespace?
>>
>> In that case, you can do colors.red - you have to do this right now if 
>> you want to use constants: (iirc)
>>
>> static struct colors
>> {
>>    const int red = 1;
>> }
>>
>> But then it is an int, and implicit casting is not as described by 
>> Thomas Kuehne.
>>
>> -[Unknown]
>>
>>
>>> On Sat, 25 Feb 2006 13:12:10 +1100, Wang Zhen <nehzgnaw at gmail.com> 
>>> wrote:
>>>
>>>
>>>
>>> [snip]
>>>
>>>> How different is enum from constants then?
>>>
>>>
>>> I agree. I thought enum were invented as a shorthand way of doing ...
>>>
>>>   const int red = 1,
>>>             blue = 2,
>>>             green = 3;
>>>
>>> Instead we can do 'enum colors { red, blue, green }'
>>>
>>> To saving the coder having to recalculate the 'enumerated' names when 
>>> some were added or deleted.
>>>
>>>   const int red = 1,
>>>             yellow = 2,
>>>             blue = 3,
>>>             green = 4;
>>>
>>> Instead we can do 'enum colors { red, yellow, blue, green }'
>>>
>>> Otherwise they are just constants of varying values.
>>>
>>> --Derek Parnell
>>> Melbourne, Australia



More information about the Digitalmars-d mailing list