在以x86-64为目标的Windows上,我们有
__unaligned
说明符。
一
二
真讨厌。我想:(1)对它进行编译时测试;2)在编译时从说明符中删除说明符;3)当从函数调用中找到它时,将它丢弃。扔掉它碰巧需要
const_cast
,幸运的是,任何解决方案都不容易保存
const
-尼斯。
另外,考虑到
ITEMIDLIST
类型(p[c][u]idlist_absolute,p[c][u]idlist_relative…)在windows shell api中,使用指向指针的指针执行上述操作会很好。
一个人如何做到这一点?
这里有一个方便的代码示例:
int main(int, char**)
{
//test that aligned_cast works for non-const pointers
using AType = int __unaligned *;
int a_data = 1;
AType a = &a_data;
auto aa = aligned_cast<int *>(a);
//check that it works for const pointers
using BType = const int __unaligned *;
const int b_data = 2;
BType b = &b_data;
auto *bb = aligned_cast<const int *>(b);
//int *bbb = aligned_cast<int *>(b); <--won't compile, good
//check that remove_aligned really is doing its job
using CType = remove_aligned<AType>::type;
static_assert (is_aligned<CType>::value, "");
//auto aaa = aligned_cast<int **>(&a); <-- &a cannot be passed as a reference
return 0;
}
1)
未对齐的
是个奇怪的怪癖
的
那是在安腾时代之前留下的。它潜入了windows api,所以我们(在windows上)一直在使用它。我们基本上不用
ITEMIDLIST
当我们这样做的时候,我们一般
#define STRICT_TYPE_ITEMIDS
小心踩。问题是
reinterpret_cast
-这些类型是
难以置信地
烦人的(然后
康斯特卡斯特
我想摆脱
未对齐的
如果需要,指定符)。
2)编辑:如果你不相信
未对齐的
不再适用
以x86-64为目标时
,在godbolt上玩我的答案,看看它是否编译而不使用casting。具体来说,从测试线束更改以下行导致编译失败:
59 //auto aa = aligned_cast<int *>(a);
60 int *aa = a; //nope, a is int __unaligned *