CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: using enum

  1. #1
    Join Date
    May 2004
    Posts
    75

    using enum

    Consider the following:
    Code:
    struct SColors
    {
    enum enColors { red, green,blue };
    };
    
    void Foo(SColors::enColor Color)
    {
      switch(Color)
      {
        case SColors::red: /*...*/ break;
        case SColors::green: /*...*/ break;
        case SColors::blue: /*...*/ break;
        default: assert(false); break;
      }
    }
    I would like to write something like this in the Foo function:
    Code:
    void Foo(SColors::enColor Color)
    {
      using SColors::enColors; // Want to "use" all color enumeration values!
    
      switch(Color)
      {
        case red: /*...*/ break;
        case green: /*...*/ break;
        case blue: /*...*/ break;
        default: assert(false); break;
      }
    }
    I know that I can apply "using" to each distinct enumeration value, but I'm too lazy to do that. So is there a way to apply "using" to all enumeration values with a single statement (although it may not be good practice)?

  2. #2
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: using enum

    Even "using SColors::red" is not-standard (and it does not work with Comeau).
    It is possible to do "using namespace NamespaceName", but it is not possible to do "using class ClassName". If it was possible we could do "using class SColors;".

    I suppose that there are no clean way to do what you want.
    Quote Originally Posted by Oliver M.
    I know that I can apply "using" to each distinct enumeration value, but I'm too lazy to do that. So is there a way to apply "using" to all enumeration values with a single statement (although it may not be good practice)?
    Maybe you are too lazy.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  3. #3
    Join Date
    May 2004
    Posts
    75

    Re: using enum

    Quote Originally Posted by SuperKoko
    I suppose that there are no clean way to do what you want.
    bummer
    Quote Originally Posted by SuperKoko
    Maybe you are too lazy.
    Maybe you're right

    Ok, I also felt it would increase code readability in some places, but well, then I have to use multiple using declarations (although they are non-standard).

    Thank you very much!

  4. #4
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: using enum

    Instead of a using declaration for red green and blue, you can declare a local constant like that.
    Code:
    void Foo(SColors::enColors Color)
    {
      const SColors::enColors red=SColors::red; // using SColors::red;
      const SColors::enColors green=SColors::green;
      const SColors::enColors blue=SColors::blue;
    
      switch(Color)
      {
        case red: /*...*/ break;
        case green: /*...*/ break;
        case blue: /*...*/ break;
        default: assert(false); break;
      }
    }
    That way, it is ISO compliant.
    But it needs even more typing.

    Maybe future ISO standard will allow to manipulate namespaces and classes in a more uniform way. But that is only speculation for now.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  5. #5
    Join Date
    May 2004
    Posts
    75

    Re: using enum

    Quote Originally Posted by SuperKoko
    Instead of a using declaration for red green and blue, you can declare a local constant like that.
    That way, it is ISO compliant.
    [...]
    But it needs even more typing.
    Aaahhrrgg! Even more typing! Aaahhrrgg! Just kidding - this is a very good idea and I think that a good optimizing compiler will not leave any overhead. Thank you!

  6. #6
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: using enum

    Quote Originally Posted by Oliver M.
    Aaahhrrgg! Even more typing! Aaahhrrgg! Just kidding - this is a very good idea and I think that a good optimizing compiler will not leave any overhead. Thank you!
    For the overhead, even if no compiler optimization can be assumed, we can be quite sure that it will produces the same code, because the value of a case statement must be constant, so we are sure that the compiler really use these constants as constants.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  7. #7
    Join Date
    May 2004
    Posts
    75

    Re: using enum

    The switch statement was just an example; the real code is much more complicated. Anyway, it's my decision whether to write optimized or readable code in the first place, and I should strive for readability in this case.

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured