home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!sdl!adk
- From: adk@Warren.MENTORG.COM (Ajay Kamdar)
- Subject: Re: destruction of temporaries
- Message-ID: <1992Aug18.205211.16789@Warren.MENTORG.COM>
- Organization: Mentor Graphics Corp. - IC Group
- References: <1992Aug17.073500.24115@ericsson.se> <23466@alice.att.com> <1992Aug17.211508.6634@lucid.com>
- Date: Tue, 18 Aug 1992 20:52:11 GMT
- Lines: 67
-
- In article <1992Aug17.211508.6634@lucid.com> jss@lucid.com (Jerry Schwarz) writes:
- >In article <23466@alice.att.com>, ark@alice.att.com (Andrew Koenig) writes:
- >|> This treatment [end of statement] has two significant disadvantages:
- >|>
- >|> 1. If I break a statement up into several, it may
- >|> quietly change its meaning. Thus, for example, if
- >|> String::operator const char*() exists, and s and t are
- >|> of class String, then
- >|>
- >|> printf("%s\n", (const char*)(s+t));
- >|>
- >|> might work but
- >|>
- >|> const char* r = s + t;
- >|> printf("%s\n", r);
- >|>
- >|> might not.
- >
- >And I feel that I should be able to add a { } around an expression
- >statement without changing its meaning. In other words I ought
- >to be able to further transform the above to
- >
- > const char* r = "" ;
- > if ( 1 ) { r = s+t ; }
- > printf("%s\n", r)
- >
- >
- >Even "end of block" (the latest anyone has proposed) makes
- >this example undefined.
- >
-
- As Andrew Koenig and Jerry Schwarz point out, "end of statement" and
- "end of block" treatment of temporaries is not good enough.
-
- The ARM also mentions a "end of function" treatment, but goes on to reject
- that too. However, I am not convinced by the ARM's explanation for not
- adopting the "end of function" approach. On page 270, the ARM gives the
- following example and uses that example to reject this approach:
-
- const char* f()
- {
- String x("foo");
- String y("bar");
- return x + y; // still not guaranteed to work
- }
-
- However, note that I can put myself in exactly the same situation even
- without any temporaries being involved.
- const char *f()
- {
- String x("foobar");
- return x; // not guaranteed to work either
- }
-
- So why would the ARM consider the example with a temporary sufficient
- reason to reject the "end of function" approach? To me it appears that
- the "end of function" approach for temporaries would not cause any more
- difficulties or program bugs than those that can already occur even without
- temporaries. So why not reconsider that approach?
-
- - Ajay
-
- --
- I speak for none but myself.
-
- Ajay Kamdar Email : ajay_kamdar@mentorg.com
- Mentor Graphics, IC Group (Warren, NJ) Phone : (908) 580-0102
-