473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Determining the "real" size of a enum

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
Jul 22 '05 #1
7 14792
"Aryeh M. Friedman" <ar***@m-net.arbornet.org> wrote...
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


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
Jul 22 '05 #2

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:OI********************@comcast.com...
"Aryeh M. Friedman" <ar***@m-net.arbornet.org> wrote...
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


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?


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
Jul 22 '05 #3
"Aryeh M. Friedman" <ar***@m-net.arbornet.org> wrote...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:OI********************@comcast.com...
"Aryeh M. Friedman" <ar***@m-net.arbornet.org> wrote...
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
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?


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


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;
3) It is for a system where new "tokens" can be added at compile time to
the enum
Huh?
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.


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
Jul 22 '05 #4
"Aryeh M. Friedman" <ar***@m-net.arbornet.org> wrote in
news:F6**************@fe11.lga:
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.


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....

Jul 22 '05 #5

"Aryeh M. Friedman" <ar***@m-net.arbornet.org> wrote in message
news:Qp**************@fe11.lga...
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
Actually it's not obvious.
but I don't want to hard code that fact


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
Jul 22 '05 #6
"Victor Bazarov" <v.********@comAcast.net> wrote in
news:2a********************@comcast.com:
1) Since I need to create an array of objects.. one per token... for
example enum foo {Orange, Bannana, Apple}; foo Fruit["sizeof foo"]


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;


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

Jul 22 '05 #7

Aryeh M. Friedman wrote:
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


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

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Nobody | last post by:
Heres the deal... I have an application where I have a list (as in a Windows list control, but thats not important) displayed to the user. I sort this list based on the list controls sort function...
4
by: Silas | last post by:
Hi, I use view to join difference table together for some function. However, when the "real" table fields changed (e.g. add/delete/change field). The view table still use the "old fields". ...
0
by: Simon Verona | last post by:
I have some Windows Forms software that I'm developing that uses a remote server (called using remoting) to provide the business rules and dataaccess. For development purposes the client and...
0
by: David Garamond | last post by:
I want to know how functional indexes are used "in the real world". Here are the common uses: * non-unique index on the first parts of a longish text field (SUBSTRING(field)) to save disk space,...
5
by: playagain | last post by:
Please help me to build a list of examples of stack and queue in real life situation... Conditions: The object concerned must only one object. And the object must be tangible. Example: Queue...
1
by: Tyno Gendo | last post by:
Hi everyone I need to move on a step in my PHP... I know what classes are, both in PHP4 and 5 and I'm aware of "patterns" existing, but what I'm looking for are some real world projects eg....
3
by: Mark Shroyer | last post by:
I guess this sort of falls under the "shameless plug" category, but here it is: Recently I used a custom metaclass in a Python program I've been working on, and I ended up doing a sort of write-up...
71
by: Jack | last post by:
I understand that the standard Python distribution is considered the C-Python. Howerver, the current C-Python is really a combination of C and Python implementation. There are about 2000 Python...
0
by: Ignacio Machin ( .NET/ C# MVP ) | last post by:
The difference between compile & runtime. CreateInstance works at runtime, you can pass ANY string to it (even an incorrect one like "123123123123") and it will compile Only at runtime you will...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.