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: complex classes and temporary destruction.
- Message-ID: <rmartin.715267018@thor>
- Date: 31 Aug 92 13:16:58 GMT
- References: <MCGRANT.92Aug26232410@rascals.stanford.edu> <23563@alice.att.com> <64821@cup.portal.com> <23578@alice.att.com> <64878@cup.portal.com>
- Sender: news@Rational.COM
- Lines: 57
-
- Aurelius@cup.portal.com (Mark Christian Barnes) writes:
-
- |Andrew Koenig writes:
-
- ||
- ||char* I(char* x) { return x; }
- ||
- ||The function I (for identity) just returns its argument, but to make things
- ||difficult, it is compiled separately. Now, you would like
- ||
- ||char* p = s+t;
- ||
- ||to cause the temporary for s+t to stick around as long as p does,
- ||but what are you going to do about this?
- ||
- || char* p = I(s+t);
- ||
- ||There's no way to know what I() does, so what basis do you have for
- ||letting the lifetime of p affect that of s+t?
-
- | But Andrew, you have changed the statement completely. The
- |temporary 's+t' is no longer even being assigned to 'p'. It is
- |constructed as a parameter for function (or macro) 'I'. In this
- |case I would suggest that 's+t' is an auto variable with a scope
- |limited to 'I'.
-
- | I know that this means that your example will not work under
- |this interpretation. Well I don't think it should. The perpetual
- |problem of aliasing, to which pointers contribute, should not be
- |solved by the compiler. I feel that the programmer should pay
- |attention to what he/she is doing.
-
- But, the problem of when to "safely" destroy temporaries is all
- wrapped up in how to deal with aliases. And I disagree that the
- compiler should ignore the creation of an alias to a temporary. It
- should at least warn that such aliases are being created.
-
- | In your example, an expression is evaluated and passed
- |(on the stack) as the parameter to a function. The function
- |'I' returns another value of type pointer to char. There should
- |not be any assumption that the parameter and the return value
- |have anything in common. You, as the programmer, know that 'I'
- |returns the parameter. Therefore, you should also insure that
- |any parameters passed to 'I' are of the appropriate storage
- |class and scope for the uses that you intend.
-
- Yes, but an alias to the temporary was created before the function was
- called. The expression s+t created a temporary, and its address was
- taken (alias created) to be passed into the function. The compiler
- should issue a warning at this point.
-
-
- --
- 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++
-