home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
- From: tmb@arolla.idiap.ch (Thomas M. Breuel)
- Newsgroups: comp.lang.c++
- Subject: Re: destruction of temporaries
- Message-ID: <TMB.92Aug20142610@arolla.idiap.ch>
- Date: 20 Aug 92 18:26:10 GMT
- References: <BszApE.49v@world.std.com> <1992Aug17.073500.24115@ericsson.se>
- <23466@alice.att.com> <3920@starlab.UUCP> <23487@alice.att.com>
- Sender: news@ai.mit.edu
- Reply-To: tmb@idiap.ch
- Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
- Perceptive)
- Lines: 39
- In-reply-to: ark@alice.att.com's message of 19 Aug 92 18:22:44 GMT
-
- In article <23487@alice.att.com> ark@alice.att.com (Andrew Koenig) writes:
-
- Destruction at end of block is far from a hopeless approach, but I
- think it loses on balance. Indeed, there's something to be said for
- almost every approach, which is why this is a knotty problem.
-
- Let me put in another vote for leaving things the way they are.
-
- People want temporaries to live longer so that they can write
- certain expression that involve returning pointers or references
- to (the contents of) temporaries.
-
- However, delaying the destruction of temporaries (say, to the end of
- an expression) fixes nothing. It makes a slightly larger set of
- programs acceptable, but you still have to know in detail whether some
- object has been derived in one way or another from a temporary. For
- example, "char *p = s+t;" still leaves "p" undefined (if s and t are
- Strings).
-
- The price you pay is that of making another set of programs use
- (sometimes considerably) more memory. This is important in particular
- for scientific applications, which can use many large temporaries
- during a single expression. Such expressions (usually already tricky
- the way they are) would be nearly impossible to rewrite by hand in
- order to minimize memory use.
-
- Maybe the ANSI C++ committee will accept this change. I think it
- would be a big mistake. I'm generally for features that make life
- easier for the programmer, but the price for this one is simply too
- high.
-
- You can avoid unexpected behavior involving temporaries by programming
- defensively. In particular, this means: do _not_ return a reference or
- pointer to an explicit or implicit argument or its contents (*this,
- call-by-value, call-by-reference). Furthermore, do _not_ define cast
- operators to pointer types in your classes (instead, make them normal
- member functions that you have to call explicitly).
-
- Thomas.
-