 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Johan Johansson Guest
|
Posted: Sun Apr 17, 2005 5:59 pm Post subject: enum expressions in integral constant expressions |
|
|
Neither gcc (3.3.3) nor Metrowerks (don't remember version) seems to
think that "enumVal1 | enumVal2" is an integral constant expression.
I've been reading the internets and searching the standard, but I
haven't found anything conclusive. In particular I can't see that §5.19
disallows it. For that matter §5.19 doesn't seem to include any
operations performed at all, but that's hardly the intent.
In the case of operator| being overloaded the expression clearly isn't a
compile time constant, not to mention the function call being explicitly
disallowed by §5.19. Comeau online 4.3.3 *does* seem to swallow that
without complaint however (see below for exact code), which greatly
diminishes my expectations of being right and greatly adds to my
confusion. However, gcc and Metrowerks seem to ban this even if
operator| is not overloaded, which as far as I can see the compiler
would need to know to compile enumVal1 | enumVal2.
Here is the exact code swallowed by Comeau online but not the others. It
is not minimal because I wanted to make sure that I wasn't just seeing
dead code elimination:
enum E { value1 = 1, value2 = 2 };
E operator|( const E& x, const E& y );
int main()
{
int x = 3;
switch (x) {
case value1:
x = 4;
break;
case value1 | value2:
x = 5;
break;
}
}
Oh, it also works on "all" Microsoft C++ compilers upto and including VS
2005 December CTP.
Johan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Mon Apr 18, 2005 9:06 am Post subject: Re: enum expressions in integral constant expressions |
|
|
On 17 Apr 2005 13:59:53 -0400, Johan Johansson <johanj (AT) ipunplugged (DOT) com>
wrote:
| Quote: | Neither gcc (3.3.3) nor Metrowerks (don't remember version) seems to
think that "enumVal1 | enumVal2" is an integral constant expression.
I've been reading the internets and searching the standard, but I
haven't found anything conclusive. In particular I can't see that §5.19
disallows it. For that matter §5.19 doesn't seem to include any
operations performed at all, but that's hardly the intent.
|
You missed the definition of constant expression as conditional
expression which allows operators. It later disallows assignment,
increment, decrement, function-call, and comma.
| Quote: | In the case of operator| being overloaded the expression clearly isn't a
compile time constant, not to mention the function call being explicitly
disallowed by §5.19. Comeau online 4.3.3 *does* seem to swallow that
without complaint however (see below for exact code), which greatly
diminishes my expectations of being right and greatly adds to my
confusion.
|
It must be converting the enums to ints and using the built-in |. I
can't find anything which allows this. Overload resolution seems to
be context free and forces selection of the enum overload.
| Quote: | However, gcc and Metrowerks seem to ban this even if
operator| is not overloaded, which as far as I can see the compiler
would need to know to compile enumVal1 | enumVal2.
|
Gcc 3.2 rejects your code and accepts it with the overload removed.
That seems to be correct and in agreement with your logic. I wonder if
that is a bug introduced in 3.3 and removed later?
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Wed Apr 20, 2005 9:39 am Post subject: Re: enum expressions in integral constant expressions |
|
|
Johan Johansson wrote:
| Quote: | Neither gcc (3.3.3) nor Metrowerks (don't remember version) seems to
think that "enumVal1 | enumVal2" is an integral constant expression.
In the case of operator| being overloaded the expression clearly
isn't a
compile time constant,
enum E { value1 = 1, value2 = 2 };
E operator|( const E& x, const E& y );
int main()
{
int x = 3;
switch (x) {
case value1:
x = 4;
break;
case value1 | value2:
x = 5;
break;
}
}
Oh, it also works on "all" Microsoft C++ compilers upto and
including VS 2005 December CTP.
|
When you say that the code "works" on Microsoft -- you mean that the
compilers correctly give an error message, right? VC.Net 2003 gives
error C2051: Case expression not constant. The error goes away if
you comment out operator|(), as expected.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nicola Musatti Guest
|
Posted: Wed Apr 20, 2005 4:52 pm Post subject: Re: enum expressions in integral constant expressions |
|
|
Uh? Once you remove the overload declaration g++ 3.3.3 (cygwin special)
compiles your example without problems.
Cheers,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|