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

Christian Fröbel
Mon Apr 10 21:22:00 GMT 2006

Hi John,

John Love-Jensen wrote:
> [...]
> The rationale is in the Design & Evolution of C++.

I'll have that soon.

> Synopsis:  it would be far too easy to accidentally use a temporary as an
> lvalue, especially since C++ is "helpful" by doing implicit conversions
> (which may create temporaries otherwise unbeknownst to the programmer).

Ahh, now I see. This really helps me understanding the design of C++ here.

> [...]
> One workaround to the language safety feature is to do what auto_ptr does.

Yes, but by doing so I start writing code for the machine again. I write code 
for other programmers to read it. And if you take a look at the auto_ptr 
implementation: it's really hard to read.

> A better workaround is to design the application so that it wouldn't need
> to use non-const references to temporaries.

I'd like to, as long as the code keeps readable. The auto_ptr approach is no 
option for me. In my case it's just about a bool attribute that need's to be 
changed. One solution could be to just make it mutable.


More information about the Gcc-help mailing list