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

  1. Xref: sparky comp.std.c++:1167 comp.lang.c++:13565
  2. Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!allegra!alice!bs
  3. From: bs@alice.att.com (Bjarne Stroustrup)
  4. Newsgroups: comp.std.c++,comp.lang.c++
  5. Subject: Exponentiation operator proposal
  6. Message-ID: <23660@alice.att.com>
  7. Date: 12 Sep 92 18:30:53 GMT
  8. Article-I.D.: alice.23660
  9. Organization: AT&T Bell Laboratories, Murray Hill NJ
  10. Lines: 68
  11.  
  12.  
  13. Comments on Mat Austern's proposal for an exponentiation
  14. operator in C++ (posted to comp.lang.std and, I think,
  15. also to comp.lang.c++).
  16.  
  17. (1) I think overloading and inlining can be used a bit
  18.     more effectively than is assumed in tht proposal.
  19.     For example:
  20.  
  21.     double pow(double,double); // what C gives us
  22.     double _pow(double,int); // general pow(double,int)
  23.  
  24.     inline double pow(double d, int i) // inlining to catch
  25.                        // important special cases
  26.     {
  27.         if (i == 2) return d*d;
  28.         else if (i == 3) return d*d*d;
  29.         else _pow(d,i);
  30.     }
  31.     
  32.     
  33.     void f(double d, int i)
  34.     {
  35.         pow(d,d);
  36.         pow(d,i);
  37.         pow(d,2);
  38.         pow(d,3);
  39.         pow(d,4);
  40.     }
  41.  
  42.     will generate better code for small integer powers.
  43.     This technique is fairly gaeneral and does not depend on
  44.     cleverness beyond the average C++ compiler.
  45.  
  46.     I agree that a clever compiler can do better, but for now
  47.     this technique might be helpful to some.
  48.  
  49. (2) I think we could actually get ** as the exponentiation operator.
  50.  
  51.     Consider the well-known counter-example:
  52.  
  53.         int a;
  54.         int* p;
  55.  
  56.         a**b;
  57.  
  58.     For C compatibility, a**b must mean a * (*b). However,
  59.     with a minor sleigh of hand this can be achieved even
  60.     if we introduce and exponentiation operator represented
  61.     by the single token **.
  62.  
  63.     All we need to do is to say that the exponentiation
  64.     operator ** given a first operand `a' of arithmetic type
  65.     and a second operand `b' of pointer type (that is, `a**b')
  66.     mean multiply the first operand with the dereference value
  67.     of the second operand (that is, `a*(*b)'). This would not
  68.     be quite 100% compatible because a*b**p where p is a pointer
  69.     would mean a*(b*(*p)) rather than (a*b)*(*p), but I suspect
  70.     that it would be compatible enough.
  71.  
  72.     Similarly the token ** when used in a declarator must mean
  73.     * * (that is pointer to pointer.
  74.  
  75.     
  76.     I think this would take care of the compatibility problems.
  77.     There may be other minor details to consider, but I don't
  78.     see any insurmountable problems.
  79.  
  80.