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

John Love-Jensen
Mon Apr 10 12:04:00 GMT 2006

Hi Christian,
> Yes, I guessed so as well. But what's the reason for this decision?

The rationale is in the Design & Evolution of C++.

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

> Yes, mostly. And that this is forbidden imposes a problem in the rare cases
> where it does make sense. And in those cases where someone does not want to
> use const at all (like me).

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

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


More information about the Gcc-help mailing list