home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!secapl!Cookie!frank
- From: frank@Cookie.secapl.com (Frank Adams)
- Subject: Re: destruction of temporaries
- Message-ID: <1992Sep02.225757.27678@Cookie.secapl.com>
- Date: Wed, 02 Sep 1992 22:57:57 GMT
- References: <1992Aug25.175751.8117@microsoft.com> <1992Aug28.221841.149034@Cookie.secapl.com> <23585@alice.att.com>
- Organization: Security APL, Inc.
- Lines: 30
-
- In article <23585@alice.att.com> ark@alice.UUCP () writes:
- >In article <1992Aug28.221841.149034@Cookie.secapl.com> frank@Cookie.secapl.com (Frank Adams) writes:
- >
- >> I don't claim to have a complete solution to the problem. However, for the
- >> specific case c ? a+b : a where a+b creates a temporary, I would be
- >> tempted (as a compiler writer) to always return a temporary: a+b if c is
- >> true, and a copy of a otherwise. This solves the problem of not knowing
- >> whether a temporary has been created.
- >
- >I'm afraid it doesn't solve the problem. For example, suppose `a' is of type
- >const char*, `b' is of type String, and String has one of those problematic
- >conversions to const char*. Now, let's modify your example a tiny bit and
- >look at c?(const char*)(a+b):a.
- >
- >This expression always returns a value of type const char*, but that's not
- >where the problem likes: the problem is in the temporary of type String
- >yielded by the subexpression a+b. The value of (const char*)(a+b) is valid
- >only so long as that temporary stays around, so any games you play with
- >the const char* values themselves won't affect that.
-
- The problem in this case is the (const char *) conversion, not the ?:
- expression. You would have essentially the same problem with only the
- conversion. Remember, I didn't claim to be solving the whole problem, just
- ?:.
-
- I consider a "conversion" (or any other function) which returns a pointer to
- an internally-allocated object to be suspect, anyhow; except in the case
- where the object returned is of a class with its own memory management. (On
- the other hand, I don't off hand have any good alternative for the cases
- where you want to use it.)
-