Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Anonymity & Object Lifetime

0 views
Skip to first unread message

Al

unread,
Jan 5, 2008, 2:52:38 AM1/5/08
to
Hi.

I'm wondering why simply naming an object changes its lifetime.

Foo foo(1);

// vs.

Foo(1);

I image the technical reason is that the latter is an expression instead
of a declaration. Is this correct? I would still like to learn what the
rationale is for this difference.

Why shouldn't anonymous objects live till the end of their lexical
scope? It usually doesn't make any difference, since the anonymous Foo
can't easily be referred to; but it does if the dtor has side effects.

Thanks!
-Al-

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Francis Glassborow

unread,
Jan 5, 2008, 10:49:45 AM1/5/08
to
Al wrote:
> Hi.
>
> I'm wondering why simply naming an object changes its lifetime.
>
> Foo foo(1);
>
> // vs.
>
> Foo(1);
>
> I image the technical reason is that the latter is an expression instead
> of a declaration. Is this correct? I would still like to learn what the
> rationale is for this difference.
>
> Why shouldn't anonymous objects live till the end of their lexical
> scope? It usually doesn't make any difference, since the anonymous Foo
> can't easily be referred to; but it does if the dtor has side effects.
>

Consider the case when Foo uses a lot of storage or critical resources
(such as file handles). Now consider what happens when you use Foo()
more than once in a lexical scope. Under present rules the compiler can
recycle the resources. In addition when ~Foo() is trivial or the
compiler can check what it is doing, the compiler may be able to
optimise away the dtor call for the first instance and just reuse that
for the second one.

Alex

unread,
Jan 5, 2008, 12:42:12 PM1/5/08
to
On Jan 5, 2:52 am, Al <t...@haik.us> wrote:

> Why shouldn't anonymous objects live till the end of their lexical
> scope? It usually doesn't make any difference, since the anonymous Foo
> can't easily be referred to; but it does if the dtor has side effects.

The main reason is performance. Imagine a scenario (e.g. scientific
computing) where large temporaries are being created and piled up
until the end of scope is reached.

There are other reasons - for detailed explanation, see Chapter 6.3.2.
in The Design and Evolution of C++ (and note that Stroustrup initially
reasoned the same way as you do).

In this context, it is also worth being familiar with the latest GotW
which explains how long do the references to temporaries live:
http://herbsutter.spaces.live.com/blog/cns!2D4327CC297151BB!378.entry

Alex

Otis Bricker

unread,
Jan 5, 2008, 12:42:14 PM1/5/08
to
Al <t...@haik.us> wrote in news:O6idnUyLSMt5a-
PanZ2dnUV...@comcast.com:

> Hi.
>
> I'm wondering why simply naming an object changes its lifetime.
>
> Foo foo(1);
>
> // vs.
>
> Foo(1);
>
> I image the technical reason is that the latter is an expression instead
> of a declaration. Is this correct? I would still like to learn what the
> rationale is for this difference.
>
> Why shouldn't anonymous objects live till the end of their lexical
> scope? It usually doesn't make any difference, since the anonymous Foo
> can't easily be referred to; but it does if the dtor has side effects.
>

It seems easy to provide a name is you want it to live to the end of its
scope but how would you do the following is you rule was applied?

myFoolisness(Foo(i));// I need the destructor to be called here

The following seems pretty ugly;

{
myFoolisness(Foo(i));// I need the destructor to be called here
}

Otis

0 new messages