C++0x Enumeration question

Discussion in 'C++' started by rcdailey@gmail.com, Jun 24, 2008.

  1. Guest

    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.
    };
    , Jun 24, 2008
    #1
    1. Advertising

  2. Guest

    On Jun 24, 6:35 pm, Sam <> wrote:
    > 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_FOOS> foo_bitset;
    , Jun 25, 2008
    #2
    1. Advertising

  3. James Kanze Guest

    On Jun 25, 12:46 am, "" <> 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.
    > };


    No. The real problem is that C++ doesn't have "enumerations",
    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_limits (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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 25, 2008
    #3
  4. Noah Roberts Guest

    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).
    Noah Roberts, Jun 25, 2008
    #4
  5. 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
    Michael Oswald, Jun 25, 2008
    #5
  6. 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?
    Juha Nieminen, Jun 26, 2008
    #6
  7. Jerry Coffin Guest

    In article <vPJ8k.46$>, lid
    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.
    Jerry Coffin, Jun 26, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timothy Parez
    Replies:
    3
    Views:
    3,764
    WhatIThink
    Jun 29, 2010
  2. Jan Kindt
    Replies:
    5
    Views:
    2,031
    Allan Herriman
    Sep 12, 2003
  3. Replies:
    15
    Views:
    1,310
    Jerry Coffin
    Feb 1, 2005
  4. Justin Wright

    XML Schema enumeration question

    Justin Wright, Jan 23, 2004, in forum: XML
    Replies:
    1
    Views:
    1,914
    Jens M. Felderhoff
    Jan 24, 2004
  5. puvit82
    Replies:
    4
    Views:
    758
    puvit82
    Feb 1, 2008
Loading...

Share This Page