Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: static_cast signed to unsigned

Thread Tools

Re: static_cast signed to unsigned

Siemel Naran
Posts: n/a
"Kevin Goodsell" <(E-Mail Removed)> wrote in message

> > To make it safe, I think you can: make the function take pointer types

> > that Src is deduced to char rather than char*, use Andrei's trick to

> > that a conversion from Src to Dest exists.
> >
> > template<class Dest, class Src>
> > Dest * pointer_cast(Src * src)
> > {
> > typedef char[sizeof(Overload<Dest>::conversion(Src()))-1]
> > AssertConversion;
> > return static_cast<Dest *>(static_cast<void *>(src));
> > }
> >
> > where
> >
> > template <class Dest>
> > struct Overload
> > {
> > char (&conversion(...))[1];
> > char (&conversion(Dest))[2];
> > };

Josh's way is more succint.

> Would this actually be safe?
> Just so we're clear, the purpose that I had in mind for this cast
> operator is to allow conversion of a pointer of type Src* to Dest* if
> and only if that conversion is reasonable. How we define "reasonable"
> may be different, but what I had in mind was pointer conversions that
> are guaranteed to give well-defined results -- for example, Any* to
> void*, Any* to unsigned char*, maybe Any* to char*. Also T* to T* would
> probably be good to allow. Same with Derived* to Base*. But the main
> purpose I had in mind was for situations where you have a buffer that
> needs to be passed to a function, and that function expects a different
> type buffer (like char when you have unsigned char).

I don't think Any* to unsigned char * is reasonable. The buffer thing you
mention is one I use myself, and I used reinterpret_cast (there were public
functions for short&, bool&, double&, etc, which called a private function
to do the reinterpret_cast, so I think it's pretty safe). I guess the
newsgroup is the place to try new things .

> Anyway, isn't it a mistake to assume that the existence of a conversion
> from Src to Dest implies a conversion from Src* to Dest*? For example,
> wouldn't this allow int* to be converted to double*?

Right, it is incorrect. Can't think of the correct way, or if there is one,
short of specialization.

Reply With Quote
Kevin Goodsell
Posts: n/a
Siemel Naran wrote:

> I don't think Any* to unsigned char * is reasonable.

Forgive me if I'm incorrectly applying a C rule, but I believe the
standard allows inspection of any object as an array of unsigned char.
unsigned char has no trap representations (not the term used for it in
the C++ standard, but a convenient term nonetheless), so it can't give
undefined behavior just by "looking". Modifying the bit-sequence then
attempting to use it as the original type is a different matter, though.

But since you mentioned it, it occurs to me that doing this kind of
thing for a non-POD type might not be very useful, and probably not a
very good idea in most circumstances.

Even so, what I have in mind could replace reinterpret_cast for its most
commons uses, and should be safer than reinterpret_cast.

Of course you can just use reinterpret_cast instead... I'm just not
comfortable with it at all.

My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
static_cast<unsigned> bonham C++ 7 12-01-2012 04:22 PM
(int) -> (unsigned) -> (int) or (unsigned) -> (int) -> (unsigned):I'll loose something? pozz C Programming 12 03-20-2011 11:32 PM
static_cast<unsigned short)( -1 ) Well defined? Jim Langston C++ 10 11-22-2006 03:52 PM
Is static_cast<int>(static_cast<double>(a)) == a? Bo Peng C++ 11 10-20-2006 12:59 PM
static_cast<unsigned int> question... GuineaPig C++ 9 11-15-2003 12:02 PM