Programatically interrogating a C/C++ enumeration?
-
If I have, say:
typedef enum { val1 = 0, val2 = 1, val3 = 5 } MyEnum;
Is there any programatic way I can find out how many constants MyEnum contains, what their highest and lowest values are, etc? I assume not, but I live in hope :-D -
If I have, say:
typedef enum { val1 = 0, val2 = 1, val3 = 5 } MyEnum;
Is there any programatic way I can find out how many constants MyEnum contains, what their highest and lowest values are, etc? I assume not, but I live in hope :-DI don't think there is. Afterall, what you are doing is defining a new enumeration type, which grants a value to certain types of selections. So a MyEnum type can only contain three possible values. But as you require the definition of the enumeration type in order to use it anyhow, then how come you can't check the definition itself which of the values is the highest or lowest ? Naturally, you could check the size of the enumeration structure, but I believe it would return a value of no significant meaning. I have never tested. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.
-
I don't think there is. Afterall, what you are doing is defining a new enumeration type, which grants a value to certain types of selections. So a MyEnum type can only contain three possible values. But as you require the definition of the enumeration type in order to use it anyhow, then how come you can't check the definition itself which of the values is the highest or lowest ? Naturally, you could check the size of the enumeration structure, but I believe it would return a value of no significant meaning. I have never tested. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.
Antti Keskinen wrote: Naturally, you could check the size of the enumeration structure, but I believe it would return a value of no significant meaning. I have never tested. It'll be sizeof(int) or sizeof(short) (I *think* it's supposed to be sizeof(int), but I'm not 100%, and I know some compilers allow one to make enums 16 bit) -- I am perpetual, I keep the country clean.
-
If I have, say:
typedef enum { val1 = 0, val2 = 1, val3 = 5 } MyEnum;
Is there any programatic way I can find out how many constants MyEnum contains, what their highest and lowest values are, etc? I assume not, but I live in hope :-DYou want C#, it adds exactly these things to the language, and more ( i.e. more for enums ). But in C++, the answer is, no. The best you can do is end each enum with an element called ItemCount, or something. It will then hold the number you want, as the list is 0 based. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
-
If I have, say:
typedef enum { val1 = 0, val2 = 1, val3 = 5 } MyEnum;
Is there any programatic way I can find out how many constants MyEnum contains, what their highest and lowest values are, etc? I assume not, but I live in hope :-DNo, the standard way of doing this is something like
enum MyEnum
{
meVal1 = 0,
meVal2 = 1,
meVal3 = 5,me__MinVal = 0,
me__MaxVal = 5,
me__Count = 3
};Otherwise, enums are just slightly typed constants. They aren't a particularly solid type (like in pascal), but is does have its advantages. //.
-
If I have, say:
typedef enum { val1 = 0, val2 = 1, val3 = 5 } MyEnum;
Is there any programatic way I can find out how many constants MyEnum contains, what their highest and lowest values are, etc? I assume not, but I live in hope :-DThe way I've always done this is:
enum MyEnum {
val1 = 0,
val2 = 1,
val3 = 5
MyEnum_min = val1,
MyEnum_max = val3,
MyEnum_cnt = 3
};
Software Zen:
delete this;