* Ian:
* Alf P. Steinbach:
* Ian:
* Victor Bazarov:
* Ian:
[..]
The calling code is irrelevant to the original question. I'm still
none the wiser as to why template functions can't be given extern "C"
linkage.
(a) It doesn't make sense since there is no way to use templates
from C -- it just doesn't have that mechanism. To learn more on
"why" certain things the way they are, post to comp.std.c++, that
is the "why" newsgroup. We here talk mostly "how".
A function template could be called indirectly through a function pointer.
You don't need 'extern "C"' to do that.
We're going round in circles here, you do to assign an instantiation of
a function template to a extern "C' function pointer.
Oh, sorry for jumping into the middle of a thread; I missed that bit of
context, if it was ever there.
Yes, it seems that's so.
But consider: to interface to C code everything about the function signature
is fixed, so all you have to do is to introduce an in-between 'extern "C"'
function calling your template code.
I've been doing the practical stuff since the early days of cfront
compilers, so I know quite a lot about what goes on under the hood.
That's why C++ uses name mangling. Which is probably why the standard
forbids function templates from having extern "C" linkage, as there is
no way for C to call a mangled function directly.
Right, you got it.
But it would still be nice to be able to assign an instantiation of a
function template to a extern "C' function pointer.
See above, although many compilers such as MSVC already allow this, as an
extension, and although I agree that it would be nice and not only for
template functions, but also for static member functions. The problem here
is that C linkage seems to have been intended to address two different
practical aspects of linkage: name mangling, and machine code level calling
convention. For pointers the former is irrelevant, but the latter is still
relevant. In MSVC the calling convention is specified via a language
extension, not via the C/C++ linkage. And so MSVC can allow such pointers.