C++ references in `extern "C"`

18 hours ago 1
ARTICLE AD BOX

I have a C++ function

void foo(quz::Bar &s);

I want to expose this as a C function. Originally I was doing this for FFI, and calling foo from another language (compiles to scheme). So I wrapped the above in

struct Bar; // opaque type extern "C" { void foo(Bar &s); }

No headers were required. This has worked until now, but now I want to compile the consuming language to C not scheme. At which point I noticed that references don't exist in C, so I'm asking why did the above compile, and is it safe to use references in extern "C"?

And finally, if references are fine, what do I write in my headers? If I use references, surely C won't accept that.

Read Entire Article