This is the mail archive of the gcc@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]

Re: Bug in egcs ?


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


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