home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / std / cplus / 1181 < prev    next >
Encoding:
Internet Message Format  |  1992-09-14  |  5.9 KB

  1. Xref: sparky comp.std.c++:1181 comp.lang.c++:13664
  2. Path: sparky!uunet!cis.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!agate!matt
  3. From: matt@physics2.berkeley.edu (Matt Austern)
  4. Newsgroups: comp.std.c++,comp.lang.c++
  5. Subject: Re: Exponentiation operator proposal
  6. Date: 14 Sep 92 22:50:05
  7. Organization: Lawrence Berkeley Laboratory (Theoretical Physics Group)
  8. Lines: 110
  9. Message-ID: <MATT.92Sep14225005@physics2.berkeley.edu>
  10. References: <23660@alice.att.com> <1992Sep13.193507.27813@ima.isc.com>
  11.     <BuJJxs.8Hx@world.std.com> <23663@alice.att.com>
  12. Reply-To: matt@physics.berkeley.edu
  13. NNTP-Posting-Host: physics2.berkeley.edu
  14. In-reply-to: bs@alice.att.com's message of 14 Sep 92 14:27:49 GMT
  15.  
  16.  
  17. Recently, Bjarne Stroustrup suggested that it would be possible for
  18. the exponentiation operator to be denoted **.  At first sight this
  19. appears to be impossible, since ** already has a very different
  20. meaning in C++, and previous discussions of an exponentiation operator
  21. (mine included) have dismissed this possibility without further
  22. analysis.
  23.  
  24. This has been excessively hasty.  Exponentiation is only defined if
  25. both operands are of a numeric type; conversely, ** is currently
  26. meaningful only if it precedes a pointer.  There is no overlap, so it
  27. might, therefore, be possible to use type information to disambiguate
  28. these different uses of **.
  29.  
  30. Specifically, we might consider the following proposal:
  31.  
  32. --------------------------------------------------------------------
  33.  
  34.     Two new tokens, ** and **=, will be added to the C++ language.  **
  35.     will be both a unary and a binary operator.  Unary ** will have
  36.     the same precedence as the other unary operators, and binary **
  37.     will have a new precedence level between the multiplicative
  38.     operators and the pointer-to-member operators.
  39.  
  40.     Unary ** is defined only for an operand which is a pointer to a
  41.     pointer, and it will mean to dereference that operand twice.
  42.  
  43.     If the arguments to binary ** are both of a numeric type, then it
  44.     denotes exponentiation, and its semantics will be those described
  45.     by my earlier proposal.  If its first operand is of a numeric type
  46.     and its second is a pointer to a number, then it means to multiply
  47.     the first operand by the contents of the second.  For other
  48.     combinations of argument types, binary ** is undefined.
  49.  
  50.     **= is defined only for the case where the first operand is a 
  51.     modifiable lvalue of a numeric type and the second operand is an
  52.     object of a numeric type; it is defined the same way as the other
  53.     op= operators.
  54.  
  55.     **=, and unary and binary **, may be overloaded by the user if 
  56.     at least one argument is a class.
  57.  
  58. ---------------------------------------------------------------------- 
  59.  
  60. This proposal does, at least, implement ** as an exponentiation
  61. operator while continuing to allow its current uses.  However, it
  62. presents several difficulties.
  63.  
  64. First, it breaks existing code.  To see why it must do so, notice that
  65. in current C++, ** is not a single token; it is a sequence of either a
  66. binary operator followed by a unary operator, or else two unary
  67. operators.  The difficulty is that the binary and the unary operators
  68. have different precedence.  So, in particular, consider the expression
  69.             x/y**p,
  70. which, by the current language definition, is equivalent to
  71.             (x/y) * (*p).
  72. According to the proposal above, however, ** is a single token with a
  73. precedence higher than division, so this expression will mean
  74.             x / (y * (*p)).
  75. I don't know how much code actually contains expressions like this,
  76. but it isn't unreasonable to assume that at least some does.
  77.  
  78. Second, even in code which won't be broken by this change in the
  79. behavior of **, users will have to examine their code to make sure
  80. that it still means what they thought it did.  ** is a common enough
  81. character sequence (consider, for example, the idiom **argv) that most
  82. programmers will at least have to glance at their code, even if
  83. nothing actually needs to be changed.
  84.  
  85. Third, the disambiguation rules don't go far enough: they solve the
  86. problem for built-in types, but not for class types.  Users who define
  87. ``smart pointers'' will want the dereference operator to work the same
  88. way for their smart pointers as for ordinary C pointers, so, if they
  89. overload unary * to mean smart pointer dereference, they will
  90. similarly be forced to overload unary and binary **.  This imposes an
  91. additional burden on people who use a rather common programming
  92. technique.
  93.  
  94. ** does have one potential advantage over *^ as an exponentiation
  95. operator: that is how exponentiation is denoted in FORTRAN.  I regard
  96. that as a small advantage, however; there is nothing particularly
  97. natural about FORTRAN's use of that symbol, and I believe that
  98. scientific programmers could get used to *^ just as easily as they got
  99. used to the once-unfamiliar **.  In fact, it isn't completely clear to
  100. me that ** would really be more readable as a C++ exponentiation
  101. operator than *^ would be: scientific programmers, like other
  102. programmers, would have to cope with the multiple meanings of **.
  103.  
  104. I believe that an exponentiation operator would be of genuine benefit
  105. to C++ scientific programmers, and I believe that we who write
  106. scientific code are justified in asking that it be included.  I don't
  107. think, however, that we are justified in asking for a change to the
  108. language that makes life harder for other C++ programmers.  Using **
  109. to denote exponentiation does exactly that: it changes the meaning of
  110. legal C++ code, and it makes a useful programming technique more
  111. difficult.
  112.  
  113. It is possible to implement an exponentiation operator so that the
  114. extension to the language will have no effect at all for people who
  115. don't deliberately use the new feature.  This is a better idea.
  116.  
  117.  
  118.  
  119.  
  120.  
  121. --
  122. Matthew Austern                   Just keep yelling until you attract a
  123. (510) 644-2618                    crowd, then a constituency, a movement, a
  124. austern@lbl.bitnet                faction, an army!  If you don't have any
  125. matt@physics.berkeley.edu         solutions, become a part of the problem!
  126.