home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!sunic!hagbard!loglule!jbn
- From: jbn@lulea.trab.se (Johan Bengtsson)
- Newsgroups: comp.lang.c++
- Subject: Re: destruction of temporaries
- Message-ID: <4850@holden.lulea.trab.se>
- Date: 19 Aug 92 15:00:16 GMT
- References: <1992Aug17.233923.14077@microsoft.com>
- Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
- Lines: 56
- X-Newsreader: Tin 1.1 PL4
-
- jimad@microsoft.com (Jim Adcock) writes:
- : In article <46140@sunquest.UUCP> bill@sunquest.UUCP (Bill Raves) writes:
- : |For example, Jim Adcock has suggested
- : |(<70784@microsoft.UUCP>) "NEVER rely on unnamed temporaries to do
- : |anything...", but this would seem to erode the expressiveness of the
- : |language: constructs like "foo(x + y)" and "foo()[i]" would no longer
- : |be permissible.
- :
- : When an unnamed temporary is assigned to a reference, then the unnamed
- : temporary is no longer unnamed. I believe this should mean that the lifetime
- : of the temporary then becomes that of the reference, but its easy to find
- : lots of compilers that disagree with this.
-
- I thought the ARM was explicit about binding a reference to a temporary,
- that the temporary may not be destroyed until the reference is?
-
- Yes, there it is, on page 268. This then means that "lots of
- compilers" are broken, no?
-
- Neither g++ version 1.40.3 nor Sun C++ 2.1 seems to do this.
-
- Below is a small program that tries to test this, for a simple
- case. Are there compilers that produce output with this program?
-
- //------------------------------------------------------------
- #include <stream.h>
-
- class C { public:
- int cookie;
- C() { cookie = 12345; }
- ~C() { cookie = 0; }
- static C temp();
- };
-
- C C::temp()
- { C c; return c; }
-
- void f( const C& c )
- { if ( c.cookie != 12345 ) cerr << "The compiler is broken\n"; }
-
- int main() {
- f( C::temp() ); // Sun C++ 2.1 warns here, why?
- const C& cr = C::temp();
- f( cr );
- return 0;
- }
- //------------------------------------------------------------
-
- Anyone have an idea why Sun C++ 2.1 doesn't like "f( C::temp() );"?
- "warning: initializer for non-const reference not an lvalue (anachronism)"
-
- --
- --------------------------------------------------------------------------
- | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden |
- | jbn@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490 |
- --------------------------------------------------------------------------
-