home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!microsoft!hexnut!jimad
- From: jimad@microsoft.com (Jim Adcock)
- Subject: Re: destruction of temporaries
- Message-ID: <1992Aug25.182538.9117@microsoft.com>
- Date: 25 Aug 92 18:25:38 GMT
- Organization: Microsoft Corporation
- References: <23466@alice.att.com> <3920@starlab.UUCP> <23487@alice.att.com>
- Keywords: temporaries,destruction,statements,block,early
- Lines: 62
-
- In article <23487@alice.att.com> ark@alice.UUCP () writes:
- | 1. strict end of block destruction creates problems for programs
- | like this:
- |
- | {
- | loop:
- | // do something that creates a temporary here
- | if(cond) goto loop;
- | }
- |
- | If you'really serious about destroying temporaries at end of
- | block, that means you need a mechanism for allocating potentially
- | uncbounded stack space and keeping track of all the destructors
- | so as to be able to undo them all at the end. This can be a
- | real pain, especially if you are trying to compile to C.
-
- I don't think people mean to imply that lifetimes of unnamed temporaries ought
- to live longer than an equivalent named temporaries. Both should be subject
- to the same "implied block" rewriting rules in the face of loops or
- conditional expressions:
-
- {
- loop:
- { // implied
- // do something that creates a temporary here
- if(cond) goto loop;
- } // implied
- }
-
-
- I suggest that something like the following set of rules would in practice
- allow compiler implementations such that all existing conforming
- compiler/program pairs could continue to work:
-
- Rules in order of precedence:
-
- 1) Lifetimes of unnamed temporaries never exceed their surrounding blocks,
- including the implied rewriting blocks of loops and conditionals.
-
- 2) Named temporaries live to the end of their name's surrounding block.
-
- 3) Temporaries must live until after their last access or end of block --
- whichever comes first.
-
- 4) Compilers are allowed to destroy temporaries anytime in any order after
- their last access.
-
- ====
-
- The above rules shift the burden of temporaries to optimizing compilers.
- Niave implementations can simply wait to the end of block [where "block"
- includes the implied rewriting rules of loops and conditions]. Optimizing
- compilers are free to reclaim objects as soon as they are done being used.
- In some cases, this may be very hard for optimizing compilers to determine --
- in which case they are free to fall back on the naive scheme. In practice,
- in many cases an optimizing compiler can simply determine last access,
- and can optimize lifetimes well.
-
- Under these rules the only difference between named objects and unnamed
- objects is the destruction of named objects *must* occur at end of block,
- whereas for unnamed objects destruction can occur earlier.
-
-