From: Dave Thompson <david.thompson1@worldnet.att.net>
Subject: Re: how can i allocate an objekt with initialization???
Date: Mon, 13 Dec 2004 06:38:17 GMT
Date: 2004-12-13T06:38:17+00:00 [thread overview]
Message-ID: <e4bqr0hceta53glsf02uvaifm7lfec5dte@4ax.com> (raw)
In-Reply-To: 1654535.Ng2VDZFboh@linux1.krischik.com
On Mon, 06 Dec 2004 14:34:13 +0100, Martin Krischik
<martin@krischik.com> 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
next prev parent reply other threads:[~2004-12-13 6:38 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-12-05 16:54 how can i allocate an objekt with initialization??? Thomas Bruns
2004-12-05 22:51 ` Stephen Leake
2004-12-06 8:52 ` Martin Krischik
2004-12-06 10:04 ` Ole-Hjalmar Kristensen
2004-12-06 11:49 ` Adrien Plisson
2004-12-06 13:34 ` Martin Krischik
2004-12-13 6:38 ` Dave Thompson [this message]
2004-12-13 11:11 ` Martin Krischik
2004-12-06 11:55 ` Martin Krischik
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox