|
-
November 4th, 2005, 06:49 AM
#1
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)?
-
November 4th, 2005, 06:58 AM
#2
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.
 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()!
-
November 4th, 2005, 07:10 AM
#3
Re: using enum
 Originally Posted by SuperKoko
I suppose that there are no clean way to do what you want.
bummer 
 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!
-
November 4th, 2005, 07:25 AM
#4
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()!
-
November 4th, 2005, 07:55 AM
#5
Re: using enum
 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!
-
November 4th, 2005, 11:09 AM
#6
Re: using enum
 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()!
-
November 7th, 2005, 02:37 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|