Boost logo

Boost :

Subject: Re: [boost] boost/pool/pool.hpp why reinterpret_cast instead ofstatic_cast?
From: Philippe Vaucher (philippe.vaucher_at_[hidden])
Date: 2009-03-13 08:12:59

> Can you expand on that?
> Casting from void* to T* (or X* to Y*) is precisely when I'd use
> reinterpret_cast (and, indeed, it's not much use for anything else).

It looks like this is a common missunderstanding. Basically, when you know
that the types involved will convert from one to the other, I think you
should use static_cast. I found some pages more or less explaining my point
better than I do, so I'll quote them :


   - *"The reinterpret_cast operator also allows any integral type to be
      converted into any pointer type and vice versa. Misuse of the
      reinterpret_cast operator can easily be unsafe. Unless the desired
      conversion is inherently low-level, you should use one of the other cast

      - "*The result of a reinterpret_cast cannot safely be used for
      anything other than being cast back to its original type. Other
uses are, at
      best, nonportable.*"


   - "*reinterpret_cast is the most dangerous cast, and should be used very
      sparingly. It turns one type directly into another - such as casting the
      value from one pointer to another, or storing a pointer in an int, or all
      sorts of other nasty things. Largely, the only guarantee you get with
      reinterpret_cast is that if you cast the result back to the
original type,
      you will get the same value. Other than that, you're on your own.
      reinterpret_cast cannot do all sorts of conversions; in fact it is
      relatively limited. It should almost never be used (even
interfacing with C
      code using void* can be done with static_cast).*"

What makes you think void* to T* should be done with reinterpret_cast? I am
interested to know.


Boost list run by bdawes at, gregod at, cpdaniel at, john at