TITLE: reinterpret_cast non-omnipotency (Newsgroups: comp.lang.c++.moderated, 3 Mar 98) COGEN: David Cogen > I want to reinterpret_cast from a void * to a bool. > > Or, I thought I did, but the compiler (gcc2.8) doesn't allow it. > > I thought reinterpret_cast could cast practically anything! CLAMAGE: clamage@Eng.Sun.COM (Steve Clamage) On the contrary. A reinterpret_cast is extremely limited in the casts it can perform. A reinterpret_cast can convert a void* to an integral type large enough to hold it, and the result is defined by the implementation. Ordinarily you expect to preserve the bit pattern of the pointer, or as much of it as possible. It is unlikely that a bool is large enough to hold a void*, and if it could, it is unlikely that you want to store the void* bit pattern in the bool. Thus, the compiler did you a favor by rejecting the cast. There is a standard conversion from any pointer type to bool; the result is false for a null pointer and true for any other value of pointer. The new-style cast appropriate for standard conversions is the static_cast. When you use an old-style cast in this example, you get a static_cast, which is why it worked. Typical uses of the new-style casts: static_cast: user-defined, standard, or implicit type conversions. dynamic_cast: navigation in a class hierarchy when static_cast is not appropriate. const_cast: remove const or volatile from a type. reinterpret_cast: non-portable conversion between pointer types, or between a pointer and an integer type.