Re: reference to non-const temporary
johnchx2@yahoo.com wrote:
Ethan Eade wrote:
I'm curious -- why aren't temporaries allowed to be passed as non-const
references? It seems slightly arbitrary, since non-const methods can be
called on them anyway.
It is arbitrary. IIRC, the rule was added to the language because
programmers were getting unexpected results from code like:
void add_one( long& x ) { x = x + 1; }
Actually, I've seen this topic come up on this group a number of times,
and this same rationale being posted a number of times. But this
problem can only occur when there is an implicit conversion going on.
Why not just disable implicit conversion whenever initializing a
non-const ref with a temporary? That way an int temporary could be
assigned to int& but not long&
This solution seems a lot cleaner ... but since its so obvious and yet
not implemented I guess there has to be a reason. But I am curious to
know what that is ... dont see it posted that often :(
Samee
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]