Search code examples
c++visual-c++calling-convention

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

Solution

  • 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.