TITLE: typedef and language linkages (Newsgroups: comp.std.c++, 4 Dec 98) GLASSBOROW: Francis Glassborow >>And one reason the change was made was exactly that an implementation of >>C and C++ from the same source with incompatible function pointers >>existed (and I believe there were good reasons for the incompatibility) OLSSON: Hans.Olsson@dna.lth.se (Hans Olsson) >So I've been told, but since you've said that you were responsible for >the change I was hoping that you could explain those reasons, i.e. >why the pointers to C++-functions and C-functions had to differ. >That two products (C and C++-compiler) from the same vendor used >(and maybe uses) different calling conventions is not in itself >sufficient, especially considering that some compilers can all by >themselves generate code with different calling conventions >(and name mangling, exception handling, etc.). CLAMAGE: Steve Clamage It is a sufficient reason. When you call a function directly, the declared language linkage tells the compiler which calling convention to use. Originally, pointers to functions did not carry any language linkage information. The compiler cannot tell from the use of the pointer what calling convention to use. Example: extern "C" int f1(int); extern "Fortran" int f2(int); extern "C++" int f3(int); typedef int (*fp)(int); // using old C++ rules int foo( fp f, int i ) { return f(i); } int main() { foo(f1); foo(f2); foo(f3); } Suppose the calling conventions for C, C++, and Fortran are different. Under the old rules there is no way that this program can work, and no help from the compiler to tell you what is wrong. Under the new rules, fp has a language linkage associated with it, and cannot point to a function declared with a different language linkage. The compiler tells you about any problems, and also will automatically use the correct linkage to call a function through a pointer. When one vendor supplies compilers for all supported languages, the calling conventions are likely to be compatible. But it is common to find incompatible C compilers (for example) on the same system. A third-party C++ vendor can't be compatible with both of them, and might choose to define different language linkages to let C++ programs work with either of the C compilers.