> PS: now that I think of it, you can't do in in C++ either.  The
declaration
> would be there in one case but not the others so what would the compiler
do
> after the switch?

fwiw, that's either definitely supported, or it's compiler-specific, but i'm
not sure which (probably the latter).  one of my coworkers did exactly that
a few years ago, and we had a big discussion about it and looked at the
assembly code that was generated, etc, etc.  the compiler got it correct.
we've shipped a couple versions of our product with that construction, and
it's survived thru at least 3 versions of the compiler we use.  i don't know
if GCC or CW can handle it; i haven't tried.  (personally i wouldn't use
this construction).

regarding the "what would the compiler do after the switch?", it's no
different than declaring a variable in the initialization expression of a
"for" statement.  (i'll avoid the religious debate as to whether the
lifetime of the variable should extend to the end of the scope outside the
"for/switch" statement, or end with the scope of the "for/switch").  the
tricky part is, what if you "goto" around it?  see below.  i think the
compiler can detect this the same as if it were normal linear code without
any switch/case statements.

switch (foo)
    {
    case 1:
        goto LSkip;
    case 2:
        int x;
LSkip:
        printf("%d", x);
        break;
    }


Reply via email to