Re: static_cast signed to unsigned

Discussion in 'C++' started by Siemel Naran, Apr 1, 2004.

  1. Siemel Naran

    Siemel Naran Guest

    "Kevin Goodsell" <> 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.
    Siemel Naran, Apr 1, 2004
    1. Advertisements

  2. 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.
    Kevin Goodsell, Apr 1, 2004
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GuineaPig
    Fraser Ross
    Nov 15, 2003
  2. bonham


    bonham, Jun 8, 2005, in forum: C++
    Dec 1, 2012
  3. Bo Peng
    Victor Bazarov
    Oct 20, 2006
  4. pozz
    Tim Rentsch
    Mar 20, 2011
  5. junyangzou

Share This Page