C++0x Enumeration question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rcdailey@gmail.com

    C++0x Enumeration question

    Will there be a feature in C++0x to facilitate getting the number of
    items in an enumeration? For example, in C++03, you have to do
    something like this:

    enum Foo
    {
    FOO1 = 0,
    FOO2,
    FOO3,
    NUM_FOOS // this will be 3, and we use this to determine how many
    enums we have.
    };
  • Sam

    #2
    Re: C++0x Enumeration question

    rcdailey@gmail. com writes:
    Will there be a feature in C++0x to facilitate getting the number of
    items in an enumeration? For example, in C++03, you have to do
    something like this:
    >
    enum Foo
    {
    FOO1 = 0,
    FOO2,
    FOO3,
    NUM_FOOS // this will be 3, and we use this to determine how many
    enums we have.
    };
    Given that enumerated values do not have to be continuous (or even unique),
    what benefit do you see in knowing the number of enumerated values?



    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)

    iEYEABECAAYFAkh hhLcACgkQx9p3GY HlUOJeNQCfYYtxy 8Cl4k+et9X5UKHF 2GI+
    MnQAnRi51EuVgVZ 8FL+mfs3rfmbx0h TH
    =wkkl
    -----END PGP SIGNATURE-----

    Comment

    • rcdailey@gmail.com

      #3
      Re: C++0x Enumeration question

      On Jun 24, 6:35 pm, Sam <s...@email-scan.comwrote:
      rcdai...@gmail. com writes:
      Will there be a feature in C++0x to facilitate getting the number of
      items in an enumeration? For example, in C++03, you have to do
      something like this:
      >
      enum Foo
      {
          FOO1 = 0,
          FOO2,
          FOO3,
          NUM_FOOS // this will be 3, and we use this to determine how many
      enums we have.
      };
      >
      Given that enumerated values do not have to be continuous (or even unique),
      what benefit do you see in knowing the number of enumerated values?
      >
       application_pgp-signature_part
      1KDownload
      They're useful currently in C++03 in std::bitset objects:

      std::bitset<NUM _FOOSfoo_bitset ;

      Comment

      • Sam

        #4
        Re: C++0x Enumeration question

        rcdailey@gmail. com writes:
        On Jun 24, 6:35 pm, Sam <s...@email-scan.comwrote:
        >rcdai...@gmail .com writes:
        Will there be a feature in C++0x to facilitate getting the number of
        items in an enumeration? For example, in C++03, you have to do
        something like this:
        >>
        enum Foo
        {
            FOO1 = 0,
            FOO2,
            FOO3,
            NUM_FOOS // this will be 3, and we use this to determine how many
        enums we have.
        };
        >>
        >Given that enumerated values do not have to be continuous (or even unique),
        >what benefit do you see in knowing the number of enumerated values?
        >>
        > application_pgp-signature_part
        >1KDownload
        They're useful currently in C++03 in std::bitset objects:

        std::bitset<NUM _FOOSfoo_bitset ;
        Except that -- as I pointed out -- this will fail miserably if the enum is
        noncontinuous:

        enum Foo {
        FOO=0,
        BAR=100
        }

        std::bitset<siz eof(Foo)foo_bit set;

        sizeof(Foo) is two, since the num contains two values. Boom.

        Or, if the enum contains duplicate values, things will even be more
        exciting.



        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.4.9 (GNU/Linux)

        iEYEABECAAYFAkh hlBUACgkQx9p3GY HlUOL09ACeNuUbH JrtJNXPeyjzTulM RCRi
        0sYAn3kCKT+tIbf GoSWh3+bLLnGYbK cq
        =Rh2Y
        -----END PGP SIGNATURE-----

        Comment

        • James Kanze

          #5
          Re: C++0x Enumeration question

          On Jun 25, 12:46 am, "rcdai...@gmail .com" <rcdai...@gmail .comwrote:
          Will there be a feature in C++0x to facilitate getting the
          number of items in an enumeration? For example, in C++03, you
          have to do something like this:
          enum Foo
          {
          FOO1 = 0,
          FOO2,
          FOO3,
          NUM_FOOS // this will be 3, and we use this to determine how many
          enums we have.
          };
          No. The real problem is that C++ doesn't have "enumeratio ns",
          in the classical sense of the word. It has a facility (using
          the keyword enum) for creating new integral types and symbolic
          constants for some (but not necessarily all) of the values of
          those types. Thus, for example, what should such a facility
          say to something like:

          enum _Ios_Fmtflags
          {
          _S_boolalpha = 1L << 0,
          _S_dec = 1L << 1,
          _S_fixed = 1L << 2,
          _S_hex = 1L << 3,
          _S_internal = 1L << 4,
          _S_left = 1L << 5,
          _S_oct = 1L << 6,
          _S_right = 1L << 7,
          _S_scientific = 1L << 8,
          _S_showbase = 1L << 9,
          _S_showpoint = 1L << 10,
          _S_showpos = 1L << 11,
          _S_skipws = 1L << 12,
          _S_unitbuf = 1L << 13,
          _S_uppercase = 1L << 14,
          _S_adjustfield = _S_left | _S_right | _S_internal,
          _S_basefield = _S_dec | _S_oct | _S_hex,
          _S_floatfield = _S_scientific | _S_fixed,
          _S_ios_fmtflags _end = 1L << 16
          };

          (This is actual code from the g++ standard library, which
          explains the somewhat strange naming convensions.)

          I have a small utility program which I use which will generate
          various information about enums---it was originally developed to
          generate an enum name to value mapping, but has options for
          additional functionality. One of the options will generate
          increment and decrement operators for the enum, along with an
          specialization of std::numeric_li mits (whose member function
          max() is probably what you are looking for) and iterators
          accessing all of the enum values; this option will be rejected,
          however, if any of the values in the enum have an assigned
          value. (Similarly, the option to generate the | and & operators
          will be rejected unless all of the enum values are given
          explicitly.)

          --
          James Kanze (GABI Software) email:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          • Noah Roberts

            #6
            Re: C++0x Enumeration question

            rcdailey@gmail. com wrote:
            Will there be a feature in C++0x to facilitate getting the number of
            items in an enumeration? For example, in C++03, you have to do
            something like this:
            >
            enum Foo
            {
            FOO1 = 0,
            FOO2,
            FOO3,
            NUM_FOOS // this will be 3, and we use this to determine how many
            enums we have.
            };
            I know of nothing that will be changing that. I don't know everything
            that's going into the standard but I've been sort of following it with
            interest. What you're talking about is simply an easy and common way to
            do what you want to do. I wouldn't think it worth any language change
            and there are a lot of other, much more important things that might not
            make it in because of timing constraints (trying to get the std out this
            decade).

            Comment

            • Michael Oswald

              #7
              Re: C++0x Enumeration question

              rcdailey@gmail. com wrote:
              Will there be a feature in C++0x to facilitate getting the number of
              items in an enumeration? For example, in C++03, you have to do
              something like this:
              >
              enum Foo
              {
              FOO1 = 0,
              FOO2,
              FOO3,
              NUM_FOOS // this will be 3, and we use this to determine how many
              enums we have.
              };
              As others already said, an enum in C++ is not an enum like in Ada, where
              you can do such things very easily. Since you can set the enum values
              quite freely, this change would break compatibility with old code, so I
              think you cannot expect that something like this will be added.


              hth,
              Michael

              Comment

              • Juha Nieminen

                #8
                Re: C++0x Enumeration question

                Sam wrote:
                Given that enumerated values do not have to be continuous (or even
                unique), what benefit do you see in knowing the number of enumerated
                values?
                Btw, if an enumerated list is specified without any values (ie. like
                enum Foo { a, b, c, d };), is it guaranteed that the first value will be
                0 and the next ones will be consecutive positive integers?

                Comment

                • Jerry Coffin

                  #9
                  Re: C++0x Enumeration question

                  In article <vPJ8k.46$zr1.1 7@read4.inet.fi >, nospam@thanks.i nvalid
                  says...
                  Sam wrote:
                  Given that enumerated values do not have to be continuous (or even
                  unique), what benefit do you see in knowing the number of enumerated
                  values?
                  >
                  Btw, if an enumerated list is specified without any values (ie. like
                  enum Foo { a, b, c, d };), is it guaranteed that the first value will be
                  0 and the next ones will be consecutive positive integers?
                  Yes. ($7.2/1):

                  If the first enumerator has no initializer, the value
                  of the corresponding constant is zero. An enumerator-
                  definition without an initializer gives the enumerator
                  the value obtained by increasing the value of the
                  previous enumerator by one.

                  --
                  Later,
                  Jerry.

                  The universe is a figment of its own imagination.

                  Comment

                  Working...