Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Programatically interrogating a C/C++ enumeration?

Programatically interrogating a C/C++ enumeration?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
6 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gary Chapman
    wrote on last edited by
    #1

    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

    A C M G 4 Replies Last reply
    0
    • G Gary Chapman

      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

      A Offline
      A Offline
      Antti Keskinen
      wrote on last edited by
      #2

      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.

      J 1 Reply Last reply
      0
      • A Antti Keskinen

        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.

        J Offline
        J Offline
        Jorgen Sigvardsson
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • G Gary Chapman

          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

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          You 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

          1 Reply Last reply
          0
          • G Gary Chapman

            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

            M Offline
            M Offline
            Michael Geddes
            wrote on last edited by
            #5

            No, 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. //.

            1 Reply Last reply
            0
            • G Gary Chapman

              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

              G Offline
              G Offline
              Gary R Wheeler
              wrote on last edited by
              #6

              The 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;

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups