473,725 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determining the "real" size of a enum

If have something like the following declartion:

enum foo {One,Two,....,T en};

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 14837
"Aryeh M. Friedman" <ar***@m-net.arbornet.or g> wrote...
If have something like the following declartion:

enum foo {One,Two,....,T en};

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.********@com Acast.net> wrote in message
news:OI******** ************@co mcast.com...
"Aryeh M. Friedman" <ar***@m-net.arbornet.or g> wrote...
If have something like the following declartion:

enum foo {One,Two,....,T en};

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.or g> wrote...

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:OI******** ************@co mcast.com...
"Aryeh M. Friedman" <ar***@m-net.arbornet.or g> wrote...
If have something like the following declartion:

enum foo {One,Two,....,T en};

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.or g> 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.or g> wrote in message
news:Qp******** ******@fe11.lga ...
If have something like the following declartion:

enum foo {One,Two,....,T en};

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.********@com Acast.net> wrote in
news:2a******** ************@co mcast.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,....,T en};

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
2983
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 (again, its not important that its Windows) which ends up calling a compare function in my code: int CompareFunc(char* str1, char* str2) { } this function returns -1, 0 or 1 which gets passed on to the internal quick
4
3133
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". Therefore everytimes when I change the real table, I also needed open the view table and save it by SQL enterprise manager manually for update the view table field.
0
1081
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 server portions are both running on my PC. I was wondering if there was some way that for "real world" performance, I could somehow "throttle" the tcp/ip stack so that calls to localhost had some lag to represent the latency/bandwidth of a real...
0
393
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, while still allowing faster searches than a sequential scan. * indexing on LOWER(field)/UPPER(field) to allow case-insensitive searches or case-insensitive unique constraint.
5
39924
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 (FIFO): The bullet in a machine gun..(you cannot fire 2 bullets at the same time) Stack (LIFO): The tennis balls in their container.. (you cannot remove 2 balls at the same time)
1
3130
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. Open Source that people consider to use classes and patterns correctly. I lack a senior person to lead me in this so I feel I'm losing out on only using bare PHP class features and not really knowing how to design
3
1757
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 on it, as an example of what a "real life" __metaclass__ might do for those who may never have seen such a thing themselves. http://markshroyer.com/blog/2007/11/09/tilting-at-metaclass-windmills/ So what's the verdict? Incorrect? Missed the...
71
3325
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 files included in the Windows version of Python distribution. I'm not sure how much of the C-Python is implemented in C but I think the more modules implemented in C, the better performance and lower memory footprint it will get. I wonder if it's...
0
174
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 get the error. And honestly, you HAVE to know something about your Class. otherwise, how do you know which method to call?
0
8749
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9250
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8079
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6699
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4776
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3216
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2626
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2153
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.