home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!UB.com!igor!thor!rmartin
- From: rmartin@thor.Rational.COM (Bob Martin)
- Newsgroups: comp.lang.c++
- Subject: Re: Destruction of temporaries
- Message-ID: <rmartin.715267769@thor>
- Date: 31 Aug 92 13:29:29 GMT
- References: <rmartin.715004480@thor> <23583@alice.att.com> <rmartin.715101472@thor> <1992Aug29.184025.328@frumious.uucp>
- Sender: news@Rational.COM
- Lines: 58
-
- pat@frumious.uucp (Patrick Smith) writes:
-
- |rmartin@thor.Rational.COM (Bob Martin) writes:
- ||String& f(String& x) {return x;}
- ||
- ||String a,b;
- ||char* c = f(a+b);
- ||
- ||The above is more of a problem, becuase in order to provide the
- ||necessary warning, the compiler must warn whenever a reference to a
- ||temporary is taken. This would generate warnings even when there was
- ||no danger. But that's what warnings are all about.
-
- |My feeling is that if a compiler did this, the meaningful warnings
- |would be buried in a landslide of warnings for completely safe cases.
- |For example,
-
- | String operator+(const String& s1, const String& s2);
-
- | String a, b, c, d;
- | d = a + b + c;
-
- |In evaluating the second sum, the reference s1 is bound to the
- |temporary representing a+b. So the compiler would issue a warning,
- |according to your rule.
-
- Ideed. But I think the warning is still useful. The difficulty is
- that an alias to a temporary can refer to the temporary, long after it
- has been destroyed, regardless of which strategy is eventually used to
- destroy it. (With the exception of GC). Moreover, it is not
- realistic to suppose that the compiler can track the usage of such
- aliases in order to warn against misuse. But, the compiler *can* warn
- us whenever such aliases are created.
-
- Such warnings are useful in my opinion, because they point at
- potential problem areas. If there are lots of them produced, there
- are lots of potential problem areas to review.
-
- Some compilers may wish to deal with the deluge of warnings by
- implementing #pragmas which specifically inhibit warnings about the
- creation of aliases as arguments to specific functions. For example:
-
- #pragma Supress_Alias_Warning String::operator+(const String&, const String&)
-
- This could prevent the compiler from warning about aliases created
- when forming the arguments to functions which are known to be benign.
- The above #pragma would eliminate all the warnings from your example,
- but would still pass warnings about other aliases.
-
- This exposes the alias problem, and puts responsibility for it square
- on the programmer. It also allows the compiler to destroy temporaries
- at the earliest possible moment.
-
- --
- Robert Martin Training courses offered in:
- R. C. M. Consulting Object Oriented Analysis
- 2080 Cranbrook Rd. Object Oriented Design
- Green Oaks, Il 60048 (708) 918-1004 C++
-