home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16382 < prev    next >
Encoding:
Text File  |  1992-11-17  |  5.7 KB  |  133 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!wupost!spool.mu.edu!agate!doc.ic.ac.uk!mrccrc!warwick!bham!bhamvx!mccauleyba
  3. From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
  4. Subject: Re: Two things: typeof() and exceptions
  5. Sender: usenet@rs6000.bham.ac.uk (USENET News Service)
  6. Message-ID: <1992Nov16.202036.1@vax1.bham.ac.uk>
  7. Date: Mon, 16 Nov 1992 20:20:36 GMT
  8. Lines: 121
  9. References: <MCGRANT.92Nov15134127@rascals.stanford.edu>
  10. Organization: University of Birmingham
  11.  
  12. In article <MCGRANT.92Nov15134127@rascals.stanford.edu>, mcgrant@rascals.stanford.edu (Michael C. Grant) writes:
  13. > First of all, I'm disappointed that the typeof() operator suggestion did 
  14. > not generate much discussion, save a couple of e-mail messages. Don't you
  15. > think that it would extend the utility of templates?
  16. typeof() is a good idea mostly but not exclusively in templates.
  17.  
  18. IMHO we should always think carefully before adding a new token to
  19. the language after all some poor sod will have used it as a symbol.
  20. It would not detract from the readability to use class() instead.
  21.  
  22. There is a presedent for using the token `class' in this way:
  23.   `template<class T>' "class" meaning "T is a symbol of type class"
  24.   `class(xxx)' "class" meaning "Convert xxx to a value of type class"
  25.  
  26. The parenthises would have to be manatory to keep the grammer simple
  27. (possible?) to parse.
  28.  
  29. > template <class T> class Foo {    ---> <double>     <complex>
  30. >         T data;                   ---> double data; complex data;
  31. >         typeof(abs(T)) norm;      ---> double norm; double norm;
  32. > }                                 --->
  33. > One person pointed out that, in my example,
  34. > that it must be considered that abs(const T&) should be a possible
  35. > substitution as well (thought the tone of his message was that he
  36. > didn't think it was necessary). Well, I agree that abs(const T&) should
  37. > be considered by typeof(abs(T)), since standard overloading would
  38. > consider it, but perhaps then the syntax is a bit confusing.
  39.  
  40. An important question is just what goes inside the parenthises? Is it
  41. something with a sytax that is already used in the language? Methinks that
  42. `abs(T)' is not. I don't think this creates an impossible situation for 
  43. the parser but it certainly doesn't make life easy for it because the thing
  44. given inside the parentises of abs() may be either an expression or
  45. a type. If it is a type it denotes a generic expression of that type
  46. and should be subjected to the same type conversion rules as would be
  47. an expression. (This answers the question about what happens if abs(const T&) 
  48. is actually defined).
  49. eg.
  50.   Blob blob;
  51.   int foo(long,class(blob)); // int foo(long,Blob);
  52.   class(foo(int,const volatile Blob)) a; // int a
  53.   const class(foo(a,blob)) b; //const int b
  54.   class(foo(1,Blob)) c; //even let us mix up the 2 syntaxes! 
  55.  
  56. As with `sizeof()' I think class() must be compile time only and hence cannot
  57. virtual.
  58.   Base *p=new Derived;
  59.   class(*p) *q; // q must be Base* _not_ Derived* 
  60.  
  61. BTW `class(abs)' is indeterminate. 
  62.  
  63. > Secondly, I was reading the reference manual for Dylan (Apple's new OO
  64. > language) and I noticed something that was pretty hot in the exception-
  65. > handling domain. Basically, in any given procedure you can define a set
  66. > of instructions that are guaranteed to be executed even if the rest of
  67. > the procedure is terminated early due to an exception. Currently in C++,
  68. > automatic variables are destroyed as the stack is unwound. But, with 
  69. > a set of 'unwind' statements in a function, I could destroy all of the
  70. > variables I allocated on the heap as well.
  71. Yes? In C++ this is written thus:
  72.  
  73. void foo() {
  74.   try { /* body of function */ }
  75.   catch (...) { /* unwinding code */ throw; }
  76. }
  77.  
  78. I'm sure the answer to your question can't be that simple so perhaps you'd
  79. better explain again.
  80.  
  81. BTW you can opt for the "industrial strength pointer" way of doing things
  82. which roughly goes like this:
  83.  
  84. template<class T> Ptr {
  85.   struct InnerBit {
  86.     T* ptr;
  87.     int ref;
  88.     InnerBit(p) : ptr(p), references(1) {}
  89.     ~InnerBit() { delete ptr; };
  90.   };
  91.   InnerBit* ptr;
  92.   cutlink() {if (ptr&&!--ptr->ref) delete ptr;} 
  93. public:
  94.   operator T*() {return ptr->ptr;}
  95.   T() : p(NULL) {}
  96.   T(T* p) : ptr(new InnerBit(p)) {}
  97.   ~T() {cutlink();}
  98.   T(const T& p) ptr(p.ptr) :{ if (ptr) ptr->ref++;}
  99.   T& operator = (const T& p) {cutlink(); (ptr=p.ptr)->ref++;}
  100.   T* operator ->() {return *this;} // see discussion on -> in this news group
  101. }
  102.  
  103. (I've not checked this code since I haven't a complier to hand so you'll
  104. have to excuse typos :-> )
  105.  
  106. An object allocated by:
  107.  
  108. Ptr<Blob> p = new Blob;
  109.  
  110. is automatically deleted once all the "industial strength pointers" that
  111. refer to it have been deleted. Obviously you must be careful not to mix in
  112. any non-industial strength pointers.
  113.  
  114. > If I had the Dylan manual with me I'd give you the exact syntax and
  115. > description. But, if you understand that last paragraph, please
  116. > comment! Note that that behavior could be simulated by defining a 
  117. > class local to that function that contains in its destructor all of
  118. > the said 'unwinid' statements. Then, when that class is destroyed by
  119. > the stack-unwinding procedure it would give the desired effect. But,
  120. > since exceptions haven't been implemented on a wide basis yet perhaps
  121. > the Dylan model can be considered.
  122. Actually the local class won't work in the general case because the
  123. destructor is not in the scope of the function so it can't get at your
  124. autos.
  125. -- 
  126.     \\   ( )    No Bullshit!     |  Email: B.A.McCauley@bham.ac.uk   
  127.  .  _\\__[oo        from         |  Voice: +44 21 471 3789 (home)
  128. .__/  \\ /\@    /~)  /~[   /\/[  |    Fax: +44 21 625 2175 (work)
  129. .  l___\\      /~~) /~~[  /   [  |  Snail: 197 Harborne Lane,
  130.  # ll  l\\    ~~~~ ~   ~ ~    ~  |         Birmingham, B29 6SS, UK
  131. ###LL  LL\\   (Brian McCauley)   |   ICBM: 52.5N 1.9W
  132.