473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
4 1513
"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

"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Generic Usenet Account | last post by:
Compile the following snippet of code and run it. If the program spits out bat:bat instead of bat:zat, what would you say? Would you say that the compiler has a problem, or would you lay the...
12
by: prashna | last post by:
Hi all, I am new to C and I am working on some simple programs.I am getting the following warning which I am not able to find what it is, please let me know what is causing this warning.Also What...
18
by: lchian | last post by:
Hi, I have a vector of class Foo. When I do a push_back(), I expect stl to call the default constructor I wrote for Foo. But instead, stl makes up its own default that is initialized with...
8
by: Joseph Turian | last post by:
Some function requires a vector<const foo*> argument. How can I cast a vector<foo*> to vector<const foo*>? Thanks! Joseph
7
by: Mark Odell | last post by:
I'm running two different compilers against some hairy macros and one, gcc, doesn't like my token pasting result so much. It says, " "." and "foo" does not give a valid preprocessing token ". Some...
3
by: Nicolas Castagne | last post by:
Hi all, I have been wondering for a while why function hiding (in a derived class) exists in C++, e.g. why when writing class Base { void foo( int ) {} }; class Derived: public Base { void...
9
by: ziman137 | last post by:
Hi all, The results from following codes got me a bit confused. #include <stdio.h> #include <iostream> using namespace std; struct A {
12
by: Vincent RICHOMME | last post by:
Hi, I am currently implementing some basic classes from .NET into modern C++. And I would like to know if someone would know a non mutable string class.
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.