From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,9fd97969641aa8c6 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!news.glorb.com!wn13feed!worldnet.att.net!bgtnsc05-news.ops.worldnet.att.net.POSTED!53ab2750!not-for-mail From: Dave Thompson Newsgroups: comp.lang.ada Subject: Re: how can i allocate an objekt with initialization??? Message-ID: References: <3459883.9H2ke8dEzz@linux1.krischik.com> <41b44663$0$9298$ba620e4c@news.skynet.be> <1654535.Ng2VDZFboh@linux1.krischik.com> X-Newsreader: Forte Agent 1.93/32.576 English (American) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Mon, 13 Dec 2004 06:38:17 GMT NNTP-Posting-Host: 12.76.17.223 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc05-news.ops.worldnet.att.net 1102919897 12.76.17.223 (Mon, 13 Dec 2004 06:38:17 GMT) NNTP-Posting-Date: Mon, 13 Dec 2004 06:38:17 GMT Organization: AT&T Worldnet Xref: g2news1.google.com comp.lang.ada:6913 Date: 2004-12-13T06:38:17+00:00 List-Id: On Mon, 06 Dec 2004 14:34:13 +0100, Martin Krischik wrote: > > Ole-Hjalmar Kristensen wrote: > > the corresponding C++ example should have been: > > a &my_object = b(1); > > I have seen this before and I am wondering if it is actualy valid. Because: > where is the result of the b(1) constructor call been stored? a& can only > hold a reference to retun value. In a temporary which persists as long as the reference, 12.2p5. In practice this means the stack frame is extended. Since C++ references cannot be reseated, and the type of an initializer is AFAICS always statically known, this isn't terribly valuable, but it does work. > Is this not a variation on the classic > mistake. > > int& > f () > { > auto int retval = 5; > > // do something > > return retval; > } > That's different. auto objects explicitly have lifetime of the block in which they are declared hence allocated. And since under the covers the reference is only a pointer, this returns a stale pointer. (Aside: auto is the default storage class where it applies, to local variables, so does not need to be and usually is not given explicitly. Especially since in C++ and C99 'implicit int' is gone and the type specifier must be present in the beginning part of a declaration. In fact there have been semiserious proposals to 'recycle' the keyword 'auto' to a new meaning since its existing one is so unnecessary.) - David.Thompson1 at worldnet.att.net