c++cpass-by-referenceabicdecl

How can a reference be present in a signature of a function callable from C code?


I'm a bit confused: I have a C++ API which is supposed to be called from C code and uses __cdecl in the function declarations.

There's a vtable with function pointers like this:

void (__cdecl *funptr) (const MyStruct& obj);

references are a C++ construct, are they not? How can there be a __cdecl with references?

And finally: is __cdecl equivalent to wrapping everything in an extern "C" statement? What about references in that case?

I'm a bit confused..


Solution

  • These are apples and oranges.

    __cdecl is a non-standard keyword used to describe one of the more common x86 ABI calling conventions (together with __stdcall) which specifies how variables are passed/stacked between caller and callee. It has nothing to do with C specifically - some historic Microsoft C compiler just used this calling convention, hence the name. Many programming languages can use this calling convention and similarly, C code doesn't have to use it.

    extern "C" just means that the code should be compiled "like C" by the C++ compiler, disabling various name mangling etc used internally by the C++ compiler. It's not necessarily related to compliant C, but could as well be used when sharing code between two different C++ compilers that may use different name mangling.

    Neither has anything to do with how references work. In C they will not compile, in C++ they will compile. The code you posted is C++.