home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / std / cplus / 1165 < prev    next >
Encoding:
Text File  |  1992-09-12  |  2.0 KB  |  56 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: Integral promotions
  5. Message-ID: <1992Sep12.174705.6381@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <1992Sep11.171215.337@muddcs.claremont.edu>
  8. Date: Sat, 12 Sep 1992 17:47:05 GMT
  9. Lines: 45
  10.  
  11. jason@jarthur.claremont.edu (Jason Merrill) writes:
  12.  
  13. >Why is the conversion int -> long (or unsigned int -> unsigned long) not
  14. >considered an integral promotion?  
  15.  
  16. >I am in the situation of trying to take either a pair of longs (with
  17. >default arguments) or a double for creation of a Fraction.  If I do
  18. >something like 
  19.  
  20. >Fraction(1), 
  21.  
  22. >my compiler (BC++ 3.0) complains about ambiguity.  This seems wrong to me.
  23.  
  24. The "promotions" are from "smaller" types to int or unsigned int.
  25. There are automatic conversions to long and double.  This is the
  26. heritage from C, unchanged in C++.  That isn't much of a "why",
  27. but I don't have a better answer.
  28.  
  29. In contrast to your question, some programmers complain about having
  30. so many silent conversions, some of which they feel ought to be errors.
  31.  
  32. No conversion is preferred over any other conversion when resolving
  33. a call to an overloaded function.  If you have only
  34.     foo(long)
  35.     foo(double)
  36. and call foo() with an int argument, neither function is preferred
  37. over the other, and the call is ambiguous.
  38.  
  39. You generally want to provide an overloaded function taking an int
  40. parameter if you have any taking any other integral types, and
  41. one taking a double parameter if you have any taking other
  42. floating types.  You can do this:
  43.  
  44. inline T foo(int i) { return foo((long)i); } // foo(long) returns type T
  45.  
  46. (Hmmm.  T foo(long) sounds like a beverage.)
  47.  
  48. Make this NON-virtual in the base class, and don't override it in
  49. any derived class.  You will then always get the correct version
  50. of a virtual foo(long) every time you call foo with a (signed)
  51. integer type, with no extra overhead.
  52. -- 
  53.  
  54. Steve Clamage, TauMetric Corp, steve@taumet.com
  55. Vice Chair, ANSI C++ Committee, X3J16
  56.