why can't a template function have extern "C" linkage?

I

Ian

Alf said:
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.

This looks to be a good idea, unfortunately Sun's compiler is rather
pedantic in this area (maybe something to do the the author being the
C++ standard committee chairman!). I suppose I could disable the
warning and get on with life.

Cheers,

Ian
 
R

Ron Natalie

Ian said:
The title says it all.

I can see the case where a function is to be called directly from C, the
name mangling will stuff this up.

But I can't see a reason why a template function can't be given extern
"C" linkage where it is to be assigned to a C function pointer.

Ian
C has no concept of multiple objects with the same name. It can't
even deal with simple function overloads, let alone templates.
 
M

Me

Ian said:
The title says it all.

I can't really tell from reading the rest of this thread what problem
you're trying to solve, but I'm assuming you have an pointer to an
extern "C" function and you want to assign a template function to it.
The portable way to do this is create an extern "C" wrapper function
around an instance of the template and take the address of it, but a
solution that works in practice is to just reinterpret_cast it to the
extern "C"'d type. I'd probably go with the wrapper function way though
since I don't like reinterpret_casts in my code.
I can see the case where a function is to be called directly from C, the
name mangling will stuff this up.

But I can't see a reason why a template function can't be given extern
"C" linkage where it is to be assigned to a C function pointer.

7.5/6 "At most one function with a particular name can have C language
linkage. Two declarations for a function with C language linkage with
the same function name (ignoring the namespace names that qualify it)
that appear in different namespace scopes refer to the same function."

Since a template function has one name but can be parameterized to
refer to different functions, this obviously can't work. It looks like
this shouldn't be a problem for template functions with internal
linkage but the standard doesn't make a special case to allow for that.
comp.std.c++ is a better place to discuss that though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top