Click to See Complete Forum and Search --> : case:__LINE__ problem


khaldoun KASSEM
July 9th, 2002, 09:14 AM
Hi


I have the folowing in my code:
#define crReturn(z) \
do {\
crLine=__LINE__; return (z); case __LINE__:;\
} while (0)

and when I call crReturn(z) in my program I get an error:
D:\SSH.C(707) : error C2051: case expression not constant

how can I solve this problem.
i use visual C++ 6.0
thank you

zdf
July 9th, 2002, 09:55 AM
Strange code!

The syntax for case statements is:
case constant-expression: statement

__LINE__ should be a decimal integer constant.

Therefore the code should be almost ok (you’ll get warnings, I reckon). It must be a compiler bug.

Could you give an example of how you intend to use it?

Regards,

cup
July 9th, 2002, 10:07 AM
It looks like some debugging code for tracing line numbers.

1) case is only allowed in switch statements. Since there isn't a switch statement surrounding the case, it is not valid code

2) The code after return cannot be reached.

3) The do-while loop is not necessary either. This style of the do-while loop is normally used by coders who don't like else if. They always seem extremely chuffed that they have 'discovered' this. Something I never really understood. They code

if (a)
...
else if (b)
....
else if (c)
....
else
....

as

do {
if (a) { ... ; break; }
if (b) { ... ; break; }
if (c) { ... ; break; }
...
} while (0);

cup
July 9th, 2002, 10:23 AM
Just got this from the VC++ forum

#define crBegin1 static int crLine = 0;
#define crBegin2 switch(crLine) { case 0:;
#define crBegin crBegin1; crBegin2;
#define crFinish(z) } crLine = 0; return (z)
#define crFinishV } crLine = 0; return
#define crReturn(z) \
do {\
crLine=__LINE__; return (z); case __LINE__:;\
} while (0)
#define crReturnV \
do {\
crLine=__LINE__; return; case __LINE__:;\
} while (0)

and in the main() he do:

crBegin;

next_packet:

pktin.type = 0;
pktin.length = 0;

for (st->i = st->len = 0; st->i < 4; st->i++) {
while ((*datalen) == 0)
crReturn(4 - st->i);


My first comment is that coding like this in macros is never a good idea. Why the code is written to switch on the current line number is beyond me. Anyway, it expands to

static int crLine = 0;
switch(crLine)
{
case 0:;

next_packet:

pktin.type = 0;
pktin.length = 0;

for (st->i = st->len = 0; st->i < 4; st->i++)
{
while ((*datalen) == 0)
do {
crLine=__LINE__;
return (4-st->i);
case __LINE__:;
} while (0);


I think it is incomplete and there are lots of unreachable sections. Are you sure you are supposed to use this?

zdf
July 9th, 2002, 10:25 AM
Following is the code I have tested on VC++. Unfortunately it is the only compiler I have so I cannot crosscheck.


int main(int argc, char* argv[])
{
int n = 1;
switch( n )
{
case __LINE__:
return 1;
default:
return 2;
}
}


Regards,

Graham
July 9th, 2002, 10:25 AM
Anyone that comes up with code like that has much more serious problems than a "case expression is not constant" error.

khaldoun KASSEM
July 9th, 2002, 10:27 AM
thank to all the code works very well

khaldoun

zdf
July 9th, 2002, 10:38 AM
Originally posted by khaldoun KASSEM

thank you because you tried to help.
I solved the problem, it was a bug in visual C++, I just changed the compiler setting from "Program Database for Edit and Continue' to "Program Database" and the code works very well

thank you again

khaldoun

Thanks, I've learned something new.

cbarnes
November 26th, 2007, 02:59 PM
In case your wondering what that code does - it does co-routines:

http://www.linuxhowtos.org/C_C++/coroutines.htm

Craig

rachmann
August 15th, 2008, 09:03 AM
Your code?

Your code?

This code snippet is a copy right out of PuTTy...

Shame on you.

JohnW@Wessex
August 15th, 2008, 09:20 AM
In case your wondering what that code does - it does co-routines:I read the article. All I can say is, if I saw code like that here I would have to nip round and borrow Graham's rubber chicken and engage in a little 'coder refactoring'!