6

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

就像是

void foo() {}

static_assert(is_stdcall<decltype(&foo)>::value, "foo() must be stdcall");

或者至少

must_be_stdcall<T>(); // compiler error or warning if not stdcall
4

1 回答 1

6

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
}

它可能在typedef decltype(foo) __stdcall* T;哪里foo是一个函数(注意,应该有foo,而不是&foo),但它不适用于静态成员函数。

于 2011-01-29T11:54:02.510 回答