简体   繁体   English

无法将参数从“ SIZE_T *”转换为“ size_t *”-如何进行投射?

[英]cannot convert parameter from 'SIZE_T *' to 'size_t *' - how to cast?

On some Windows API call, I get: cannot convert argument 6 from 'size_t *' to 'SIZE_T *' . 在某些Windows API调用上,我得到: cannot convert argument 6 from 'size_t *' to 'SIZE_T *'

This answer tells me that SIZE_T and std::size_t are different types. 这个答案告诉我SIZE_Tstd::size_t是不同的类型。 I know that both describe the same thing, just by different SDKs (C compiler vs. Windows SDK), so I want to cast 我知道两者都通过不同的SDK(C编译器与Windows SDK)描述了同一件事,所以我想

reinterpret_cast<SIZE_T *>(&my_size_t_var)`

Is there a less-ugly way to do it? 有没有一个丑陋的方式做到这一点?

Edit: SIZE_T is (when compiling for 64-bit targets, at least): 编辑: SIZE_T是(至少为64位目标编译时):

typedef ULONG_PTR SIZE_T, *PSIZE_T; 
typedef unsigned __int64 ULONG_PTR, *PULONG_PTR; 

Edit2: I'm using Win32 compressions API, specifically Compress , which has a parameter _In_ SIZE_T UncompressedDataSize , which I want to call with some std::vector<byte>.size() . Edit2:我正在使用Win32压缩API,特别是Compress ,它具有一个_In_ SIZE_T UncompressedDataSize参数,我想使用一些std::vector<byte>.size()进行调用。

I would do this: 我会这样做:

static_assert(sizeof(size_t) == sizeof(SIZE_T), "Expect size_t to be same size as SIZE_T");

somewhere near your reinterpret_cast<SIZE_T*>(&my_size_t_var) . 您的reinterpret_cast<SIZE_T*>(&my_size_t_var)附近的某个地方。

This will ensure that they are compatible in size, which is the key point. 这将确保它们在大小上兼容,这是关键。

They are the same types on x86-64, but on x86(32 bits) they are not. 在x86-64上,它们是相同的类型,但是在x86(32位)上,它们不是相同的类型。 SIZE_T is unsigned long and size_t is unsigned int on x86 so you have to use either reinterpret_cast or C-style cast: (SIZE_T*)&my_size_t_var . SIZE_T在x86上是unsigned longsize_tunsigned int ,因此您必须使用reinterpret_cast或C样式(SIZE_T*)&my_size_t_var(SIZE_T*)&my_size_t_var Alternatively you can switch to x86-64. 或者,您可以切换到x86-64。 I'm afraid these are the only options you have in this case. 恐怕在这种情况下,这是您唯一的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM