Topic: Problem with reinterpret_cast on integers
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/02/24 Raw View
Uwe Tantz wrote:
The problem appear in the subject: reinterpret cast are for
pointers cast (and to and from integers).
To cast betwen numeric types you should use a
convert/implicit_cast or if you cast to an enum.
> I have problems casting away sign-ness of chars that I do not understand.
> I apologize if that is just a bug in Microsofts compiler which I am using.
It's not.
> static const int Table[]={ ... compare value for each char ... };
>
> bool my_less(const string& str1, const string& str2)
> {
> for (int i=0; i<str1.length(); ++i)
> if(str1[i]!=str2[i])
> return Table[str1[i]]<Table[str2[i]];
> return false;
> }
>
> unfortunately microsoft uses signed chars and diacritics are in negative
> range, so I changed line 7:
>
> return Table[reinterpret_cast<const unsigned char>(str1[i])] <
> Table[reinterpret_cast<const unsigned char>(str2[i])];
>
> The compiler refuses this: "Invalid cast"
>
> (BTW: using *reinterpret_cast<const unsigned char*>(&str1[i])] works! But
> looks disgusting)
If you prefer, use: reinterpret_cast<const unsigned char&>(str1[i])
which is equivalent and shorter and yes it should be accepted
by the compiler.
It's not only disgusting, the use of the value (read or write)
is undefined (but will in most cases work since alignement
restriction of T = align. restrict. of unsigned T).
I have read in the thread that reinterpret_cast can't add
a const qualifier; this is simply wrong, so the constness is
not an issue here.
--
Valentin Bonnard
mailto:bonnardv@pratique.fr
http://www.pratique.fr/~bonnardv (Informations sur le C++ en Francais)
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Uwe Tantz <tau04037@rphc1.physik.uni-regensburg.de>
Date: 1997/02/19 Raw View
I have problems casting away sign-ness of chars that I do not understand.
I apologize if that is just a bug in Microsofts compiler which I am using.
I have to compare two strings but in an unusual manner:
german diacritic letter 'Ae' like 'A', 'Oe' like 'O', 'Sz' like 'S' and
so on. I wrote this code:
static const int Table[]={ ... compare value for each char ... };
bool my_less(const string& str1, const string& str2)
{
for (int i=0; i<str1.length(); ++i)
if(str1[i]!=str2[i])
return Table[str1[i]]<Table[str2[i]];
return false;
}
unfortunately microsoft uses signed chars and diacritics are in negative
range, so I changed line 7:
return Table[reinterpret_cast<const unsigned char>(str1[i])] <
Table[reinterpret_cast<const unsigned char>(str2[i])];
The compiler refuses this: "Invalid cast"
(BTW: using *reinterpret_cast<const unsigned char*>(&str1[i])] works! But
looks disgusting)
Any ideas, why?
Thank you
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Larry Brasfield" <SpamGuard_LarryBr@microsoft.com>
Date: 1997/02/20 Raw View
Uwe Tantz <tau04037@rphc1.physik.uni-regensburg.de> wrote in article
<Pine.SOL.3.90.970219110132.27930A-100000@rphc2.physik.uni-regensburg.de
>...
> I have problems casting away sign-ness of chars that I do not understand.
> I apologize if that is just a bug in Microsofts compiler which I am using.
I think it is a bug in MSVC 4. Version 5 does better.
But for a cleaner solution now, see below.
> I have to compare two strings but in an unusual manner:
> german diacritic letter 'Ae' like 'A', 'Oe' like 'O', 'Sz' like 'S' and
> so on. I wrote this code:
>
> static const int Table[]={ ... compare value for each char ... };
>
> bool my_less(const string& str1, const string& str2)
> {
> for (int i=0; i<str1.length(); ++i)
> if(str1[i]!=str2[i])
> return Table[str1[i]]<Table[str2[i]];
> return false;
> }
>
> unfortunately microsoft uses signed chars and diacritics are in negative
> range, so I changed line 7:
>
> return Table[reinterpret_cast<const unsigned char>(str1[i])] <
> Table[reinterpret_cast<const unsigned char>(str2[i])];
>
> The compiler refuses this: "Invalid cast"
It should have taken your reinterpret_cast. But why not use
a static_cast instead? (BTW, that will work with MSVC 4, and
is considerably less ugly than the pointer type pun below.)
The static_cast is meant for just this sort of conversion.
And by using it, anybody whose hackles rise upon spotting a
reinterpret_cast will be able to relax a little more.
> (BTW: using *reinterpret_cast<const unsigned char*>(&str1[i])] works! But
> looks disgusting)
--
Larry Brasfield
The opinions expressed herein are mine, and are not
intended to reflect the views of any other entity.
(For an e-mail reply, remove "SpamGuard_".)
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Branko Cibej <branko.cibej@hermes.si>
Date: 1997/02/20 Raw View
Uwe Tantz wrote:
> unfortunately microsoft uses signed chars and diacritics are in negative
> range, so I changed line 7:
>
> return Table[reinterpret_cast<const unsigned char>(str1[i])] <
> Table[reinterpret_cast<const unsigned char>(str2[i])];
>
> The compiler refuses this: "Invalid cast"
Since you're casting from a 'const char' to a 'const unsigned char',
you should use a static cast:
return Table[static_cast<const unsigned char>(str1[i])] <
Table[static_cast<const unsigned char>(str2[i])];
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: maurice s barnum <pixi@bloodbath.slaughter.com>
Date: 1997/02/20 Raw View
Uwe Tantz <tau04037@rphc1.physik.uni-regensburg.de> writes:
> bool my_less(const string& str1, const string& str2)
> {
...
> return Table[reinterpret_cast<const unsigned char>(str1[i])] <
> Table[reinterpret_cast<const unsigned char>(str2[i])];
>
> The compiler refuses this: "Invalid cast"
whether this is a bug or not depends upon if string's const index
operator returns a const char or not. if it does return a const char
(kind of silly), the the cast is fine; if it doesn't, the cast is in
error since reinterpret_cast<> is not allowed to change the const
qualification of an expression.
by the way, i believe static_cast<> would be more appropriate here,
since you are explicitly asking the compiler to make a conversion that
could be done implicitly without a cast. reinterpret_cast<> should be
reserved for those cases where it is truly needed (such as casting
between integers and pointers or between incompatible pointer types).
--xmsb
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: jpotter@falcon.lhup.edu (John E. Potter)
Date: 1997/02/21 Raw View
Uwe Tantz (tau04037@rphc1.physik.uni-regensburg.de) wrote:
[ snip ]
: unfortunately microsoft uses signed chars and diacritics are in negative
: range, so I changed line 7:
: return Table[reinterpret_cast<const unsigned char>(str1[i])] <
: Table[reinterpret_cast<const unsigned char>(str2[i])];
: The compiler refuses this: "Invalid cast"
: (BTW: using *reinterpret_cast<const unsigned char*>(&str1[i])] works! But
: looks disgusting)
The compiler is correct. reinterpret_cast has specific uses which all
involve a pointer of some type. That's why the disgusting one is valid.
In this case, you have a well defined standard conversion from char to
unsigned char. Standard conversions are explicetly done using
static_cast. Replacing reinterpret with static in the above should be
less disgusting and even valid.
John
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]