Bartc wrote:
"Eric Sosman" <es*****@ieee-dot-org.invalidwrot e in message
news:r6******** *************** *******@comcast .com...
>Bartc wrote:
>>There's no reason why switch expressions can't be any (identical) type,
as Keith has suggested.
If they are all constant integers, then fine generate a jump table.
Otherwise generate appropriate if-statements. This is very easy at a time
when C compilers are so super-sophisticated they can almost bake bread
too.
But the people responsible for new C standards have decided to keep this
area of the language (control statements in general) at a primitive
level.
So the programmer has to code his logic in a less expressive manner.
If you switch on a double, with double case labels,
what's the matching criterion?
...
> If you switch on a struct-valued expression, what's
the matching criterion?
...
> If you switch on a `char*', should the matching
criterion be strcmp()?
Yes I know there's all sorts of obscure examples where such a switch would
be ambiguous, but in that case no advance would ever be made.
The question is whether the "advance" produces something
useful, and the situations I mentioned seem to me like real-
life problems the enhanced switch would have trouble with.
Probably all the OP wants is to be have variable int cases, then an
extension of switch which simply applies the == operator would be a useful
advance.
The O.P. in this case asked a very short question, from
which you seem to derive a lot more information than I can.
Besides, I'm still not convinced of the utility, even
if restricted to int values. Try this:
int x = 42, y = 43;
for (int i = 40; i < 50; ++i) {
switch (i) {
case x: printf("x\n"); break;
case y: printf("y\n"); break;
default: printf("?\n"); break;
}
y = 42;
}
Worse still, try this:
int c1, c2;
c1 = getchar();
switch (c1) {
case EOF: printf("done!\n "); return;
case c2 = getchar():
printf("two %c's in a row\n", c1);
break;
default:
ungetc(c2); break;
}
Question: Is the "internal" getchar() executed if c1==EOF?
Another simple enhancement is a range operator (maybe gcc has this?) like
case 'A'..'Z':, which in the case of doubles could translate into >= and <=.
Yes, gcc has an extension like this. And yes, it's less
useful than one might think. For example, the case you show
does necessarily select all upper-case letters, nor does it
necessarily exclude all non-letters. What good is it?
Case ranges might be handy in some other situations, but
it seems suggestive that the example always offered for them
is the one you've shown, where ranges are clearly *not* the
right thing to use.
And with char* strings, the compiler can apply some common sense and use
strcmp(). There are ways.
... so the enhanced switch is limited to just one notion
of matching. True, it's probably the most "natural" notion
for strings, but it's not the only notion. Also, what do
you get if the char* you switch on (or one of the labels)
is NULL? What if it's just a char* that points at a char,
but not at the start of a string?
I've worked on a compiler for a language where something similar is possible
(although I found it easier to have two forms of switch: one with constant
int cases, and one with any simple type where =/== made sense). And it was
workable.
I'm not saying that definitions could not be invented to
nail down these loose bits; other languages have certainly
come up with switch-ish constructs that handle at least some
of the difficulties. But is it worth the trouble? I'm not
sure -- and that's why I suggested doing an implementation
and finding out whether people would use the feature in
interesting ways.
But no I don't fancy delving into the gcc sources!
Well, if the advance isn't worth some fairly serious
work, that may mean the need it addresses isn't too urgent.
--
Er*********@sun .com