home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16288 < prev    next >
Encoding:
Text File  |  1992-11-10  |  2.3 KB  |  62 lines

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