Why only the constant expression can be used as case expression in Switch statement?

Posted by sinec on Stack Overflow See other posts from Stack Overflow or by sinec
Published on 2010-04-16T11:48:30Z Indexed on 2010/04/16 11:53 UTC
Read the original article Hit count: 225

Filed under:
|
|

Hi,

what is bothering me is that I can't found an info regarding the question from the title.

I found that assembly form the Switch-case statement is compiled into bunch of (MS VC 2008 compiler) cmp and je calls:

0041250C  mov         eax,dword ptr [i] 
0041250F  mov         dword ptr [ebp-100h],eax 
00412515  cmp         dword ptr [ebp-100h],1 
0041251C  je          wmain+52h (412532h) 
0041251E  cmp         dword ptr [ebp-100h],2 
00412525  je          wmain+5Bh (41253Bh) 
00412527  cmp         dword ptr [ebp-100h],3 
0041252E  je          wmain+64h (412544h) 
00412530  jmp         wmain+6Bh (41254Bh) 

where for above code will in case that if condition (i) is equal to 2, jump to address 41253Bh and do execution form there (at 41253Bh starts the code for 'case 2:' block)

What I don't understand is why in case that,for instance, function is used as 'case expression', why first function couldn't be evaluated and then its result compared with the condition?

Am I missing something?

Thank you in advance

© Stack Overflow or respective owner

Related posts about c++

Related posts about compiler