home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13201 < prev    next >
Encoding:
Internet Message Format  |  1992-09-02  |  2.1 KB

  1. Path: sparky!uunet!UB.com!igor!thor!rmartin
  2. From: rmartin@thor.Rational.COM (Bob Martin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Destruction of temporaries
  5. Message-ID: <rmartin.715444704@thor>
  6. Date: 2 Sep 92 14:38:24 GMT
  7. References: <MCGRANT.92Aug26232410@rascals.stanford.edu> <23563@alice.att.com> <rmartin.715004480@thor> <23583@alice.att.com> <rmartin.715101472@thor> <23590@alice.att.com> <rmartin.715268992@thor> <23599@alice.att.com>
  8. Sender: news@Rational.COM
  9. Lines: 50
  10.  
  11. ark@alice.att.com (Andrew Koenig) writes:
  12.  
  13. |In article <rmartin.715268992@thor> rmartin@thor.Rational.COM (Bob Martin) writes:
  14.  
  15. |> These strategies should not supress the warnings.  ANY time an address
  16. |> or reference to a temporary is taken, whether is is via implicit
  17. |> conversion, or as the argument of a member function, a warning should
  18. |> be issued.  No exceptions.
  19.  
  20. |> This will flag every instance where a long lived alias to a temporary
  21. |> could be created.
  22.  
  23. |No, it will flag some instances.  For example:
  24.  
  25. |    struct A {
  26. |        A& foo();
  27. |    };
  28.  
  29. |    A a();
  30.  
  31. |    main()
  32. |    {
  33. |        A& b = a().foo();
  34. |    }
  35.  
  36. |Should this give a warning or not? 
  37.  
  38. It should, because the temporary created by 'a()' must be converted to
  39. a pointer in order to be used in the method call as 'this'.  And, of
  40. course, this means that every time you invoke a method on a temporary,
  41. you will get a warning.
  42.  
  43. Issuing warnings every time a method is invoked on a temporary is very
  44. ugly.  #pragmas for selectively supressing such warnings are a
  45. solution, but not a very satisfying one.  Still, it is when a pointer
  46. or reference to a temporary is taken, that the risk of a long lived
  47. alias is created; and a warning about that risk seems appropriate.
  48.  
  49. In the absence of GC, or some other foolproof mechanisms for
  50. eliminating the risk of long lived aliases to temporaries, I think
  51. warnings are the only practical answer.  But I must admit, it's not a
  52. very good answer.  It's just better than silence.
  53.  
  54.  
  55.  
  56. --
  57. Robert Martin                        Training courses offered in:
  58. R. C. M. Consulting                       Object Oriented Analysis
  59. 2080 Cranbrook Rd.                        Object Oriented Design
  60. Green Oaks, Il 60048 (708) 918-1004       C++
  61.