将这个ccast改为reinterpret_cast是安全的吗? [英] Is it safe to change this c-cast to a reinterpret_cast?

查看:187
本文介绍了将这个ccast改为reinterpret_cast是安全的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从正在处理的代码中删除c风格的转换,并且我只关心唯一的替代方法。



原始代码was:

  WPARAM param =(WPARAM)(GetDlgItem(IDC_WORKFLOW).m_hWnd); 
this-> PostMessage(WM_NEXTDLGCTL,param,TRUE);

如果我使用静态转型:

  WPARAM param = static_cast< WPARAM>(GetDlgItem(IDC_WORKFLOW).m_hWnd); 
this-> PostMessage(WM_NEXTDLGCTL,param,TRUE);

我得到错误'static_cast':无法从'HWND'转换为'WPARAM',因为在底层类型之间没有有效的转换。这给我留下了魔鬼的选项:

  WPARAM param = reinterpret_cast< WPARAM>(GetDlgItem(IDC_WORKFLOW).m_hWnd); 
this-> PostMessage(WM_NEXTDLGCTL,param,TRUE);

根据我的理解,如果static_cast不可能,这与constness无关, C-cast必须执行reinterpret_cast,这意味着底层代码必须回滚,这意味着这是安全的(注释中的第3点)。



这个转换在这个特定实例中是否安全,如何确认?这是安全的,因为 WPARAM / code>定义为:

  typedef UINT_PTR WPARAM; 

和_PTR后缀表示类型足够大以容纳指针。



HWND是:

  typedef HANDLE HWND; 

其中HANDLE是:

  typedef void * HANDLE; 

因此void *和UINT_PTR的大小总是相同的。如果你将它存储在64位的应用程序,并尝试读取32位的应用程序,那么你会陷入麻烦。如果你仍然坚持,如果这是安全的话,


你可以搜索Visual Studio源代码(在C:\Program Files(x86)\Microsoft Visual Studio 8 \文件夹),你会发现很多行与 reinterpret_cast< LPARAM> ..) reinterpret_cast< WPARAM>(...)


I'm trying to remove a c-style cast from some code I'm working on, and I have concerns about the only alternative.

The original code was:

WPARAM param = (WPARAM)(GetDlgItem(IDC_WORKFLOW).m_hWnd);
this->PostMessage(WM_NEXTDLGCTL, param, TRUE);

If I use a static cast:

WPARAM param = static_cast<WPARAM>(GetDlgItem(IDC_WORKFLOW).m_hWnd);
this->PostMessage(WM_NEXTDLGCTL, param, TRUE);

I get the error, 'static_cast' : cannot convert from 'HWND' to 'WPARAM', because there's no valid conversion between the underlying types. This leaves me with "the devil's option":

WPARAM param = reinterpret_cast<WPARAM>(GetDlgItem(IDC_WORKFLOW).m_hWnd);
this->PostMessage(WM_NEXTDLGCTL, param, TRUE);

As I understand it, if a static_cast is impossible, and this isn't related to constness, the C-cast must be doing a reinterpret_cast anyway, which means that the underlying code must be casting back, which means this is safe (point 3 in remarks). But I'd like to confirm this, before just changing the code.

Is this cast safe in this specific instance, and how do I confirm this? If not, what is the alternative?

解决方案

It is safe, because WPARAM is defined as:

typedef UINT_PTR            WPARAM;

and _PTR suffix means the type is big enough to hold a pointer.

while HWND is:

 typedef HANDLE HWND;

where HANDLE is:

typedef void *HANDLE;

so size of void* and UINT_PTR are always the same. If you would store it in 64bit application and try read in 32 bit application, then you would get in trouble.

if you are still concenred if that is safe to do such casts, you can search Visual Studio sources (in C:\Program Files (x86)\Microsoft Visual Studio 8\ folder), and you will find lots of lines with reinterpret_cast<LPARAM>(...) and reinterpret_cast<WPARAM>(...).

这篇关于将这个ccast改为reinterpret_cast是安全的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆