home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!uunet.ca!frumious!pat
- From: pat@frumious.uucp (Patrick Smith)
- Subject: Re: Destruction of temporaries
- Message-ID: <1992Sep2.044937.653@frumious.uucp>
- Date: Wed, 2 Sep 1992 04:49:37 GMT
- Reply-To: uunet.ca!frumious!pat
- References: <23590@alice.att.com> <rmartin.715268992@thor> <23599@alice.att.com>
- Organization: None
- Lines: 65
-
- ark@alice.UUCP () writes:
- |In article <rmartin.715268992@thor> rmartin@thor.Rational.COM (Bob Martin) writes:
- |> These strategies should not supress the warnings. ANY time an address
- |> or reference to a temporary is taken, whether is is via implicit
- |> conversion, or as the argument of a member function, a warning should
- |> be issued. No exceptions.
- |
- |> This will flag every instance where a long lived alias to a temporary
- |> could be created.
- |
- |No, it will flag some instances. For example:
- |
- | struct A {
- | A& foo();
- | };
- |
- | A a();
- |
- | main()
- | {
- | A& b = a().foo();
- | }
- |
- |Should this give a warning or not? It is impossible to tell without seeing the
- |definition of A::foo.
-
- As I understand Bob's suggestion, the compiler should give a warning
- here, on the grounds that *this will be bound to a temporary when
- foo() is called. The point isn't that this is likely to cause a
- problem, just that it _might_ cause a problem. As I posted a few days
- ago, I think this would cause far too many undeserved warnings.
-
- [safe example deleted]
-
-
- |in which case there is no problem, or it might be
- |
- | A& A::foo() {
- | return *this;
- | }
- |
- |in which case there is. Without seeing it, the compiler can't tell.
- |
- |But looking at A::foo without seeing main, it's impossible to tell either.
- |Returning *this from a member function is completely innocent unless the
- |member happens to be called on a temporary object.
-
- Here Eric Smith's proposal, extended to member functions, would
- be handy. It would allow the programmer to write something like
-
- struct A {
- A& foo() no_temporaries;
- };
-
- if foo() shouldn't be called on a temporary.
-
-
- | --Andrew Koenig
- | ark@europa.att.com
-
-
- --
- Patrick Smith
- uunet.ca!frumious!pat
- pat%frumious.uucp@uunet.ca
-