警告 C4090:“正在初始化":不同的“__unaligned"限定符 [英] warning C4090: 'initializing': different '__unaligned' qualifiers
问题描述
void file_explore(std::wstring str) {
ITEMIDLIST *pIDL = ILCreateFromPath(str.c_str());
if ( NULL != pIDL ) {
SHOpenFolderAndSelectItems(pIDL , 0 , 0 , 0);
ILFree(pIDL);
} else {
std::wstring p = str.substr(0 , str.find_last_of('\\'));
ShellExecute(NULL , L"explore" , p.c_str() , NULL , NULL , SW_SHOWNORMAL);
}
}
以上编译 32 位时没有警告,但使用 64 位时我收到警告 c4090 但是文档:https://msdn.microsoft.com/en-us/library/k77bkb8d.aspx 声明这是交流错误,我将获得 C2440 for c++,但我正在使用 c++.
The above compiles without warning for 32bit but with 64bit I get warning c4090 however the docs: https://msdn.microsoft.com/en-us/library/k77bkb8d.aspx state that this is a c error and I will get C2440 for c++ yet I'm using c++.
抱怨的代码行是:
ITEMIDLIST *pIDL = ILCreateFromPath(str.c_str());
如何解决 64 位版本的这个问题?
How to fix this issue for 64bit builds?
推荐答案
LPITEMIDLIST
被定义为 typedef ITEMIDLIST __unaligned *LPITEMIDLIST
,所以接受结果为 ITEMIDLIST*
失去了 __unaligned
修饰符.我不确定这与 32 位和 64 位有何关系.
LPITEMIDLIST
is defined as typedef ITEMIDLIST __unaligned *LPITEMIDLIST
, so accepting the result as ITEMIDLIST *
loses that __unaligned
modifier. I am not sure how this relates to 32-bit versus 64-bit.
正如 Hans Passant 评论的那样,使用 typedef 解决了这个问题.就我而言,我使用的是 std::unique_ptr
,它需要基类型而不是指针,所以我需要一个非指针 typedef.因为它可能对偶然发现这个答案的人有切切的兴趣,所以我将介绍我如何使用 std::unique_ptr
和 ILCreateFromPath
,包括自定义删除者的乐趣:
As Hans Passant commented, using the typedef solves the issue. In my case, I'm using std::unique_ptr
, which wants the base type rather than the pointer, so I needed a non-pointer typedef. Because it might be of tangential interest for people stumbling upon this answer, I'll put in how I am making use of a std::unique_ptr
with ILCreateFromPath
, including the custom deleter for fun:
auto deleter = [](LPITEMIDLIST ptr) { ILFree(ptr); };
using itemidlistptr_t = std::unique_ptr<
std::remove_ptr_t<LPITEMIDLIST>, decltype(deleter)>;
auto dir = itemidlistptr_t(ILCreateFromPathW(folder.c_str()), deleter);
使用 std::remove_pointer_t
有点绕,但我喜欢这里而不是直接提到 __unaligned
自己.
Using std::remove_pointer_t
is a bit roundabout, but I like it here rather than mentioning __unaligned
myself directly.
这篇关于警告 C4090:“正在初始化":不同的“__unaligned"限定符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!