Proposed Enhancement to select/case (yes, I know...)

Ron Guilmette rfg at NCD.COM
Sun Sep 2 03:12:07 AEST 1990


In article <BURLEY.90Aug30030645 at world.std.com> burley at world.std.com (James C Burley) writes:
<
<...But how about this: allow ranges (and, perhaps, lists) on case statements.

There are two separate questions here.  One concerns case ranges, and the
other concerns lists.  I perfer to talk about each separately.

Regarding case ranges, my personal feeling is that this is a good and useful
feature and that it ought to make its way into the final standard for C++.
It is my understanding that this feature was proposed during the ANSI C
deliberations, but that it was rejected by X3J11 (for reasons that I'm not
clear on).  Perhaps someone who served on X3J11 could give us a quick
summary about what happened to case ranges in X3J11.

Anyway, ANSI C standard or no, some C compiler vendors do offer this feature.
The only one I'm sure offers it is MetaWare.

In the C++ would, g++ offers case ranges.  This should prevent X3J16 members
from rejecting the idea outright because of a lack of prior art.

By the way, I believe that both MetaWare C and g++ implement case ranges via
a syntax like:

	case LOW..HIGH:

Regarding "case lists", it seems clear that a simple syntax, i.e.:

	case FOO,BAR:

won't work because the separator comma could be parsed as the normal comma
operator, which would mess up everything.  James proposes:

	case [FOO,BAR]:

which is more verbose than the simple syntax but which still saves a bit of
typing relative to:

	case FOO:
	case BAR:

The only problem is that the values used to designate cases should (if reason
prevails) still be allowed to be formed from "static constant expressions"
and thus, the problem of the comma being parsed as a comma operator still
plagues this syntax also.

Anyway, it looks like developing a special syntax for "case lists" may be
more trouble than it is worth.

<I believe nothing about this proposal is at all difficult to implement in
<terms of parsing or generating code.

Correct.

<Anyway, the basic idea of case ranges (and lists of cases) comes from Fortran
<90, as many of you already know.

I think not.  I'm pretty sure that case ranges appeared in Pascal long
before anybody in FORTRAN land ever though of the idea.

<Does anyone know any good reasons NOT to implement some or all of these
<features in today's C++ and C compilers, with an eye towards codifying them
<in the next ANSI standards for these languages?

Apparently, Michael Tiemann could not think of any serious problem with
case ranges, so he went forth and implemented them.

<I wouldn't suggest putting
<them into the standards until people had had a couple of years to try them
<out in real code...

Right.  Given that this feature has already been in g++ for some time,
you may consider it "prior art".

-- 

// Ron Guilmette  -  C++ Entomologist
// Internet: rfg at ncd.com      uucp: ...uunet!lupine!rfg
// Motto:  If it sticks, force it.  If it breaks, it needed replacing anyway.



More information about the Comp.std.c mailing list