Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check calling convention of pointer-to-function type

How to check at compile-time that function pointer has the __stdcall calling convention?

Something like

void foo() {}

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

or at least

must_be_stdcall<T>(); // compiler error or warning if not stdcall
like image 630
Abyx Avatar asked Jan 29 '11 11:01

Abyx


1 Answers

MSVC has the 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
}

It may be typedef decltype(foo) __stdcall* T; where foo is a function (note, that there should be foo, not &foo), but it doesn't works with static member functions.

like image 179
Abyx Avatar answered Sep 28 '22 01:09

Abyx