Article View: gnu.g++.bug
Article #6Re: temp instance in function arguments
From: Joe Buck
Date: Wed, 28 Oct 1992 06:13
Date: Wed, 28 Oct 1992 06:13
45 lines
2103 bytes
2103 bytes
In article <1992Oct28.013753.12311@nuscc.nus.sg> scstech@solomon.technet.sg (Fong Khai Yin) writes: >I think there is a bug in g++ 2.2.2 where an object is used as an argument to a function >that is expecting a char*. >Please correct me if this is incorrect. No, it's not a bug. Temporary objects may be deleted after their last use; however, cfront (and Sun C++) doesn't delete them until the end of the enclosing scope. g++ destroys temporaries fairly aggressively, which is legal according to the ARM but surprising when used with objects with (const char*) casts defined. >Is it possible to get a patch for this problem ??? No, because you would then port your code to yet another compiler and have the same problem. Your program is not portable. The only way according to the current rules to guarantee that a temporary lives until the end of a block is to give the temporary a name. Example: a typical String class, that defines an operator const char* function. Given the declarations extern "C" FILE * fopen(const char*,const char*); String foo(); the code FILE* fd = fopen(foo(),"r"); is unsafe, because the temporary object returned by foo() may be destroyed before fopen is called. The only portable way to write the above is something like String& tmp = foo(); // give the temporary a name FILE* fd = fopen(tmp,"r"); The ANSI C++ standardization committee is still debating the rules for destruction of temporaries. According to the present rules, both the cfront and the g++ behavior is legal. -- Joe Buck jbuck@ohm.berkeley.edu
Technical Details
Message-ID:
<1clb29INNrtu@agate.berkeley.edu>Path:
arc1-ca.pugleaf.net!sparky!uunet!convex!darwin.sura.net!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!forney.berkeley.edu!jbuckReferences:
<1992Oct28.013753.12311@nuscc.nus.sg>Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.bug Path: arc1-ca.pugleaf.net!sparky!uunet!convex!darwin.sura.net!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!forney.berkeley.edu!jbuck From: jbuck@forney.berkeley.edu (Joe Buck) Subject: Re: temp instance in function arguments Message-ID: <1clb29INNrtu@agate.berkeley.edu> Sender: gnulists@ai.mit.edu Organization: U. C. Berkeley References: <1992Oct28.013753.12311@nuscc.nus.sg> Distribution: gnu Date: Wed, 28 Oct 1992 06:13:29 GMT Approved: bug-g++@prep.ai.mit.edu Lines: 46