home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12585 < prev    next >
Encoding:
Text File  |  1992-08-19  |  2.4 KB  |  68 lines

  1. Path: sparky!uunet!mcsun!sunic!hagbard!loglule!jbn
  2. From: jbn@lulea.trab.se (Johan Bengtsson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: destruction of temporaries
  5. Message-ID: <4850@holden.lulea.trab.se>
  6. Date: 19 Aug 92 15:00:16 GMT
  7. References: <1992Aug17.233923.14077@microsoft.com>
  8. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  9. Lines: 56
  10. X-Newsreader: Tin 1.1 PL4
  11.  
  12. jimad@microsoft.com (Jim Adcock) writes:
  13. : In article <46140@sunquest.UUCP> bill@sunquest.UUCP (Bill Raves) writes:
  14. : |For example, Jim Adcock has suggested
  15. : |(<70784@microsoft.UUCP>) "NEVER rely on unnamed temporaries to do
  16. : |anything...", but this would seem to erode the expressiveness of the
  17. : |language: constructs like "foo(x + y)" and "foo()[i]" would no longer
  18. : |be permissible. 
  19. : When an unnamed temporary is assigned to a reference, then the unnamed
  20. : temporary is no longer unnamed.  I believe this should mean that the lifetime
  21. : of the temporary then becomes that of the reference, but its easy to find
  22. : lots of compilers that disagree with this.
  23.  
  24. I thought the ARM was explicit about binding a reference to a temporary,
  25. that the temporary may not be destroyed until the reference is?
  26.  
  27. Yes, there it is, on page 268.  This then means that "lots of
  28. compilers" are broken, no?
  29.  
  30. Neither g++ version 1.40.3 nor Sun C++ 2.1 seems to do this.
  31.  
  32. Below is a small program that tries to test this, for a simple
  33. case.  Are there compilers that produce output with this program?
  34.  
  35. //------------------------------------------------------------
  36. #include <stream.h>
  37.  
  38. class C { public:
  39.    int cookie;
  40.    C() { cookie = 12345; }
  41.    ~C() { cookie = 0; }
  42.    static C temp();
  43. };
  44.  
  45. C C::temp()
  46. { C c; return c; }
  47.  
  48. void f( const C& c )
  49. { if ( c.cookie != 12345 ) cerr << "The compiler is broken\n"; }
  50.  
  51. int main() {
  52.    f( C::temp() );  // Sun C++ 2.1 warns here, why?
  53.    const C& cr = C::temp();
  54.    f( cr );
  55.    return 0;
  56. }
  57. //------------------------------------------------------------
  58.  
  59. Anyone have an idea why Sun C++ 2.1 doesn't like "f( C::temp() );"?
  60. "warning:  initializer for non-const reference not an lvalue (anachronism)"
  61.  
  62. -- 
  63. --------------------------------------------------------------------------
  64. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  65. | jbn@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490              |
  66. --------------------------------------------------------------------------
  67.