home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!concert!sas!mozart.unx.sas.com!sasghm
- From: sasghm@theseus.unx.sas.com (Gary Merrill)
- Subject: Re: constant-expression
- Originator: sasghm@theseus.unx.sas.com
- Sender: news@unx.sas.com (Noter of Newsworthy Events)
- Message-ID: <BxIGot.B3J@unx.sas.com>
- Date: Tue, 10 Nov 1992 17:31:40 GMT
- References: <0E010598.ia94k0@kithrup.Irvine.CA.US>
- Nntp-Posting-Host: theseus.unx.sas.com
- Organization: SAS Institute Inc.
- Lines: 48
-
-
- In article <0E010598.ia94k0@kithrup.Irvine.CA.US>, nagel@kithrup.Irvine.CA.US (Mark D. Nagel) writes:
- |> I checked the FAQ and found not a clue to the following question.
- |>
- |> Given an array declaration like:
- |>
- |> const int foo[] = { 1, 45, 67, 4 };
- |>
- |> why is the following code rejected (it is in MSC 6.0 at least)?
- |>
- |> switch (integer_variable)
- |> {
- |> case foo[2]:
- |> ...
- |> break;
- |>
- |> ...
- |> }
- |>
- |> I gather that the ANSI standard says that the indexing operation makes
- |> the expression not a constant-expression, but it seems that a compiler
- |> should be able to deal with this. I could understand if the case
- |> contained something like foo[i], but it doesn't.
- |>
- |>
- |> -------------------------------------
- |> Mark Nagel <nagel@mdn.uucp>
- |>
- |> If at first you don't succeed, that's pretty much it.
- |> -- SDI Design Team
-
- Sure, in the specific (type of) case that you provide in the example,
- a compiler could do something reasonable. But why bother? First,
- the implementation of the appropriate action in a code generator would
- not be a particular instance of a general code generation strategy
- since you can't do it when you don't have access to the array and
- its initialization in terms of constants. Think of how (if you
- were writing the language standard) you would phrase the constraint
- on case values to permit just the sort of thing you have in mind
- here but prohibit pathological cases.
-
- Second, the above is an example of genuinely silly code. The
- language has a fairly nice mechanism for what you are trying to
- accomplish by means of your perversion: its called an *enumeration*.
- --
- Gary H. Merrill [Principal Systems Developer, C Compiler Development]
- SAS Institute Inc. / SAS Campus Dr. / Cary, NC 27513 / (919) 677-8000
- sasghm@theseus.unx.sas.com ... !mcnc!sas!sasghm
-