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?
Calling conventions specify how arguments are passed to a function, how return values are passed back out of a function, how the function is called, and how the function manages the stack and its stack frame. In short, the calling convention specifies how a function call in C or C++ is converted into assembly language.
Since it typically saves at least four memory accesses, yes it is generally faster.
Asking for help, clarification, or responding to other answers. Making statements based on opinion; back them up with references or personal experience.
__cdecl is the default calling convention for C and C++ programs. Because the stack is cleaned up by the caller, it can do vararg functions.
My approach is to use the default compiler calling convention for internal code and to use a well-defined explicitly stated calling convention for any methods which are exported across a module boundary.
The default calling convention for most compilers makes good use of registers for performance reasons so there are advantages to using it where appropriate. It also makes your code easier on the eye since you don't need to specify the convention to get the default.
For exported functions you clearly need to specify the convention. If you are making a library that you anticipate will be called from languages other than C or C++ it would be conventional to use stdcall. If you only expect C or C++ clients then cdecl is probably the most common convention.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With