I have encountered a working (with XLC8 and MSFT9 compilers) piece of code, containing a C++ file with a function defined with C linkage and a reference argument. This bugs me, as references are C++ only. The function in question is called from C code, where it is declared as taking a pointer argument to the same type in place of the reference argument.
Simplified example:
C++ file:
extern "C" void f(int &i)
{
i++;
}
C file:
void f(int *);
int main()
{
int a = 2;
f(&a);
printf("%d\n", a); /* Prints 3 */
}
Now, the word on the street is that most C++ compilers, under the hood, implement references just like a pointer. Is it like that and just pure luck the reason this code works or does it say somewhere in the C++ specification what the result is when you define a function with a reference argument and C linkage? I haven’t been able to find any information on this.
My copy of n3000.pdf (from here), has this to say in section 7.5—Linkage specifications:
Since C and C++ are different languages, this means that you can’t rely on this "feature" of common compilers.
Stronger is note 5 in the same section (emphasis mine):
So, I would say that what you did is not guaranteed to work according to the standard, and the compiler is not required to print a diagnostic for the example you have given because the declarations are in different translation units.
FYI, it "works for me" with gcc and g++ version 4.2.1 on Snow Leopard.