Connecting Tech Pros Worldwide Forums | Help | Site Map

Determining the "real" size of a enum

Aryeh M. Friedman
Guest
 
Posts: n/a
#1: Jul 23 '05
If have something like the following declartion:

enum foo {One,Two,....,Ten};

How do I determine how many elements (enumerations)... in this case it is
obviously 10 but I don't want to hard code that fact

--Aryeh



Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Determining the "real" size of a enum


"Aryeh M. Friedman" <aryeh@m-net.arbornet.org> wrote...[color=blue]
> If have something like the following declartion:
>
> enum foo {One,Two,....,Ten};
>
> How do I determine how many elements (enumerations)... in this case it is
> obviously 10 but I don't want to hard code that fact[/color]

There is no way. The enumerators are not members, they are not elements,
they are not objects, they don't occupy memory, so any _existing_ means
to find out sizes of different things in C++ do not work. And there is
no special enum-specific mechanism because nobody ever needed one.

Why do you think you need to know the number of those constants? And why
is it obvious that in your example it's 10? And why do you think you need
to know that number, yet can't hard-code it?

V


Aryeh M. Friedman
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Determining the "real" size of a enum



"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:OISdnXEsdsqrZ3jcRVn-hg@comcast.com...[color=blue]
> "Aryeh M. Friedman" <aryeh@m-net.arbornet.org> wrote...[color=green]
>> If have something like the following declartion:
>>
>> enum foo {One,Two,....,Ten};
>>
>> How do I determine how many elements (enumerations)... in this case it is
>> obviously 10 but I don't want to hard code that fact[/color]
>
> There is no way. The enumerators are not members, they are not elements,
> they are not objects, they don't occupy memory, so any _existing_ means
> to find out sizes of different things in C++ do not work. And there is
> no special enum-specific mechanism because nobody ever needed one.
>
> Why do you think you need to know the number of those constants? And why
> is it obvious that in your example it's 10? And why do you think you need
> to know that number, yet can't hard-code it?[/color]

1) Since I need to create an array of objects.. one per token... for example
enum foo {Orange, Bannana, Apple}; foo Fruit["sizeof foo"]

2) In the example I gave it is clearly ten since it is using counting words
but that is besides the point here

3) It is for a system where new "tokens" can be added at compile time to the
enum and since we need the size to make the array and/or any associated
loops for the array need some "constant" that is the size of enum but do not
want to maintain two seperate values when 1 should do (the enum it self and
size
if derived)... doing it the other way is just asking for bugs in the lonbg
run.

--Aryeh


Victor Bazarov
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Determining the "real" size of a enum


"Aryeh M. Friedman" <aryeh@m-net.arbornet.org> wrote...[color=blue]
>
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:OISdnXEsdsqrZ3jcRVn-hg@comcast.com...[color=green]
>> "Aryeh M. Friedman" <aryeh@m-net.arbornet.org> wrote...[color=darkred]
>>> If have something like the following declartion:
>>>
>>> enum foo {One,Two,....,Ten};
>>>
>>> How do I determine how many elements (enumerations)... in this case it
>>> is obviously 10 but I don't want to hard code that fact[/color]
>>
>> There is no way. The enumerators are not members, they are not elements,
>> they are not objects, they don't occupy memory, so any _existing_ means
>> to find out sizes of different things in C++ do not work. And there is
>> no special enum-specific mechanism because nobody ever needed one.
>>
>> Why do you think you need to know the number of those constants? And why
>> is it obvious that in your example it's 10? And why do you think you
>> need
>> to know that number, yet can't hard-code it?[/color]
>
> 1) Since I need to create an array of objects.. one per token... for
> example enum foo {Orange, Bannana, Apple}; foo Fruit["sizeof foo"][/color]

And what is that for? To assign each element the corresponding value?
Then all you need to do is to have a macro and proper initialiser:

#define MYENUMS { Orange, Bannana, Apple }

enum foo MYENUMS;
foo Fruit[] = MYENUMS;
[color=blue]
> 3) It is for a system where new "tokens" can be added at compile time to
> the enum[/color]

Huh?
[color=blue]
> and since we need the size to make the array and/or any associated loops
> for the array need some "constant" that is the size of enum but do not
> want to maintain two seperate values when 1 should do (the enum it self
> and size
> if derived)... doing it the other way is just asking for bugs in the lonbg
> run.[/color]

I would like to hear a bit more about the "system" where source code
apparently changes at compile time. I am sure I've just fallen behind
on programming methods.

V


Andre Kostur
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Determining the "real" size of a enum


"Aryeh M. Friedman" <aryeh@m-net.arbornet.org> wrote in
news:F6nFd.116$2l2.10@fe11.lga:
[color=blue]
> 1) Since I need to create an array of objects.. one per token... for
> example enum foo {Orange, Bannana, Apple}; foo Fruit["sizeof foo"]
>
> 2) In the example I gave it is clearly ten since it is using counting
> words but that is besides the point here
>
> 3) It is for a system where new "tokens" can be added at compile time
> to the enum and since we need the size to make the array and/or any
> associated loops for the array need some "constant" that is the size
> of enum but do not want to maintain two seperate values when 1 should
> do (the enum it self and size
> if derived)... doing it the other way is just asking for bugs in the
> lonbg run.[/color]

A frequent "trick" that is used (or at least I've seen it frequently):

enum fruit_e { Orange, Bannana, Apple, LastFruit }; fruit_e Fruit
[LastFruit];

Note that this only works if you don't assign your own values to the enum,
such as:

enum fruit_e { Orange = 10, Bannana, Apple, LastFruit };

That would result in LastFruit being 13... obviously not the intended
value....

Mike Wahler
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Determining the "real" size of a enum



"Aryeh M. Friedman" <aryeh@m-net.arbornet.org> wrote in message
news:QpmFd.102$1I1.53@fe11.lga...[color=blue]
> If have something like the following declartion:
>
> enum foo {One,Two,....,Ten};
>
> How do I determine how many elements (enumerations)... in this case it is
> obviously 10[/color]

Actually it's not obvious.
[color=blue]
>but I don't want to hard code that fact[/color]

If you only use default values, you can add a 'dummy'
value as the last one, and that value will equal
the number of values preceding it.

enum foo{x, y, z, count};

-Mike


Andre Kostur
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Determining the "real" size of a enum


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in
news:2aOdnS3YaK2jnHvcRVn-1w@comcast.com:
[color=blue][color=green]
>> 1) Since I need to create an array of objects.. one per token... for
>> example enum foo {Orange, Bannana, Apple}; foo Fruit["sizeof foo"][/color]
>
> And what is that for? To assign each element the corresponding value?
> Then all you need to do is to have a macro and proper initialiser:
>
> #define MYENUMS { Orange, Bannana, Apple }
>
> enum foo MYENUMS;
> foo Fruit[] = MYENUMS;[/color]

That's a neat stunt... hadn't thought of that way before...

msalters
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Determining the "real" size of a enum



Aryeh M. Friedman wrote:[color=blue]
> If have something like the following declartion:
>
> enum foo {One,Two,....,Ten};
>
> How do I determine how many elements (enumerations)...
> in this case it is
> obviously 10 but I don't want to hard code that fact[/color]

How many elements are there in

enum access { None, Read, Write, ReadWrite };
or in
enum access { None, Read, Write };
Of course, if I wrote the latter, I'd still be able
to write Read|Write instead of ReadWrite, and it
would mean exactly the same ( == 3)
Any bitwise combination of enumerators is also
a legal value for an object of that enum type.

HTH,
Michiel Salters

Closed Thread