🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4727

Re: integral types in switch expressions

#4727
From: Konrad Schwarz
Date: Wed, 31 Jan 1996 10:42
67 lines
2727 bytes
In article <1996Jan26.220546.22346@sq.com>, msb@sq.com (Mark Brader) writes:
|> Konrad Schwarz (schwarz@mips.complang.tuwien.ac.at) writes:
|> > T a [] = { ... } ...

[ ... ]

|> I don't know what was in the Committee's minds, but I can point out
|> that this code contains constant pointer expressions only when a[]
|> has static storage duration. 

Sorry about being unclear about this.  I did not know that ANSI C allows
initialization of any automatic object.  I meant ``a'' to have static
storage duration and thought this was clear from the context.

|> Therefore, without extending the switch
|> statement to support case-expressions that would have to be computed
|> at run time, you would have a construct that would be useful for some
|> pointer values but not others.

just as only certain expressions are allowed in case expressions

|> 
|> > I am of course aware that
|> > switch (c - a) {
|> > 	case 0: ...
|> > 	case 1: ...
|> > 	case 3: ...
|> > 	default: ...
|> > }
|> > is equivalent code, but it suffers from the division needed to evaluate
|> > c - a.
|> 
|> No, unfortunately, it isn't.  The first one, if it worked at all, would
|> work for *any* value c of appropriate type.  In the second one, c must
|> point to an element of a[], or one past the last element.  But the
|> overhead of division, at least, is a red herring given a suitable
|> optimizer, as the code can simply be compiled as if it read:
|> 
|>   switch ((char *) c - (char *) a) {
|>   	case 0 * sizeof *a: ...
|>   	case 1 * sizeof *a: ...
|>   	case 3 * sizeof *a: ...
|>   	default: ...
|>   }

Given a suitable optimizer, any language deficiency can be optimized away.
I believe languages should not
introduce gratuitous restrictions in the first place.

The illegal code mentioned in my original posting would have useful to me.
I find its illegality arbitrary.  The non-equivalence between the first
and second version of my code you point out only exacerbates the situation,
since a more general version of the code is:

static T a, b;
T *c;
...
switch (c) {
	case &a: ...
	case &b: ...
	default: ...
}

for which no translation is possible, except as a series of ``if'' statements
of the kind the ``switch'' statement is presumably designed to avoid.

Konrad Schwarz
Technical Details
Message-ID: <4enh1r$dq7@news.tuwien.ac.at>
Path: arc1-ca.pugleaf.net!a3.complang.tuwien.ac.at!schwarz
References: <4eb5r1$b04@news.tuwien.ac.at> <1996Jan26.220546.22346@sq.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!a3.complang.tuwien.ac.at!schwarz
From: schwarz@a3.complang.tuwien.ac.at (Konrad Schwarz)
Newsgroups: comp.std.c
Subject: Re: integral types in switch expressions
Date: 31 Jan 1996 10:42:03 GMT
Organization: TU Wien
Message-ID: <4enh1r$dq7@news.tuwien.ac.at>
References: <4eb5r1$b04@news.tuwien.ac.at> <1996Jan26.220546.22346@sq.com>
NNTP-Posting-Host: a3.complang.tuwien.ac.at