* James Kanze:
I'm not sure what you mean here. I've never used any
non-portable language extensions with regards to linkage.
Hm. OK, lets agree on terminology first, that "linkage", for the
purpose of this discussion, includes not only external, internal and
none, but also what the standard calls "language linkage". Which is not
very well defined, but presumably is meant to include such things as
data representation, calling convention and name mangling (these last
two are mentioned by the standard), which in turn determines whether
type information is communicated across translation units.
Now let's say you're using some abstract binary interface (ABI) that
imposes a particular data representation, calling convention and name
mangling. This is very common. In Windows programming the most common
such ABI is named COM (Common Object Model or whatever current acronym).
The 'extern "C"' syntax only standardizes strings for general C and C++
language linkage. This is useless when there is more than one possible
combination of calling convention and name mangling for each language,
and it lumps together things that shouldn't be. So the compiler must
use non-standard strings and a non-standard naming scheme (hopefully not
a specific name for each combination!) if it is to use this syntax.
In practice, Windows compilers let the name mangling scheme be
determined by the language name (C or C++) and the calling convention,
and use de-facto standard language extensions for the calling convention.
If the 'extern' syntax had standardized a little more variation than
just C versus C++, in particular the three most common calling
conventions for free functions (or better, stack versus register
passing, and for stack, right-left versus left-right plus who cleans up,
for a total of five), and placed less emphasis on being a means to
specify language and more emphasis on being a means to specify calling
convention and name mangling, this mess could have been avoided.
And
I've used compilers where C and C++ used different calling
conventions.
I presume you don't mean the situation when compiling the C++ code with
switches to use calling convention 1, and the C code with the same
compiler with switches to use calling convention 2, or with some
unrelated C compiler?
Do you mind clarifying which compiler(s) this was?
For numerous reasons, compilers will continue to support broken
code.
No, on the contrary. The code is Right (TM), expressing what one needs.
The language, offering no formally correct way to do what's right and
needed and established practice (to have an 'extern "C"' type compatible
function that is not in the global namespace), is Broken (TM).
Even with Sun CC, it is just a warning (for now---the
message definitly indicates that it will become an error at some
time in the future). Still, there are definite reasons why
the standards committee added the linkage to the type. (I don't
think it was originally part of Stroustrup's intent, although
you'd have to ask him to be sure.)
Presumably, since it is part of the standard, other compilers
will add such checks in the future.
Let's hope they make the warning very optional...
Cheers,
- Alf