How do I reinterpret an unsigned long (DWORD) as a signed long in C++?
Asked Answered
T

2

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?

Trickish answered 15/7, 2011 at 15:22 Comment(1)
see also: #2205853 and #4476682Lithopone
L
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.

Librarianship answered 15/7, 2011 at 15:24 Comment(3)
What do you mean by "excessively permissive"?Lithopone
there's slight mistake in your second paragraph where you say (change a to be ?) it just trails off.Lithopone
Good explanation, not too long and painful. Thanks.Chemisorb
S
9

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

Seward answered 15/7, 2011 at 15:23 Comment(4)
is that why it says invalid type conversion?Lithopone
@0A0D: Standard explicitly names what you can use reinterpret_cast on, integral type to an integral type is not part of that list.Seward
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?Lithopone
@0A0D: I don't know, really. You should ask someone more familiar with the standardese.Seward
L
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.

Librarianship answered 15/7, 2011 at 15:24 Comment(3)
What do you mean by "excessively permissive"?Lithopone
there's slight mistake in your second paragraph where you say (change a to be ?) it just trails off.Lithopone
Good explanation, not too long and painful. Thanks.Chemisorb

© 2022 - 2024 — McMap. All rights reserved.