This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Bug in egcs ?
- To: egcs at cygnus dot com
- Subject: Re: Bug in egcs ?
- From: Nathan Myers <ncm at cygnus dot com>
- Date: Thu, 23 Apr 1998 16:41:21 -0700
- Newsgroups: cygnus.egcs
- Organization: http://www.cantrip.org/
- References: <353F3C9D.EDC7B0D8.cygnus.egcs@cern.ch>
Laurent Deniau wrote:
> egcs doesn't complain on the following code:
>
> template <typename T>
> struct Object {
> Object&
> operator = (Object<T> const& e) {
> const_cast<T>(_e) = e;
> return *this;
> }
> T _e;
> };
>
> According to the CD2, we should have write :
>
> const_cast<T&>(_e) = e;
>
> in order to get reference to _e.
This code is wrong in any case.
No cast is needed, generally, to call T::operator= on a
const right-operand. (auto_ptr<> is a special case.)
In this case, the expression _e already has type T&, because
_e is itself not const, so the const_cast has no effect anyway.
I don't see how the compiler could be instrumented to detect
errors like this.
It might be reasonable for the compiler to warn about calling operator=
with an rvalue left-operand, and perhaps to warn about a cast that has
no effect.
Nathan Myers
ncm@cantrip.org