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 

Some stupid question

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





PostPosted: Wed Apr 20, 2005 9:13 am    Post subject: Some stupid question Reply with quote



Hi,

I'm seeing strange thing there, and I'm wondering if it is a compiler
error or a misunderstanding of the standard.

Here is an example of the issue :

class Object
{
//... Some member ...
public:
static Object CreateObject()
{
return Object();
}

Object & Add(int a)
{ // The adding code here (sorry no needed here)
printf("%dn", a);
}

Object() { printf("Constructorn"); }
virtual ~Object() { printf("Destructorn"); }
};

Then in the main:
{
//...
CreateObject().Add(3).Add(4);

printf("Out of mainn");
}

As far I understand the standard, I'm creating an unnamed object in
CreateObject line, and it should be destructed in the main
end-of-scope. So I'm expecting :
Constructor
3
4
Out of main
Destructor
as output.

However I get as output:
Constructor
3
4
Destructor
Out of main
Meaning that the destructor is called at the end of the CreateObject
line...

So is there something I'm missing ?


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

Back to top
Maciej Sobczak
Guest





PostPosted: Wed Apr 20, 2005 4:51 pm    Post subject: Re: Some stupid question Reply with quote



Cyril wrote:

Quote:
As far I understand the standard, I'm creating an unnamed object in
CreateObject line,

This is a temporary object.

Quote:
and it should be destructed in the main
end-of-scope.

It should be destructed at the end of the full expression that created it.

Quote:
I get as output:
Constructor
3
4
Destructor
Out of main
Meaning that the destructor is called at the end of the CreateObject
line...

Which is exactly what you should expect.

--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/

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


Back to top
lotusny78@yahoo.com
Guest





PostPosted: Wed Apr 20, 2005 4:52 pm    Post subject: Re: Some stupid question Reply with quote



Quote:
static Object CreateObject()
{
return Object();
}
[snip]
As far I understand the standard, I'm creating an unnamed object in
CreateObject...
//Here you tell the compiler to create an object and return by value.

It applies the return value optimization to skip the usual
copy-initialization and direct-initialize the return value temporary.
[snip]
Quote:
CreateObject().Add(3).Add(4); //***********************

...and it should be destructed in the main
end-of-scope.

Usually (in this case), compiler-created temporaries exist from the
time you create them to the end of the "full expression" (the largest
expression that they are a part of). In your example, that refers to
the whole line marked with the stars


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


Back to top
Thomas Maeder
Guest





PostPosted: Wed Apr 20, 2005 4:53 pm    Post subject: Re: Some stupid question Reply with quote

"Cyril" <mail.pourri (AT) laposte (DOT) net> writes:

Quote:
class Object
{
//... Some member ...
public:
static Object CreateObject()
{
return Object();
}

Object & Add(int a)
{ // The adding code here (sorry no needed here)
printf("%dn", a);
}

Object() { printf("Constructorn"); }
virtual ~Object() { printf("Destructorn"); }
};

Then in the main:
{
//...
CreateObject().Add(3).Add(4);

printf("Out of mainn");
}

As far I understand the standard, I'm creating an unnamed object in
CreateObject line, and it should be destructed in the main
end-of-scope. So I'm expecting :
Constructor
3
4
Out of main
Destructor
as output.

However I get as output:
Constructor
3
4
Destructor
Out of main
Meaning that the destructor is called at the end of the CreateObject
line...

So is there something I'm missing ?

Temporary objects are destructed at the end of the evaluation of the
"full expression" as part of which they are created.

In the above code, that is after Add(4) has returned, and before the
printf() expression two lines below.

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


Back to top
Alberto Barbati
Guest





PostPosted: Wed Apr 20, 2005 4:56 pm    Post subject: Re: Some stupid question Reply with quote

Cyril wrote:
Quote:

Then in the main:
{
//...
CreateObject().Add(3).Add(4);

printf("Out of mainn");
}

As far I understand the standard, I'm creating an unnamed object in
CreateObject line, and it should be destructed in the main
end-of-scope. So I'm expecting :
Constructor
3
4
Out of main
Destructor
as output.

However I get as output:
Constructor
3
4
Destructor
Out of main
Meaning that the destructor is called at the end of the CreateObject
line...

So is there something I'm missing ?


Yes, you are missing §12.2/3: "Temporary objects are destroyed as the
last step in evaluating the full-expression (1.9) that (lexically)
contains the point where they were created."

In your specific case, this means that the temporary object is destroyed
just before the ";" of the statement that constructed it, not at the end
of the scope of function main.

HTH,

Alberto

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


Back to top
Gary Labowitz
Guest





PostPosted: Wed Apr 20, 2005 8:55 pm    Post subject: Re: Some stupid question Reply with quote

"Cyril" <mail.pourri (AT) laposte (DOT) net> wrote


Quote:
I'm seeing strange thing there, and I'm wondering if it is a compiler
error or a misunderstanding of the standard.

Here is an example of the issue :

class Object
{
//... Some member ...
public:
static Object CreateObject()
{
return Object();
}

Object & Add(int a)
{ // The adding code here (sorry no needed here)
printf("%dn", a);
}

Object() { printf("Constructorn"); }
virtual ~Object() { printf("Destructorn"); }
};

Then in the main:
{
//...
CreateObject().Add(3).Add(4);

printf("Out of mainn");
}

As far I understand the standard, I'm creating an unnamed object in
CreateObject line, and it should be destructed in the main
end-of-scope. So I'm expecting :
Constructor
3
4
Out of main
Destructor
as output.

However I get as output:
Constructor
3
4
Destructor
Out of main
Meaning that the destructor is called at the end of the CreateObject
line...

So is there something I'm missing ?

I am sure the destructor is called in the exit coding of the function, when
the object goes out of focus. I'm not a printf kind of guy (cout for me) but
I would think there is buffering involved and the buffer doesn't get flushed
until the program itself is ending. It is probable that the buffer clean-up
is just happening after the exit code of the function.
--
Gary



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


Back to top
Antoun Kanawati
Guest





PostPosted: Wed Apr 20, 2005 9:11 pm    Post subject: Re: Some stupid question Reply with quote

Cyril wrote:

Quote:
I'm seeing strange thing there, and I'm wondering if it is a compiler
error or a misunderstanding of the standard.

Here is an example of the issue :

class Object
{
//... Some member ...
public:
static Object CreateObject()
{
return Object();
}

Object & Add(int a)
{ // The adding code here (sorry no needed here)
printf("%dn", a);
}

Object() { printf("Constructorn"); }
virtual ~Object() { printf("Destructorn"); }
};

Then in the main:
{
//...
CreateObject().Add(3).Add(4);

printf("Out of mainn");
}

As far I understand the standard, I'm creating an unnamed object in
CreateObject line, and it should be destructed in the main
end-of-scope. So I'm expecting :
Constructor
3
4
Out of main
Destructor
as output.

However I get as output:
Constructor
3
4
Destructor
Out of main
Meaning that the destructor is called at the end of the CreateObject
line...

I presume you omitted the "return *this" from the function Add while
posting; as presented, it is not clear how the code could do what it's
doing.

The observed behavior looks right. The end-of-scope is for named
variables. Unnamed temporaries have shorter lifespans.

Before the standard defined the lifetime of unnamed temporaries, the
usual problem was that they did not live long enough to allow for the
call-chain you have above.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

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


Back to top
beliavsky@aol.com
Guest





PostPosted: Thu Apr 21, 2005 8:48 am    Post subject: Re: Some stupid question Reply with quote

I suggest that the moderator reject any question with a meaningless
subject such as "some stupid question" or "newbie question", thus
forcing the poster to use a subject that conveys what the message is
about. A better subject for this thread would have been (for example)

"problem with destructor (newbie question)"


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

Back to top
Llewelly
Guest





PostPosted: Fri Apr 22, 2005 2:21 pm    Post subject: Re: Some stupid question Reply with quote

[email]beliavsky (AT) aol (DOT) com[/email] writes:

Quote:
I suggest that the moderator reject any question with a meaningless
subject such as "some stupid question" or "newbie question", thus
forcing the poster to use a subject that conveys what the message is
about. A better subject for this thread would have been (for example)

"problem with destructor (newbie question)"

Unfortunately, having difficulty determining the proper question to
ask (and therefor the subject) is part of being a newbie.

It is overwhelmingly common to see beginners who have a legitimate
problem, but have trouble expressing it. (Indeed, a beginner who
can express his/her problem can often find the answer without
resorting to advice from us.)


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


Back to top
Carlos Moreno
Guest





PostPosted: Sat Apr 23, 2005 5:14 pm    Post subject: Re: Some stupid question Reply with quote

Antoun Kanawati wrote:

Quote:
The observed behavior looks right. The end-of-scope is for named
variables. Unnamed temporaries have shorter lifespans.

Tiny nit pick: there's an exception to the last statement; if an
unnamed temporary is bound to a reference-to-const, then its lifespan
may be extended. Notice that this does not fit the "named variables"
category -- the *object* is still an unnamed temporary -- it's the
reference the one that is a named variable.


Carlos
--

[ 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.