By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 837 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Alex Vinokur" <al****@big-foot.com> wrote in message
news:36*************@individual.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.
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
Jul 23 '05 #2

P: n/a

"Ivan Vecerina" <IN*************************@vecerina.com> wrote in message news:ct**********@news.hispeed.ch...
"Alex Vinokur" <al****@big-foot.com> wrote in message
news:36*************@individual.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.


---------------------------------------- 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)?
---------------------------------------
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

Jul 23 '05 #3

P: n/a
"Alex Vinokur" <al****@big-foot.com> wrote in message
news:36*************@individual.net...
"Ivan Vecerina" <IN*************************@vecerina.com> wrote in
message news:ct**********@news.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.
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
Jul 23 '05 #4

P: n/a
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.

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.