Discussion: why Its a Valid Code
Afficher un message
Vieux 03/01/2008, 14h24   #6
Joe Greer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: why Its a Valid Code

James Kanze <james.kanze@gmail.com> wrote in news:2c634276-e17c-470a-924d-
8c8684f4f75c@i29g2000prf.googlegroups.com:

>
> The code outside the switch is completely wrong, of course, but
> the switch itself is a very literal copy of Duff's device (where
> to pointed to what would today be a char volatile*). There are
> lots of possible variants, of course. The whole point of the
> exercise is that switch, in C++, really is a calculated goto
> with special labels. And although this example doesn't show it,
> there's also no requirement that the controled statement in a
> switch be a compound statement. Something like:
>
> switch ( count % 8 )
> while ( count > 0 ) {
> case 0 :
> *to ++ = *from ++ ;
> case 7 :
> *to ++ = *from ++ ;
> case 6 :
> *to ++ = *from ++ ;
> case 5 :
> *to ++ = *from ++ ;
> case 4 :
> *to ++ = *from ++ ;
> case 3 :
> *to ++ = *from ++ ;
> case 2 :
> *to ++ = *from ++ ;
> case 1 :
> *to ++ = *from ++ ;
> count -= 8 ;
> }
>
> would also be perfectly legal.
>
> Another variant I've seen:
>
> switch ( *p ) {
> // ...
> case '.' :
> if ( isdigit( *(p + 1) ) ) {
> case '0' : case '1' : case '2' : case '3' :
> case '4' : case '5' : case '6' : case '7' :
> case '8' : case '9' :
> scanFloatingPointConstant( p ) ;
> } else {
> case '~' : case '!' : case '%' : case '^' :
> // ...
> scanPunctuation( p ) ;
> }
> break ;
> // ...
> }
>
> There's no rule that all of the case labels have to be at the
> same level of nesting either.
>


I would never say there is no use for such code, but I would want to be very sure that any code like
that was truly required. You can easily run into traps where the code is actually slower because you
are actually doing more work than just a straight forward calculation. Not to mention that any poor
maintenance programmer who had to try to fix it would have a few hair pulling sessions before he
figured it out (in my experience any way). Anyway, I was responding to the specific code, not the
idea.

joe


  Réponse avec citation
 
Page generated in 0,06928 seconds with 9 queries