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

  1. Path: sparky!uunet!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
  2. From: tmb@arolla.idiap.ch (Thomas M. Breuel)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: defining cast operators outside classes
  5. Message-ID: <TMB.92Sep8215104@arolla.idiap.ch>
  6. Date: 9 Sep 92 01:51:04 GMT
  7. References: <TMB.92Sep7162324@arolla.idiap.ch> <1992Sep8.173613.25113@taumet.com>
  8. Sender: news@ai.mit.edu
  9. Reply-To: tmb@idiap.ch
  10. Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
  11.     Perceptive)
  12. Lines: 86
  13. In-reply-to: steve@taumet.com's message of 8 Sep 92 17:36:13 GMT
  14.  
  15. In article <1992Sep8.173613.25113@taumet.com> steve@taumet.com (Steve Clamage) writes:
  16.  
  17.    tmb@arolla.idiap.ch (Thomas M. Breuel) writes:
  18.  
  19.    >Apparently, it is impossible to define a conversion operator for a
  20.    >class without making the conversion operator a member function.
  21.  
  22.    In the case of some operators you run the risk of having legal code
  23.    with different meanings depending on whether the operator declaration
  24.    is visible.
  25.  
  26. Why is the C++ committee suddenly so concerned with my well-being?
  27. Those people who feel that such a feature is dangerous simply don't
  28. define conversion operators outside their classes.
  29.  
  30.    The semantics depend on which non-class declarations are visible,
  31.    which is not normally the case in C++.  (There is a somewhat
  32.    parallel case:  You omit one overloaded function declaration and
  33.    an unintended function is called.  Maybe someone can think of a way
  34.    to solve that problem in the language.)
  35.  
  36. As you yourself point out, the problem is analogous to "subtle bugs"
  37. that come from overloading. I'm in general no friend of either
  38. overloading or automatic conversions precisely because they make a new
  39. class of subtle bugs possible. But given that C++ has gone down this
  40. path so far (and sacrificed a lot in other kinds of potential
  41. notational conveniences for it), I'd just as much have all the
  42. facilities and not be limited in arbitrary ways.
  43.  
  44.    In the case of conversions, it is likely that the conversion function
  45.    would need to be a friend function, thus needing to appear in the
  46.    class definition anyway.
  47.  
  48. I don't find that to be true at all. And, in fact, there are good
  49. reasons why conversion operators tend _not_ to need to look at the
  50. internals of data structures in many cases.  Conversions tend to be
  51. used in cases where visible properties of an object are preserved but
  52. representation is changed. Hence, conversions mainly "take data out"
  53. of one public interface and "stuff it into" another public interface.
  54. Concretely, in the case of matrices:
  55.  
  56.      operator SomeMatrix(OtherMatrix &m) {
  57.         SomeMatrix result(m.dim(0),m.dim(1));
  58.         for(int i=0;i<m.dim(0);i++)
  59.            for(int j=0;j<m.dim(1);j++)
  60.               result(i,j) = m(i,j);
  61.         return result;
  62.      }
  63.  
  64.    If you need to create an ad-hoc conversion from class A to class B
  65.    when you don't own class A, you can make a B constructor taking an A.
  66.    If you don't own class B either, you can write an ordinary function
  67.    which does the conversion.
  68.  
  69. Yes, thank you, I knew that. By that argument, we might do away with
  70. virtual functions and classes as well, since the can be replaced by
  71. function pointers and structures.
  72.  
  73.    In the end, the restriction elminates a source of dangerous but subtle
  74.    bugs while creating virtually no extra inconvenience.
  75.  
  76. I disagree on both points. If you don't use the feature, you don't get
  77. the class of bugs that can result from it (just like if you don't use
  78. multiple inheritance, you don't get MI bugs). And, it does create
  79. "extra inconvenicence" because...
  80.  
  81.    The only
  82.    inconvenience I can see occurs when ALL of these are met simultaneously:
  83.        - You need to convert an A to a B.
  84.        - You can't modify A or B.
  85.        - Everything needed to do the conversion is public.
  86.        - You want implicit conversion, not "b = A_to_B(a)".
  87.  
  88. Yes, and these conditions are frequently met in mathematical/numerical
  89. software, where you have to convert between different vector
  90. datatypes, etc. Implicit conversions are useful because you don't want
  91. to clutter your already complicated equations with conversions; in
  92. fact, mathematical/numerical objects are the _only_ place where I like
  93. to use overloading or implicit conversions.
  94.  
  95. In summary, allowing the user to define conversion operators outside
  96. classes would remove an arbitrary restriction from C++. I don't think
  97. it is any more dangerous than, say, overloading, and you are immune
  98. from "its subtle bugs" if you don't use the feature.
  99.  
  100.                     Thomas.
  101.