David Webber
2011-02-20 17:16:56 UTC
I wonder if I'm breaking any rules here?
Consider
enum MYTYPE { ZERO=0, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN };
The names have been changed to protect the innocent, but I like enums like
this as the names are shown in the debugger, and if they are meaningful it
means I don't have to grope around in the recesses of my memory to find what
6 represents.
But one thing which doesn't automatically work is
MYTYPE a=ONE; b=TWO;
MYTYPE c = a|b;
But it appears to be fine if I include in my header file
inline MYTYPE operator | (MYTYPE a, MYTYPE b )
{
return (MYTYPE)( ((UINT)a) | ((UINT)b) );
}
Is it legal to define such operators on enums? - So far I haven't found it
in the language spec anywhere but it seems to compile fine.
And while we're at it, is the following syntax ok?
inline MYTYPE operator |= (MYTYPE a, MYTYPE b )
{
a = (MYTYPE)( ((UINT)a) | ((UINT)b) );
return a;
}
[I usually do such things with class members, but obviously that's not
appropriate here, and I'm blowed if I can remember the syntax (or indeed
find it).]
Dave
-- David Webber
Mozart Music Software
http://www.mozart.co.uk
For discussion and support see
http://www.mozart.co.uk/mozartists/mailinglist.htm
Consider
enum MYTYPE { ZERO=0, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN };
The names have been changed to protect the innocent, but I like enums like
this as the names are shown in the debugger, and if they are meaningful it
means I don't have to grope around in the recesses of my memory to find what
6 represents.
But one thing which doesn't automatically work is
MYTYPE a=ONE; b=TWO;
MYTYPE c = a|b;
But it appears to be fine if I include in my header file
inline MYTYPE operator | (MYTYPE a, MYTYPE b )
{
return (MYTYPE)( ((UINT)a) | ((UINT)b) );
}
Is it legal to define such operators on enums? - So far I haven't found it
in the language spec anywhere but it seems to compile fine.
And while we're at it, is the following syntax ok?
inline MYTYPE operator |= (MYTYPE a, MYTYPE b )
{
a = (MYTYPE)( ((UINT)a) | ((UINT)b) );
return a;
}
[I usually do such things with class members, but obviously that's not
appropriate here, and I'm blowed if I can remember the syntax (or indeed
find it).]
Dave
-- David Webber
Mozart Music Software
http://www.mozart.co.uk
For discussion and support see
http://www.mozart.co.uk/mozartists/mailinglist.htm