home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 18247 < prev    next >
Encoding:
Text File  |  1992-12-21  |  7.2 KB  |  204 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re:  Give me safe C++
  5. Message-ID: <1992Dec20.010907.10429@ucc.su.OZ.AU>
  6. Sender: news@ucc.su.OZ.AU
  7. Nntp-Posting-Host: extro.ucc.su.oz.au
  8. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  9. References: <1992Dec3.193006.5520@almserv.uucp> <1992Dec6.131757.7448@ucc.su.OZ.AU> <716@ulogic.UUCP>
  10. Date: Sun, 20 Dec 1992 01:09:07 GMT
  11. Lines: 191
  12.  
  13. In article <716@ulogic.UUCP> hartman@ulogic.UUCP (Richard M. Hartman) writes:
  14. >In article <1992Dec6.131757.7448@ucc.su.OZ.AU> maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  15. >>In article <1992Dec3.193006.5520@almserv.uucp> e3ubec@fnma.COM writes:
  16. >>>
  17. >>>     I agree. My implication was that if you don't like C++, then
  18. >>>you should look for a language that does what you want.
  19. >>
  20. >>    If you are employed by a large firm you may not have any choice.
  21. >>Many organisations may chose C++ because it is an ISO standard language.
  22. >>(Well, it will be ...) Therefore, the C++ designers have a responsibility
  23. >>to make it as decent as possible. For many programmers, C++ will be
  24. >>the language they are required to use, not the one they are free to choose.
  25. >
  26. >Au contraire.  The responsibility is to make it as *useful* as
  27. >possible.  Utility and safety are frequently at odds.  Look at
  28. >the difference between Pascal (safe) and C (useful).  
  29.  
  30.     And even Pascal was not so safe, eg variant records.
  31. >
  32. >Safety (when not being enforced by the language) is to a large
  33. >extent (IMHO) a matter of good programming practices.  
  34.  
  35.     To some extent, yes. But safety enforced by the 
  36. language is sometimes a good thing. For example, you would
  37. probably agree that type-safe linkage was a Good Thing??
  38. This is deliberately and strictly enforced by the compiler
  39. (and linker).
  40.  
  41.     Have you lost something here? Yes, indeed, rapid
  42. prototyping is much more difficult when routines in
  43. parts of the code that never get called must still be
  44. called with correct parameters.
  45.  
  46.     There are other times when enforcing something
  47. is good because it allows automatic optimisation to be performed,
  48. and often this also allow guarantees of correctness. For example,
  49. the pascal for loop is guarranteed to termina, and the
  50. control variable can always be put in a fast register.
  51.  
  52. >Like a
  53. >seemingly ever-growing segment of the general populace, the cry
  54. >comes "oh powers that be, protect us from ourselves", and thus
  55. >we get laws requiring people to wear seatbelts.  It is no that
  56. >I am against seatbelts (or safety), but I was perfectly capable
  57. >of wearing a seatbelt before the law existed.  
  58.  
  59.     It is not so simple  Australia, which has a national
  60. health scheme. Here the alternative "If you get hurt in a crash
  61. and were'nt wearing a safety belt, we'll just let you die"
  62. would not be considered politically palatable.
  63.  
  64.     Similarly, lots of kids got hurt in bicycle accidents.
  65. Now its mandatory to wear a helmet. I didnt wear one until
  66. I had to. Was saving those kids from brain damage such a bad thing?
  67.  
  68. >Likewise, to remove some of the "sharp
  69. >edges" from C and C++ would be to gut them of half of their power
  70. >and usefulness.  
  71.  
  72.     No one is talking about preventing you stuffing
  73. up your software :-) The issue is more to make you declare
  74. publically and loudly when you do so, so others reading
  75. your code will know what you have done.
  76.  
  77. >>    This doesn't mean we should not provide facilities in C++
  78. >>for writing programs which can be checked for safety by the compiler
  79. >>or run-time system, merely that we should not remove or fail to add
  80. >>unsafe features which have other advantages.
  81. >
  82. >Wait a minute.  If you add (or retain) an "unsafe feature" you
  83. >are defeating your own point!  An unsafe feature is an unsafe 
  84. >feature, no matter how you slice it.
  85.  
  86.     No, my point was poorly expressed. I am considering
  87. adding 'variants', a form of secure union in which it is
  88. impossible to access the wrong component.
  89.  
  90.     variant V [V1, V2] v=V1(2);
  91.     select(v){
  92.     type(V1 v1) { ... }
  93.     type(V2 v2) { ... }
  94.     };
  95.  
  96. Here you simply cannot access the wrong type, unlike an ordinary
  97. union. COmpletely safe.
  98.  
  99. What I'm saying is that 
  100.  
  101.     a) ordinary unions stay. 
  102.          I'm not removing them just because they're not safe.
  103.     b) variants are needed because they allow safety not available
  104.        using unions. They're also more efficient.
  105.  
  106. >
  107. >
  108. >>    In particular where commonly used unsafe idioms are actually
  109. >>the bast available technique, we should look at what sort of language
  110. >>extension would facilitate expression of the idiom in a safe way.
  111. >
  112. >But we would not change the language to outlaw the unsafe idiom?
  113.  
  114.     Well, in this case I would not, because the idiom for
  115. variants is only ONE use of unions.
  116.  
  117. >Then the language is still unsafe.
  118.  
  119.     Yes, in this case. But lots of programs whose correctness
  120. was uncertain in this area could not be written in such a way to
  121. be absolutely certain that there were no errors. You wouldn't
  122. *have* to do this, of course.
  123.  
  124.     The 'variants' proposal makes it possible  for the compiler
  125. to tell you if you stuff up, just like type-safe linkage.
  126. Using unions means you have to verify every line of code by hand.
  127.  
  128. >
  129. >>    A simple (??) language extension would allow static checking of
  130. >>this method. The designers and users must chose if the added language
  131. >>complexity is worth it. What do you think?
  132. >>
  133. >>    class PayRec;
  134. >>    class ContractorRec;
  135. >>    class DirectorRec;
  136. >>    variant PayRec [EmployeeRec, ContractorRec, DirectorRec];
  137. >>
  138. >>    PayRec* P;
  139. >>
  140. >>    for(int i=0; i<nP; ++i)
  141. >>    {
  142. >>        cout << P[i]->name; // common field
  143. >>        select(P[i]) // type sensitive  code below
  144. >>        {
  145. >>            type(EmployeeRec *E) { ... }
  146. >>            type(ContractorRec *C) { ... }
  147. >>            type(DirectorRec *D) { ...}
  148. >>        }
  149. >>    }
  150. >
  151. >With no language extensions, this seems equivilant to what you
  152. >have proposed (note: off the top of my head, incomplete, uncompiled 
  153. >example follows -- standard disclaimers apply):
  154. >
  155. >class PayRec {
  156. >    public:
  157. >        enum PayRecType { Employee, Contractor, Director };
  158.  
  159. >class EmployeeRec : public PayRec {
  160. >...
  161. >};
  162.  
  163.     Here is where your example fails. How do you know in advance
  164. to derive EmployeeRec from PayRec?
  165.  
  166.     In the case of a union or variant, thats is done
  167. after EmployeeRec is locked away in the library.
  168.  
  169.     You example is more or less correct (except you forgot
  170. to downcast) in the sense that it works.
  171. You will note that your code is not secure.
  172. There is no reason to use virtual functions or a common
  173. base here, no inheritance or other relationship exists
  174. between these types.
  175.  
  176.     The equivalent idiom is to use
  177.  
  178.     struct PayRec {
  179.         enum {Employee, Contractor, Director} tag;
  180.         union {
  181.             Employee E;
  182.             Contractor C;
  183.             Director D;
  184.         };
  185.     };
  186.  
  187. and a switch, which doesnt require the downcast (which incidentally
  188. you forgot), and can be done after Employee, Contractor and Director
  189. have been closed off and locked away.
  190.  
  191. Both this method and yours, however suffer from the
  192. problem of being unsafe, and messy to code. It much easier
  193. to say
  194.  
  195.     variant PayRec [Employee, Contractor, Director];
  196.  
  197. isnt it?
  198.  
  199. -- 
  200. ;----------------------------------------------------------------------
  201.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  202.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  203. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  204.