C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Confusion about "12.2 Temporary objects"

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Matthias Hofmann
Guest





PostPosted: Sat Oct 18, 2003 11:09 pm    Post subject: Confusion about "12.2 Temporary objects" Reply with quote



Hello!

I have studied "12.2 Temporary objects" for hours now and there are some
points that confuse the heck out of me. I'd be rather grateful if someone
could clarifiy things for me.

1. Question:

12.2/2 gives roughly the following example (I omitted certain things for
clarity)

class X {};

X f(X);

void g()
{
X a(1);
a = f(a);
}

and states that "On the other hand, the expression a=f(a) requires a
temporary for either the argument a or the result of f(a) to avoid undesired
aliasing of a."

What does this mean? What would be the problem of neither using a temporary
for the argument passing nor for the return value? The call to f() would
call X's copy constructor to copy "a". Afterwards, operator=() would be
called on "a", but this is done after it has been copied, so what can go
wrong?

2. Question:

12.2/4 States that "There are two contexts in which temporaries are
destroyed at a different point than the end of the fullexpression. The first
context is when an expression appears as an initializer for a declarator
defining an object. In that context, the temporary that holds the result of
the expression shall persist until the object's initialization is complete.
The object is initialized from a copy of the temporary; during this copying,
an implementation can call the copy constructor many times; the temporary is
destroyed after it has been copied, before or when the initialization
completes. If many temporaries are created by the evaluation of the
initializer, the temporaries are destroyed in reverse order of the
completion of their construction."

As you can see, it first states that "the temporary that holds the result of
the expression shall persist until the object's initialization is complete".
However, a few sentences later it states that "the temporary is destroyed
after it has been copied, before or when the initialization completes". So
how can it "persist until the initialization is complete" and at the same
time be "destroyed before the initialization completes"?

Furthermore, why is the object initialized from a copy of the temporary? And
why can the implementation "call the copy constructor many times"?

3. Question:

I wonder if there is a typo in 12.2/5, which says "The temporary to which
the reference is bound or the temporary that is the complete object TO a
subobject OF which the TEMPORARY is bound persists for the lifetime of the
reference except as specified below."

Should this be "The temporary to which the reference is bound or the
temporary that is the complete object OF a subobject TO which the REFERENCE
is bound persists for the lifetime of the reference except as specified
below."

I used upper-case letters for the parts I think need to be changed.

Best regards,

Matthias Hofmann



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ben Hutchings
Guest





PostPosted: Mon Oct 20, 2003 10:00 pm    Post subject: Re: Confusion about "12.2 Temporary objects" Reply with quote



In article <bmrjh2$2pl$1 (AT) news1 (DOT) nefonline.de>, Matthias Hofmann wrote:
Quote:
Hello!

I have studied "12.2 Temporary objects" for hours now and there are some
points that confuse the heck out of me. I'd be rather grateful if someone
could clarifiy things for me.

1. Question:
snip


I agree with your analysis. The note appears to be wrong. This has already
been reported as a potential defect:
<http://std.dkuug.dk/jtc1/sc22/wg21/docs/cwg_active.html#320>.

Quote:
2. Question:
snip


This has been reported and answered (apparently it's not a defect) at
<http://std.dkuug.dk/jtc1/sc22/wg21/docs/cwg_closed.html#117>.

Quote:
Furthermore, why is the object initialized from a copy of the temporary? And
why can the implementation "call the copy constructor many times"?

I don't know.

Quote:
3. Question:

I wonder if there is a typo in 12.2/5, which says "The temporary to which
the reference is bound or the temporary that is the complete object TO a
subobject OF which the TEMPORARY is bound persists for the lifetime of the
reference except as specified below."

Should this be "The temporary to which the reference is bound or the
temporary that is the complete object OF a subobject TO which the REFERENCE
is bound persists for the lifetime of the reference except as specified
below."

I used upper-case letters for the parts I think need to be changed.

That seems to make more sense. I suggest you post that to comp.std.c++.

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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.