Re: Temporary objects

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux