Enums

Discussion in 'C++' started by Jon Slaughter, Sep 26, 2007.

  1. I'm declaring an enum and I would like to limit the scope

    I know that

    enum day2 {sun, mon, };

    is equivalent to

    static const int sun = 0;
    static const int mon = 1;

    but I'd like not to have these global.

    I can do day2::sun and use that but I get a warning

    about nonstandard extension. It does work though.

    I suppose I can wrap it in a static class and use it... maybe something like

    static struct day2
    {
    static const int sun = 0;
    static const int mon = 1;
    }


    Which seems to work but I get errors about no variables in day2. I suppose I
    can fix this in some way but I wondering if there is some other better
    method. (using the static constant int every time is a little trouble if its
    not necessary)

    Any ideas?

    Thanks,
    Jon
    Jon Slaughter, Sep 26, 2007
    #1
    1. Advertising

  2. Actualy what I'm doing is

    class X
    {
    enum Y
    {
    };
    };

    and it seems to work fine.
    Jon Slaughter, Sep 26, 2007
    #2
    1. Advertising

  3. * Jon Slaughter:
    > I'm declaring an enum and I would like to limit the scope
    >
    > I know that
    >
    > enum day2 {sun, mon, };
    >
    > is equivalent to
    >
    > static const int sun = 0;
    > static const int mon = 1;


    Well, it isn't. The enum is a distinct type. And its underlying
    integral type need not be int.


    > but I'd like not to have these global.
    >
    > I can do day2::sun and use that but I get a warning
    > about nonstandard extension. It does work though.


    Right, that's non-standard.


    > I suppose I can wrap it in a static class


    There's no such thing as static class.


    > and use it... maybe something like
    >
    > static struct day2
    > {
    > static const int sun = 0;
    > static const int mon = 1;
    > }


    Missing variable name and missing semicolon. By writing static you
    force the compiler to assume that you're declaring a variable. It's OK
    to define the struct inline there, but you then need a variable name at
    the end, and anyway you need a semicolon.


    > Which seems to work but I get errors about no variables in day2.


    How can it seem to work when it doesn't compile?


    > I suppose I
    > can fix this in some way but I wondering if there is some other better
    > method. (using the static constant int every time is a little trouble if its
    > not necessary)
    >
    > Any ideas?


    You can wrap the enum in a class or in a namespace.

    A class has the advantage that it can be inherited.

    A namespace has the advantage that you can use "using".


    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Sep 26, 2007
    #3
  4. Jon Slaughter

    Ian Collins Guest

    Jon Slaughter wrote:
    > I'm declaring an enum and I would like to limit the scope
    >
    > I know that
    >
    > enum day2 {sun, mon, };
    >

    Drop that last comma.

    >
    > but I'd like not to have these global.
    >
    > I can do day2::sun and use that but I get a warning
    >

    You could put the enum in a namespace.

    namespace day2
    {
    enum { sun, mon };
    }

    --
    Ian Collins.
    Ian Collins, Sep 26, 2007
    #4
  5. "Alf P. Steinbach" <> wrote in message
    news:...
    >* Jon Slaughter:
    >> I'm declaring an enum and I would like to limit the scope
    >>
    >> I know that
    >>
    >> enum day2 {sun, mon, };
    >>
    >> is equivalent to
    >>
    >> static const int sun = 0;
    >> static const int mon = 1;

    >
    > Well, it isn't. The enum is a distinct type. And its underlying integral
    > type need not be int.
    >
    >


    This is what microsoft said, not me.

    >> but I'd like not to have these global.
    >>
    >> I can do day2::sun and use that but I get a warning
    >> about nonstandard extension. It does work though.

    >
    > Right, that's non-standard.
    >
    >
    >> I suppose I can wrap it in a static class

    >
    > There's no such thing as static class.
    >
    >
    >> and use it... maybe something like
    >>
    >> static struct day2
    >> {
    >> static const int sun = 0;
    >> static const int mon = 1;
    >> }

    >
    > Missing variable name and missing semicolon. By writing static you force
    > the compiler to assume that you're declaring a variable. It's OK to
    > define the struct inline there, but you then need a variable name at the
    > end, and anyway you need a semicolon.
    >


    I left the semicolon off on purpose just to bother you. jesus christ man!

    >
    >> Which seems to work but I get errors about no variables in day2.

    >
    > How can it seem to work when it doesn't compile?
    >
    >
    >> I suppose I can fix this in some way but I wondering if there is some
    >> other better method. (using the static constant int every time is a
    >> little trouble if its not necessary)
    >>
    >> Any ideas?

    >
    > You can wrap the enum in a class or in a namespace.
    >
    > A class has the advantage that it can be inherited.
    >
    > A namespace has the advantage that you can use "using".
    >
    >


    I suppose a namespace is a good idea. I used a class already though since
    that worked for my purposes. I suppose the usage syntax is identical though.

    Thanks,
    Jon
    Jon Slaughter, Sep 26, 2007
    #5
  6. "Ian Collins" <> wrote in message
    news:...
    > Jon Slaughter wrote:
    >> I'm declaring an enum and I would like to limit the scope
    >>
    >> I know that
    >>
    >> enum day2 {sun, mon, };
    >>

    > Drop that last comma.


    god damn people!! First off I cut and pasted this directly from microsofts
    help so bitch at them about irrelevant(for my problem) stuff.

    http://msdn2.microsoft.com/en-us/library/a6cskb49(VS.80).aspx


    >
    >>
    >> but I'd like not to have these global.
    >>
    >> I can do day2::sun and use that but I get a warning
    >>

    > You could put the enum in a namespace.
    >
    > namespace day2
    > {
    > enum { sun, mon };
    > }


    Yes, that is a better method I suppose. I wrapped it with a class but I
    guess I'll change it to a namescape since it seems better(even though its
    probably equivalent).

    Thanks,
    Jon
    Jon Slaughter, Sep 26, 2007
    #6
  7. Jon Slaughter

    Pete Becker Guest

    On 2007-09-26 16:39:37 -0400, Ian Collins <> said:

    > Jon Slaughter wrote:
    >> I'm declaring an enum and I would like to limit the scope
    >>
    >> I know that
    >>
    >> enum day2 {sun, mon, };
    >>

    > Drop that last comma.
    >


    The comma is allowed, and sometimes useful.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
    Pete Becker, Sep 27, 2007
    #7
  8. Jon Slaughter

    Ian Collins Guest

    Jon Slaughter wrote:
    > "Ian Collins" <> wrote in message
    > news:...
    >> Jon Slaughter wrote:
    >>> I'm declaring an enum and I would like to limit the scope
    >>>
    >>> I know that
    >>>
    >>> enum day2 {sun, mon, };
    >>>

    >> Drop that last comma.

    >
    > god damn people!! First off I cut and pasted this directly from microsofts
    > help so bitch at them about irrelevant(for my problem) stuff.
    >

    If you post something that appears incorrect, some one will point his out.

    --
    Ian Collins.
    Ian Collins, Sep 27, 2007
    #8
  9. Jon Slaughter

    Ian Collins Guest

    Pete Becker wrote:
    > On 2007-09-26 16:39:37 -0400, Ian Collins <> said:
    >
    >> Jon Slaughter wrote:
    >>> I'm declaring an enum and I would like to limit the scope
    >>>
    >>> I know that
    >>>
    >>> enum day2 {sun, mon, };
    >>>

    >> Drop that last comma.
    >>

    >
    > The comma is allowed, and sometimes useful.
    >

    I didn't think it was, from 7.2 I thought an enumerator-list couldn't
    end in a comma, so I just tried to compile the OP's code;

    Sun CC says:

    Warning: Identifier expected instead of "}".

    gcc says:

    error: comma at end of enumerator list

    Which sums up my interpretation of 7.2.

    --
    Ian Collins.
    Ian Collins, Sep 27, 2007
    #9
  10. "Pete Becker" <> wrote in message
    news:2007092621345916807-pete@versatilecodingcom...
    > On 2007-09-26 16:39:37 -0400, Ian Collins <> said:
    >
    >> Jon Slaughter wrote:
    >>> I'm declaring an enum and I would like to limit the scope
    >>>
    >>> I know that
    >>>
    >>> enum day2 {sun, mon, };
    >>>

    >> Drop that last comma.
    >>

    >
    > The comma is allowed, and sometimes useful.


    The extra comma is allowed in C99:

    http://groups.google.com/group/comp.lang.c/browse_frm/thread/57f41c6dee5f8e52

    no?
    Chris Thomasson, Sep 27, 2007
    #10
  11. "Ian Collins" <> wrote in message
    news:...
    > Jon Slaughter wrote:
    >> "Ian Collins" <> wrote in message
    >> news:...
    >>> Jon Slaughter wrote:
    >>>> I'm declaring an enum and I would like to limit the scope
    >>>>
    >>>> I know that
    >>>>
    >>>> enum day2 {sun, mon, };
    >>>>
    >>> Drop that last comma.

    >>
    >> god damn people!! First off I cut and pasted this directly from
    >> microsofts
    >> help so bitch at them about irrelevant(for my problem) stuff.
    >>

    > If you post something that appears incorrect, some one will point his out.
    >


    First off is not necessarily an error.

    Second, my question wasn't about a syntax error and the problem has nothing
    to do with it. Now if it was a serious issue then I could see the need but
    else its just pedantism. I'm sure you have better things to do with your
    time than waste it writing such irrelevant things? (of course the 2nd half
    of your post was very helpful)

    Suppose it was an syntax error. Do you really think it has anything to do
    with my original problem? And if it did could it not be easily caught when
    the example was tried? This is not some subtle issue that needs
    addressing... I know you know that but when you post such things it makes
    you seem like you are simple minded are a pedant. Of course maybe you are a
    pedant and proud of it? ;)
    Jon Slaughter, Sep 27, 2007
    #11
  12. Jon Slaughter

    Ian Collins Guest

    Jon Slaughter wrote:

    > Of course maybe you are a pedant and proud of it? ;)
    >

    I was attempting to help answer your question (which I did). I won't
    make that mistake again.

    --
    Ian Collins.
    Ian Collins, Sep 27, 2007
    #12
  13. Jon Slaughter

    Ian Collins Guest

    Chris Thomasson wrote:
    > "Pete Becker" <> wrote in message
    > news:2007092621345916807-pete@versatilecodingcom...
    >> On 2007-09-26 16:39:37 -0400, Ian Collins <> said:
    >>
    >>> Jon Slaughter wrote:
    >>>> I'm declaring an enum and I would like to limit the scope
    >>>>
    >>>> I know that
    >>>>
    >>>> enum day2 {sun, mon, };
    >>>>
    >>> Drop that last comma.
    >>>

    >>
    >> The comma is allowed, and sometimes useful.

    >
    > The extra comma is allowed in C99:
    >

    Where it is listed as a major change from C95, so probably a change from
    C++. I assume the change will carry forward to the next C++ standard?

    --
    Ian Collins.
    Ian Collins, Sep 27, 2007
    #13
  14. Chris Thomasson, Sep 27, 2007
    #14
  15. Jon Slaughter

    Ian Collins Guest

    Chris Thomasson wrote:
    > "Jon Slaughter" <> wrote in message
    > news:%kzKi.1306$...
    >> I'm declaring an enum and I would like to limit the scope
    >>

    > [...]
    >
    > There is a possible solution in the form of a macro hack:
    >
    > http://appcore.home.comcast.net/misc/enum-scope-cpp.html
    >

    That's better than using a namespace?

    --
    Ian Collins.
    Ian Collins, Sep 27, 2007
    #15
  16. Jon Slaughter

    James Kanze Guest

    On Sep 27, 3:34 am, Pete Becker <> wrote:
    > On 2007-09-26 16:39:37 -0400, Ian Collins <> said:


    > > Jon Slaughter wrote:
    > >> I'm declaring an enum and I would like to limit the scope


    > >> I know that


    > >> enum day2 {sun, mon, };


    > > Drop that last comma.


    > The comma is allowed, and sometimes useful.


    Not according to ISO 14882:1998. Nor was it allowed in C90. It
    is allowed in C99, and in the current draft. I don't know what
    the status is in ISO 14882:2003, but it really doesn't matter,
    since most compilers haven't even fully implemented C++98 yet.
    G++ doesn't allow it, for example (and only allows -std=c++98,
    not -std=c++03).

    Of course, the fact that C90 didn't allow it was probably an
    oversight (copied by C++98); I suspect that many compiler
    writers considered it as such, and didn't forbid it. But as
    long as some do, you're stuck with dropping it, at least in
    portable code.

    --
    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, Sep 27, 2007
    #16
  17. "Ian Collins" <> wrote in message
    news:...
    > Chris Thomasson wrote:
    >> "Jon Slaughter" <> wrote in message
    >> news:%kzKi.1306$...
    >>> I'm declaring an enum and I would like to limit the scope
    >>>

    >> [...]
    >>
    >> There is a possible solution in the form of a macro hack:
    >>
    >> http://appcore.home.comcast.net/misc/enum-scope-cpp.html
    >>

    > That's better than using a namespace?


    Well, not really; those macros were kind of on the hacky/crazy side...

    ;^)


    Anyway, I think I agree with the "apparent" line of *reasoning I deciphered
    from your post. Basically, use the namespace keyword to confine an enum
    within its scope. Unfortunately, there could be a problem with this approach
    because you can't use a namespace within the scope of a struct/class:

    ____________________

    class/struct {
    namespace xxx {} // syntax error.
    class/struct yyy {} // okay.
    };
    ____________________


    Right? I am not an "expert" in the C++ Standard. Therefore, I am probably
    missing something real simple here...




    *-- Here is some quick example code that tries to fit the bill:

    ____________________

    namespace foolib {
    namespace constant {
    namespace config {
    enum value_e {
    value_a,
    value_b
    };
    }} // namespace constant::config


    struct foo {
    #undef MYCONST
    #define MYCONST foolib::constant
    struct constant {
    struct state {
    enum value_e {
    value_a = MYCONST::config::value_a + 1,
    value_b = MYCONST::config::value_b + 1
    };
    };}; // struct constant::state
    }; // class/struct foo


    namespace constant {
    namespace state {
    #undef MYCONST
    #define MYCONST foolib::foo::constant
    enum value_e {
    value_a = MYCONST::state::value_a - 1,
    value_b = MYCONST::state::value_b - 1
    };
    }} // namespace constant::state

    } // namespace foolib



    #include <assert.h>
    #include <stdio.h>

    int main(void) {
    namespace myconst = foolib::constant;

    assert(
    (myconst::config::value_a ==
    myconst::state::value_a) &&

    (myconst::config::value_b ==
    myconst::state::value_b)
    );

    using namespace std;

    puts("done; hit <enter>");
    getchar();

    return 0;
    }

    ____________________


    Something like that looks better than the contrived macro-based stuff no?
    Chris Thomasson, Sep 27, 2007
    #17
  18. "Ian Collins" <> wrote in message
    news:...
    > Jon Slaughter wrote:
    >
    >> Of course maybe you are a pedant and proud of it? ;)
    >>

    > I was attempting to help answer your question (which I did). I won't
    > make that mistake again.
    >


    No problem.. Do what you wish.
    Jon Slaughter, Sep 27, 2007
    #18
    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. Syd

    Loop through enums

    Syd, Aug 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    3,026
    John Saunders
    Aug 28, 2003
  2. martin

    enums

    martin, May 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    603
    Brian W
    May 28, 2004
  3. Hans De Schrijver

    Basic question about enums

    Hans De Schrijver, Jun 5, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    341
    Peter Rilling
    Jun 5, 2004
  4. Matt

    Storing Enums in web.config

    Matt, Jun 7, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,595
  5. =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=

    Enums without identifier, enums and typedef

    =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=, Jan 19, 2007, in forum: C Programming
    Replies:
    10
    Views:
    1,097
    Keith Thompson
    Jan 20, 2007
Loading...

Share This Page