home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13141 < prev    next >
Encoding:
Text File  |  1992-09-01  |  1.7 KB  |  58 lines

  1. Path: sparky!uunet!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!att!allegra!alice!ark
  2. From: ark@alice.att.com (Andrew Koenig)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Destruction of temporaries
  5. Message-ID: <23599@alice.att.com>
  6. Date: 1 Sep 92 12:51:55 GMT
  7. Article-I.D.: alice.23599
  8. 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>
  9. Reply-To: ark@alice.UUCP ()
  10. Organization: AT&T Bell Laboratories, Murray Hill NJ
  11. Lines: 45
  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?  It is impossible to tell without seeing the
  37. definition of A::foo.  For instance, it might be:
  38.  
  39.     A& A::foo() {
  40.         static A aa;
  41.         return aa;
  42.     }
  43.  
  44. in which case there is no problem, or it might be
  45.  
  46.     A& A::foo() {
  47.         return *this;
  48.     }
  49.  
  50. in which case there is.  Without seeing it, the compiler can't tell.
  51.  
  52. But looking at A::foo without seeing main, it's impossible to tell either.
  53. Returning *this from a member function is completely innocent unless the
  54. member happens to be called on a temporary object.
  55. -- 
  56.                 --Andrew Koenig
  57.                   ark@europa.att.com
  58.