Search code examples
c++windowsvisual-c++comcalling-convention

Should I prefer "default" calling convention over __fastcall when I don't really care of the calling convention?


We have a huge C++ codebase with lots of COM objects. Each function exposed to COM must have __stdcall calling convention (usually STDMETHODCALLTYPE macro) and so we have lots of functions marked STDMETHODCALLTYPE.

Now I see a function that is not directly called through COM, but rather called only from within our C++ code and this function also has STDMETHODCALLTYPE macro in its signature. I'm completely sure that macro is useless there - no calls through COM to that function ever happen.

Should I drop the __stdcall so that it becomes a "default" calling convention function? How do I make such decisions?


Solution

  • The only reason why the COM stuff explicitly sets the calling convention is because it is used across DLL boundaries.
    So my advise would be to drop the explicit setting of the calling convention and set it by the compiler settings.
    In general:
    If the functions are exported as a DLL set a macro that defines the calling convention in the Headers. This prevents users from the DLL to use a wrong calling convention when linking to your DLL. Explicit overrides the compiler setting.
    Do not use any calling convection on local functions. Convention can be set by a compiler switch. If you decide to set one explicitly, do it on all Functions. Then you still have a central place to change the calling convention.
    Of course if it makes sense or you need some special calling convention e.g fastcall for optimization then you need to set explicitly too.