Re: non-const reference to temporary (was: non-const copy ctor)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Christian Fröbel writes:
 > Hi Brian,
 > 
 > Brian Budge wrote:
 > > Hi Christian -
 > >
 > > It makes no sense to pass a temporary to a function which expects a
 > > reference.  The reference means that it has a place in memory
 > > somewhere, and that place in memory isn't generated for your
 > > temporary.
 > 
 > Well, this is confusing. From my understanding also a temporary object has a 
 > place in memory somewhere. My guess is that this space is on the stack, but 
 > that I'm not sure of--and, it's not important.

C++ doesn't have any stack, and it doesn't have any registers.  By
thinking "under the hood" in this way you're confusing implementation
with langauge in an unhelpful way.  All that matters is how the
language is *defined*.  I suspect this unnecessary mixing of levels is
responsible for much of the confusion in this thread.

 > Let's look at another example in which I call a non-const method on a temprary 
 > object, which--to the contrary--works fine.
 > 
 > class C
 > {
 > public:
 > 	void method() {} // not const
 > };
 > 
 > int main()
 > {
 > 	C().method(); // why does that work then?
 > 	return 0;
 > }
 > 
 > So, altering a temporary makes sense to the compiler this time?

The reason non-const references to non-lvalues is not allowed is
explained in Section 3.7 of "The Design and Evolution of C++".

Andrew.


Stroustrup, "The Design and Evolution of C++", Addison-Wesley 1994,
ISBN 0-201-54330-3.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux