5

I want to reinterpret an unsigned long (actually, a DWORD) as a signed long. I tried:

DWORD x;
long y = reinterpret_cast<signed long>(x);

However, VC++2010 intellisense tells me "Invalid type conversion". Why? How do I fix it?

Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
  • see also: http://stackoverflow.com/questions/2205353/why-doesnt-this-reinterpret-cast-compile and http://stackoverflow.com/questions/4476182/which-cast-to-use-static-cast-or-reinterpret-cast –  Jul 16 '11 at 21:09

2 Answers2

9

You don't need reinterpret_cast to convert unsigned type into a signed one, static_cast will do.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
  • 1
    is that why it says invalid type conversion? –  Jul 15 '11 at 15:26
  • 3
    @0A0D: Standard explicitly names what you can use `reinterpret_cast` on, integral type to an integral type is not part of that list. – Cat Plus Plus Jul 15 '11 at 15:48
  • looking into it again, you can reinterpret_cast short to int, so why is that OK and not unsigned long to signed long? Is it because it is long-to-long, instead of long-to-int, for instance? –  Jul 15 '11 at 21:20
  • @0A0D: I don't know, really. You should ask someone more familiar with the standardese. – Cat Plus Plus Jul 16 '11 at 10:10
8

try static_cast instead. VC generates an error if you try an excessively permissive cast (like using reinterpret_cast when static_cast or const_cast will suffice).

There are 5 types of casts in C++, each of which allows you to do more (grants more permissions). The least permissive casts are const casts (const_cast<int>(<const int>)) which allow you to change the const modifier. There are static casts (static_cast<int>)(<short>)) which allow you to perform type safe coersions (cast base to derived, for example).There are dynamic casts (dynamic_cast<derived_type>(base_type) that allow you to cast from one type to another if there is a legal conversion between the two (and that return null if there is no conversion). Finally, there are casts that allow conversion between unrelated types - reinterpret_cast reinterpret_cast<int>(<void *>) and C style cast (int)<void *>.

I don't have a good way of describing these different types of casts, so I describe them as "more permissive" because each of them allows you to do more.

VC warns you if you are using a reinterpret cast when one of the other cast types would be more appropriate to achieve your goal. C style casts don't have a similar warning for backwards compatibility.

Larry Osterman
  • 16,086
  • 32
  • 60