This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Temporary objects
- From: John Love-Jensen <eljay at adobe dot com>
- To: Aseem Rastogi <aseem at india dot tejasnetworks dot com>, MSX to GCC <gcc-help at gcc dot gnu dot org>
- Date: Mon, 18 Jun 2007 09:27:50 -0500
- Subject: Re: Temporary objects
Hi Aseem,
> Any ideas why [passing temporary objects as non-const reference] could be [an
error] ?
C++ can generate unexpected temporaries, which when passed into functions
that are declared to mutate the temporary that can result in extremely
difficult to diagnose bugs.
As such, temporaries are not allowed to be passed to in/out and out
parameters.
> If passing a temporary object as non-const reference is not allowed
> because making changes to it doesn't make sense, doesn't same thing
> apply to calling non-const member function on a temporary ?
No. It's not a const related issue. It's an issue related to temporaries,
and C++'s rather promiscuous conversions.
If C++ required explicit conversions, it wouldn't be a problem. But then
the code wouldn't be C++ as we have it today. And we'd have a different set
of problems (such as problems regarding code verbosity, legibility,
maintainability).
> Thanks in advance,
You're welcome.
Sincerely,
--Eljay