gpt4 book ai didi

c++ - 检查函数指针类型的调用约定

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:35:59 24 4
gpt4 key购买 nike

如何在编译时检查函数指针是否具有 __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

最佳答案

MSVC 有 C4440 compiler warning :

// 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),但它不适用于静态成员函数。

关于c++ - 检查函数指针类型的调用约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4836526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com