我需要在`std::unique_ptr`删除器中使用`__stdcall`吗?

Ben*_*uch 0 c++ winapi stdcall unique-ptr visual-c++

我想通过以下方式管理 WinAPI 函数对std::unique_ptr

#include <memory>

int* __stdcall construct(){ return new int{5}; }
void __stdcall destruct(int* v){ delete v; }

int main() {
    using Ptr = std::unique_ptr<int, void(&)(int*)>;
    Ptr v(construct(), destruct);
}
Run Code Online (Sandbox Code Playgroud)

当我使用 MSVC 19.33 构建 64 位二进制文​​件时,代码可以正常工作。如果我构建 32 位二进制文​​件,则会出现错误。

example.cpp
<source>(8): error C2660: 'std::unique_ptr<int,void (__cdecl &)(int *)>::unique_ptr': function does not take 2 arguments
C:/data/msvc/14.33.31631/include\memory(3291): note: see declaration of 'std::unique_ptr<int,void (__cdecl &)(int *)>::unique_ptr'
Compiler returned: 2
Run Code Online (Sandbox Code Playgroud)

__stdcall如果我明确命名in ,它适用于两者std::unique_ptr<int, void(__stdcall&)(int*)>,所以我假设它是函数签名的一部分。

为什么 32 位和 64 位会有这种差异?这是编译器中的错误吗?

Öö *_*iib 5

通过微软文档

在 ARM 和 x64 处理器上,编译器接受并忽略 __stdcall;在 ARM 和 x64 体系结构上,按照惯例,参数尽可能在寄存器中传递,后续参数在堆栈上传递。

so__stdcall被 64 位编译器忽略,但仅对 32 位编译器有意义。