Home > Enterprise >  Why can functions with different calling conventions still call each other?
Why can functions with different calling conventions still call each other?

Time:09-21

int __cdecl funcB(int a, int b) {

    return 0;
}

int __stdcall funcA(int a, int b) {

    return funcA(a, b);
}

I wrote this two functions and they have different calling conventions: __stdcall and __cdecl.

And my question is why MSVC didn't throw a compile error?

Because in my view two functions with different calling conventions can't call each other

If caller think callee should clean the stack, and callee think caller should clean the stack, and that's my problem

Any answers will be helpful

CodePudding user response:

Because in my view two functions with different calling conventions can't call each other

That's simply an incorrect view. A calling convention is just a set of rules for how arguments are handled across the call. The compiler generates instructions at each call site and within the body of the function that follow whichever convention the function is defined with.

If caller think callee should clean the stack, and callee think caller should clean the stack, and that's my problem

The problem you are thinking of is when the calling convention is omitted, and different translation units are compiled with different default conventions. The declarations in one TU are used in a manner incompatible with the definition in another TU.

  • Related