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 

Foo(300) = Foo(500); Why does a compiler compile that?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Alex Vinokur
Guest





PostPosted: Sun Jan 30, 2005 9:48 am    Post subject: Foo(300) = Foo(500); Why does a compiler compile that? Reply with quote



Compiler GNU gpp.exe (GCC) 3.4.1

Foo(300) = Foo(500); // Foo(300) is const. Why does a compiler compile that?


------ foo.cpp ------
struct Foo
{
explicit Foo(int) {}

Foo& operator= (const Foo&)
{
return *this;
}
};

int main()
{
Foo var1(100);
const Foo var2(200);

var1 = Foo(500);
Foo(300) = Foo(500); // Foo(300) is const. Why does a compiler compile that?
// var2 = Foo(500); // A compiler doesn't compile that

return 0;
}
---------------------

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



Back to top
Ivan Vecerina
Guest





PostPosted: Sun Jan 30, 2005 9:57 am    Post subject: Re: Foo(300) = Foo(500); Why does a compiler compile that? Reply with quote



"Alex Vinokur" <alexvn (AT) big-foot (DOT) com> wrote

Quote:
Compiler GNU gpp.exe (GCC) 3.4.1

Foo(300) = Foo(500); // Foo(300) is const. Why does a compiler compile
that?

Foo(300) is not const - it is a temporary.
It is true that temporaries can be bound to a const reference only
(not to a non-const ref).
However, it is legal to call any member function on a temporary.
And the line above is equivalent to:
Foo(300).operator=( Foo(500) );

This is indeed a case where user-defined classes do not behave
like built-in types...


Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



Back to top
Alex Vinokur
Guest





PostPosted: Sun Jan 30, 2005 10:09 am    Post subject: Re: Foo(300) = Foo(500); Why does a compiler compile that? Reply with quote




"Ivan Vecerina" <INVALID_use_webform_instead (AT) vecerina (DOT) com> wrote

Quote:
"Alex Vinokur" <alexvn (AT) big-foot (DOT) com> wrote in message
news:363oriF4scb34U1 (AT) individual (DOT) net...
Compiler GNU gpp.exe (GCC) 3.4.1

Foo(300) = Foo(500); // Foo(300) is const. Why does a compiler compile
that?

Foo(300) is not const - it is a temporary.

----------------------------------------
Quote:
It is true that temporaries can be bound to a const reference only
(not to a non-const ref).
If 'Foo(300) = Foo(500);' is legal, why can temporaries be bound to a const reference only (not to a non-const ref)?

---------------------------------------

Quote:
However, it is legal to call any member function on a temporary.
And the line above is equivalent to:
Foo(300).operator=( Foo(500) );

This is indeed a case where user-defined classes do not behave
like built-in types...

[snip]


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn




Back to top
Ivan Vecerina
Guest





PostPosted: Sun Jan 30, 2005 10:54 am    Post subject: Re: Foo(300) = Foo(500); Why does a compiler compile that? Reply with quote

"Alex Vinokur" <alexvn (AT) big-foot (DOT) com> wrote

Quote:
"Ivan Vecerina" <INVALID_use_webform_instead (AT) vecerina (DOT) com> wrote in
message news:ctib29$n0u$1 (AT) news (DOT) hispeed.ch...
Foo(300) = Foo(500); // Foo(300) is const. Why does a compiler
compile
that?

Foo(300) is not const - it is a temporary.

----------------------------------------
It is true that temporaries can be bound to a const reference only
(not to a non-const ref).
If 'Foo(300) = Foo(500);' is legal, why can temporaries be bound
to a const reference only (not to a non-const ref)?

Probably to avoid programming errors such as:
copySomeMembers( myTargetVariable, getSomeTemporary() );
//woops: actually, the functions takes 1st source, 2nd destination:
// copySomeMembers( Foo const& src, Foo& dst );
Because temps can only be bound to a const reference, you cannot
accidentally pass a temporary for the output of a function.
I find this is a good thing, but some disagree.

Quote:
However, it is legal to call any member function on a temporary.
And the line above is equivalent to:
Foo(300).operator=( Foo(500) );
On the other hand, it is sometimes convenient (and somewhat less

error prone) to write something like:
getSomeTermporaryHandle().doSomething();
And there is no special treatment for assignment operators,
which is just another member function.

I think a balance had to be chosen between convenence
and safety.



Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



Back to top
Matthew Schaefer
Guest





PostPosted: Sun Jan 30, 2005 11:16 am    Post subject: Re: Foo(300) = Foo(500); Why does a compiler compile that? Reply with quote

I think the problem is the following:
1) Foo(300) is non-const.
2) your operator= function does nothing. It simply assigns the lhs back
to itself. It does not check that the members of lhs and rhs are equal.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) 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.