This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]