Hi,
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 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 info on this.