This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


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

Re: Incorrect char extraction code


>>>>> "Rosen," == Rosen, Hyman <Hyman.Rosen@kbcfp.com> writes:

> std_istream.h has the following pair of templates:
>   template<class _Traits>
>     basic_istream<char, _Traits>&
operator> (basic_istream<char, _Traits>& __in, unsigned char& __c)
>     { return (__in >> static_cast<char>(__c)); }

>   template<class _Traits>
>     basic_istream<char, _Traits>&
operator> (basic_istream<char, _Traits>& __in, signed char& __c)
>     { return (__in >> static_cast<char>(__c)); }

> These are clearly wrong; the result of a static_cast<char> is not an
> lvalue, so code that tries an extraction into a signed or unsigned char
> shouldn't even compile. Those casts need to be reinterpret_cast<char &>,

Yes.

> and then you need to verify that you won't run afoul of gcc's alias
> detection.

If it did, that would be a bug in the compiler; anything can be aliased by
a char*.

Jason

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