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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!uunet.ca!frumious!pat
  3. From: pat@frumious.uucp (Patrick Smith)
  4. Subject: Re: Destruction of temporaries
  5. Message-ID: <1992Sep2.044937.653@frumious.uucp>
  6. Date: Wed, 2 Sep 1992 04:49:37 GMT
  7. Reply-To: uunet.ca!frumious!pat
  8. References: <23590@alice.att.com> <rmartin.715268992@thor> <23599@alice.att.com>
  9. Organization: None
  10. Lines: 65
  11.  
  12. ark@alice.UUCP () writes:
  13. |In article <rmartin.715268992@thor> rmartin@thor.Rational.COM (Bob Martin) writes:
  14. |> These strategies should not supress the warnings.  ANY time an address
  15. |> or reference to a temporary is taken, whether is is via implicit
  16. |> conversion, or as the argument of a member function, a warning should
  17. |> be issued.  No exceptions.
  18. |
  19. |> This will flag every instance where a long lived alias to a temporary
  20. |> could be created.
  21. |
  22. |No, it will flag some instances.  For example:
  23. |
  24. |    struct A {
  25. |        A& foo();
  26. |    };
  27. |
  28. |    A a();
  29. |
  30. |    main()
  31. |    {
  32. |        A& b = a().foo();
  33. |    }
  34. |
  35. |Should this give a warning or not?  It is impossible to tell without seeing the
  36. |definition of A::foo.
  37.  
  38. As I understand Bob's suggestion, the compiler should give a warning
  39. here, on the grounds that *this will be bound to a temporary when
  40. foo() is called.  The point isn't that this is likely to cause a
  41. problem, just that it _might_ cause a problem.  As I posted a few days
  42. ago, I think this would cause far too many undeserved warnings.
  43.  
  44. [safe example deleted]
  45.  
  46.  
  47. |in which case there is no problem, or it might be
  48. |
  49. |    A& A::foo() {
  50. |        return *this;
  51. |    }
  52. |
  53. |in which case there is.  Without seeing it, the compiler can't tell.
  54. |
  55. |But looking at A::foo without seeing main, it's impossible to tell either.
  56. |Returning *this from a member function is completely innocent unless the
  57. |member happens to be called on a temporary object.
  58.  
  59. Here Eric Smith's proposal, extended to member functions, would
  60. be handy.  It would allow the programmer to write something like
  61.  
  62.    struct A {
  63.       A& foo() no_temporaries;
  64.    };
  65.  
  66. if foo() shouldn't be called on a temporary.
  67.  
  68.  
  69. |                --Andrew Koenig
  70. |                  ark@europa.att.com
  71.  
  72.  
  73. -- 
  74. Patrick Smith
  75. uunet.ca!frumious!pat
  76. pat%frumious.uucp@uunet.ca
  77.