home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / std / c / 3021 < prev    next >
Encoding:
Text File  |  1992-11-17  |  1.3 KB  |  38 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: Casting const; Is this standard conforming?
  5. Message-ID: <1992Nov17.183027.19459@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <1992Nov16.200843.5565@Princeton.EDU>
  8. Date: Tue, 17 Nov 1992 18:30:27 GMT
  9. Lines: 27
  10.  
  11. rhl@grendel.Princeton.EDU (Robert Lupton the Good) writes:
  12.  
  13.  
  14. >   const int i = 0;
  15. >
  16. >   (int)i = 1;                /* Fails */
  17. >   *(int *)&i = 1;            /* OK */
  18.        
  19. >Thinking about ROMs my guess is that gcc and the IBM xlc compilers are
  20. >correct to reject the former assignment, and that their acceptance of
  21. >the latter is permitted, but could anyone comment? 
  22.  
  23. The first assignment is illegal because a cast yields a value, not
  24. an lvalue, and you may not assign to a value.  It has nothing to do
  25. with const.
  26.  
  27. The second assignment assigns to an object via a modifiable lvalue, and
  28. so violates no constraint.  Nonetheless, the results of assigning to an
  29. object declared const via a non-const lvalue are undefined.  (ANSI 3.5.3)
  30.  
  31. You can't rely on any particular result.  The compiler is not obliged
  32. to diagnose the problem, but may do so.  The resulting code might do
  33. what you intended, but need not.
  34. -- 
  35.  
  36. Steve Clamage, TauMetric Corp, steve@taumet.com
  37. Vice Chair, ANSI C++ Committee, X3J16
  38.