home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / std / cplus / 1516 < prev    next >
Encoding:
Text File  |  1992-11-08  |  3.6 KB  |  86 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!news.tek.com!uw-beaver!uw-coco!nwnexus!ole!ssc!eskimo!seanews!jimad
  3. From: jimad@microsoft.com (Jim Adcock)
  4. Subject: Re: return type of operator=
  5. Message-ID: <1992Nov05.190257.15676@microsoft.com>
  6. Date: 05 Nov 92 19:02:57 GMT
  7. Organization: Microsoft Corporation
  8. References: <1992Oct31.025229.6520@cs.brown.edu> <1992Nov03.195240.14360@microsoft.com> <1992Nov4.051914.25983@en.ecn.purdue.edu>
  9. Lines: 75
  10.  
  11. In article <1992Nov4.051914.25983@en.ecn.purdue.edu> davisonj@en.ecn.purdue.edu (John M Davison) writes:
  12. |In article <1992Nov03.195240.14360@microsoft.com> jimad@microsoft.com (Jim Adcock) writes:
  13. |>In article <1992Oct31.025229.6520@cs.brown.edu> sdm@cs.brown.edu (Scott Meyers) writes:
  14. |>|Is the return type of operator= an lvalue?  
  15. |>
  16. |>No.
  17. |
  18. |        Jim is mistaken; the built-in types return an lvalue in C++ (except for
  19. |bit-field assignment, which I don't know about offhand).
  20. |
  21. |>|In particular, if a, b, and c are of built-in type, is this legal?
  22. |>|
  23. |>|    (a = b) = c;
  24. |>
  25. |>No, it is an error.  If (a = b) were an lvalue the same
  26. |>as "a" then "a" would be being accessed twice for modification in one
  27. |>expression, which in turn is an unconstrained error.  
  28. |
  29. |        It is not an error.  ((a=b)=c) is equivalent to (a=c), except for side
  30. |effects, of course.
  31.  
  32. I continue to disagree -- with a *tiny* retraction.  The ref manual says
  33. that (a = b) is a *value* which is a lvalue.  The ref manual DOES NOT specify
  34. that the lvalue is a reference to a.  Thus I continue to maintain that
  35.  
  36. ((a=b)=c) 
  37.  
  38. has to either NOT modify a in the outer assignment, rather instead modifying
  39. an unnamed temporary, or alternately a is being accessed twice in one expression
  40. for modification, which continues to be an unconstrained error.  In either
  41. case it is certainly NOT correct to say that ((a=b)=c) is the same as (a=c)
  42. except for side effect.  Either ((a=b)=c) is NOT the same as (a=c) or
  43. alternately its an unconstrained error.
  44.  
  45. |>My answers are based on the ANSI-C spec
  46. |
  47. |        In Standard C, = returns an rvalue.  In C++, = returns an lvalue.
  48. |
  49. |        Stroustrup talks about this in the "Lvalues" section of "The Evolution
  50. |of C++: 1985 to 1989".  I quote:
  51. |
  52. |                  "Note that the default definition of assignment of
  53. |          an X as a call of
  54. |
  55. |              X& operator=(const X&)
  56. |
  57. |      makes assignment of Xs produce an lvalue.  For uniformity,
  58. |      this rule has been extended to assignments of built-in
  59. |      types.  By implication, +=, -=, *=, etc.  now also produce
  60. |      lvalues.  So -- again by implication -- does prefix ++ and
  61. |      -- (but not the postfix versions of these operators).
  62. |
  63. |                  "In addition, the comma and ?: can also produce
  64. |          lvalues...."
  65.  
  66. Note in none of this did the author of one of the base documents specify
  67. *what* object the lvalue was referring to.  By continuing *not* to make
  68. that specification the situation remains the same as in ANSI-C where accessing
  69. a variable more than once in an expression for modification remains
  70. ill-defined.  You may get either or both side-effects or neither or something
  71. else entirely.
  72.  
  73. |>The "right" thing to do is to return a value, not a reference.
  74. |>...Don't return a non-const reference.
  75. |
  76. |        Return a non-const reference.  C++ does it with its built-in types.
  77.  
  78. I continue to disagree.  It is extremely unlikely that any C++ programmer
  79. is going to use that non-const reference in a legal manner.  Do them a 
  80. favor up front and at least make it const.  
  81.  
  82. I wonder if this whole mess shouldn't be added to the list of non-modifiable 
  83. lvalues?  Or are these issues going to be eventually clarified by adding clear
  84. constraints to the eventual ANSI/ISO-C++ spec?
  85.   
  86.