Bug in egcs ?

Nathan Myers ncm@cygnus.com
Thu Apr 23 21:12:00 GMT 1998


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



More information about the Gcc mailing list