检查指向函数类型的调用约定

Aby*_*byx 6 c++ calling-convention visual-c++

如何在编译时检查函数指针是否具有__stdcall调用约定?

就像是

void foo() {}

static_assert(is_stdcall<decltype(&foo)>::value, "foo() must be stdcall");
Run Code Online (Sandbox Code Playgroud)

或至少

must_be_stdcall<T>(); // compiler error or warning if not stdcall
Run Code Online (Sandbox Code Playgroud)

Aby*_*byx 5

MSVC有C4440编译器警告:

// library code

#pragma warning(push)
#pragma warning(error: 4440)
template<typename F> void must_be_stdcall(F*) { typedef F __stdcall* T; }
#pragma warning(pop)

// test code

void __stdcall stdcall_fn() {}
void __cdecl cdecl_fn() {}

int main()
{
    must_be_stdcall(&stdcall_fn); // OK
    must_be_stdcall(&cdecl_fn); // error
}
Run Code Online (Sandbox Code Playgroud)

这可能是typedef decltype(foo) __stdcall* T;其中foo的一个功能(注意,应该有foo,没有&foo),但它不与静态成员函数的工作.