 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ian Guest
|
Posted: Wed Jul 27, 2005 9:38 pm Post subject: why can't a template function have extern "C" linkage? |
|
|
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
|
|
| Back to top |
|
 |
Andre Kostur Guest
|
Posted: Wed Jul 27, 2005 9:50 pm Post subject: Re: why can't a template function have extern "C" linkage? |
|
|
Ian <noone (AT) nowhere (DOT) com> wrote in news:1122500290.491907@drone2-svc-
skyt.qsi.net.nz:
| Quote: | 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
|
Um... how would your C program (which doesn't know anything about
templates) include the header file where the template function is declared?
Keep in mind that a template function doesn't actually exist until it is
instantiated.... (not sure that "instantiated" is the right word....)
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Jul 27, 2005 10:23 pm Post subject: Re: why can't a template function have extern "C" linkage? |
|
|
Andre Kostur wrote:
| Quote: | [..]
Keep in mind that a template function doesn't actually exist until it is
instantiated.... (not sure that "instantiated" is the right word....)
|
It is the right word.
V
|
|
| Back to top |
|
 |
Ian Guest
|
Posted: Wed Jul 27, 2005 11:08 pm Post subject: Re: why can't a template function have extern "C" linkage? |
|
|
Andre Kostur wrote:
| Quote: | Ian <noone (AT) nowhere (DOT) com> wrote in news:1122500290.491907@drone2-svc-
skyt.qsi.net.nz:
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
Um... how would your C program (which doesn't know anything about
templates) include the header file where the template function is declared?
Keep in mind that a template function doesn't actually exist until it is
instantiated.... (not sure that "instantiated" is the right word....)
|
As I said, I understand why this case can't be, but the problem I had
involved setting up some callbacks for a C driver. The driver API has a
number of structs with function pointers for various callbacks.
So in this case, the C function pointers will be assigned in C++ code,
after the templates have been instantiated. the names are irrelevant to
the C code.
Ian
|
|
| Back to top |
|
 |
Jay Nabonne Guest
|
Posted: Wed Jul 27, 2005 11:21 pm Post subject: Re: why can't a template function have extern "C" linkage? |
|
|
On Thu, 28 Jul 2005 11:08:17 +1200, Ian wrote:
| Quote: | As I said, I understand why this case can't be, but the problem I had
involved setting up some callbacks for a C driver. The driver API has a
number of structs with function pointers for various callbacks.
So in this case, the C function pointers will be assigned in C++ code,
after the templates have been instantiated. the names are irrelevant to
the C code.
|
What problem are you running into? I'm not sure of your distinction of "C"
vs. "C++" functions, unless you mean stand-alone vs. class member
functions. Is your template creating a template function or a templated
class? If it's a class, then you can't use the address of the member
functions whether it's a template or not.
As far as I know, 'extern "C"' is irrelevant as far as function
pointers go.
If all else fails, create a standalone (non-template) function that calls
the template one, and set a pointer to it instead.
- Jay
|
|
| Back to top |
|
 |
Ian Guest
|
Posted: Wed Jul 27, 2005 11:47 pm Post subject: Re: why can't a template function have extern "C" linkage? |
|
|
Jay Nabonne wrote:
| Quote: | On Thu, 28 Jul 2005 11:08:17 +1200, Ian wrote:
As I said, I understand why this case can't be, but the problem I had
involved setting up some callbacks for a C driver. The driver API has a
number of structs with function pointers for various callbacks.
So in this case, the C function pointers will be assigned in C++ code,
after the templates have been instantiated. the names are irrelevant to
the C code.
What problem are you running into? I'm not sure of your distinction of "C"
vs. "C++" functions, unless you mean stand-alone vs. class member
functions. Is your template creating a template function or a templated
class? If it's a class, then you can't use the address of the member
functions whether it's a template or not.
In an ideal world, I wanted something like: |
extern "C" {
template <typename T> int bla() { return T().bla(); }
}
so I could set up the C struct thus:
template <typename T>
struct Fred : FredFromC
{
Fred() { fnPointer = bla<T>; }
};
Where fnPointer is a member of FredFromC.
| Quote: | As far as I know, 'extern "C"' is irrelevant as far as function
pointers go.
That's just it, there is a difference and it is relevant. |
Ian
|
|
| Back to top |
|
 |
Jay Nabonne Guest
|
Posted: Thu Jul 28, 2005 12:24 am Post subject: Re: why can't a template function have extern "C" linkage? |
|
|
On Thu, 28 Jul 2005 11:47:09 +1200, Ian wrote:
| Quote: | In an ideal world, I wanted something like:
extern "C" {
template <typename T> int bla() { return T().bla(); }
}
so I could set up the C struct thus:
template <typename T
struct Fred : FredFromC
{
Fred() { fnPointer = bla
};
Where fnPointer is a member of FredFromC.
|
This compiles for me under Visual C++ 7.0 and gcc 3.2.3:
// Begin code
struct FredFromC
{
int (*fnPointer)();
};
template <typename T> int bla() { /*return T().bla();*/ return 0; }
template <typename T>
struct Fred : FredFromC
{
Fred() { fnPointer = bla<T>; }
};
Fred<int> myStruct;
// End code
It even works if I put 'extern "C"' around the "FredFromC" structure
definition.
Again, what is the problem that you're having (e.g. what is the error
message).
- Jay
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Jul 28, 2005 12:26 am Post subject: Re: why can't a template function have extern "C" linkage? |
|
|
Ian wrote:
| Quote: | In an ideal world, I wanted something like:
extern "C" {
template <typename T> int bla() { return T().bla(); }
}
so I could set up the C struct thus:
template <typename T
struct Fred : FredFromC
{
Fred() { fnPointer = bla
};
Where fnPointer is a member of FredFromC.
|
And how is it going to be used?
V
|
|
| Back to top |
|
 |
Ian Guest
|
Posted: Thu Jul 28, 2005 12:31 am Post subject: Re: why can't a template function have extern "C" linkage? |
|
|
Victor Bazarov wrote:
| Quote: | Ian wrote:
In an ideal world, I wanted something like:
extern "C" {
template <typename T> int bla() { return T().bla(); }
}
so I could set up the C struct thus:
template <typename T
struct Fred : FredFromC
{
Fred() { fnPointer = bla
};
Where fnPointer is a member of FredFromC.
And how is it going to be used?
By the operating system as a callback into the driver. FredFromC is a |
structure defined by the OS to be populated with callbacks for various
driver functions.
Ian
|
|
| Back to top |
|
 |
Ian Guest
|
Posted: Thu Jul 28, 2005 12:38 am Post subject: Re: why can't a template function have extern "C" linkage? |
|
|
Jay Nabonne wrote:
| Quote: | On Thu, 28 Jul 2005 11:47:09 +1200, Ian wrote:
In an ideal world, I wanted something like:
extern "C" {
template <typename T> int bla() { return T().bla(); }
}
so I could set up the C struct thus:
template <typename T
struct Fred : FredFromC
{
Fred() { fnPointer = bla
};
Where fnPointer is a member of FredFromC.
Again, what is the problem that you're having (e.g. what is the error
message).
If I compile the following (based on your example): |
extern "C" {
struct FredFromC
{
int (*fnPointer)();
};
}
template <typename T> int bla() { return 0; }
template <typename T>
struct Fred : FredFromC
{
Fred() { fnPointer = bla<T>; }
};
int
main()
{
Fred<int> myStruct;
}
The compiler correctly warns:
"/tmp/x.cc", line 13: Warning (Anachronism): Assigning int(*)() to
extern "C" int(*)().
In this example, I can see no practical reason why bla() couldn't have
been declared as extern "C".
Ian
|
|
| Back to top |
|
 |
Jay Nabonne Guest
|
Posted: Thu Jul 28, 2005 12:49 am Post subject: Re: why can't a template function have extern "C" linkage? |
|
|
On Thu, 28 Jul 2005 12:38:08 +1200, Ian wrote:
| Quote: |
The compiler correctly warns:
"/tmp/x.cc", line 13: Warning (Anachronism): Assigning int(*)() to
extern "C" int(*)().
In this example, I can see no practical reason why bla() couldn't have
been declared as extern "C".
|
Ah, I see. A warning. Unfortunately, I can't coax any of the compilers I
have at my disposal to warn me about that, so I can't even begin to try to
figure out how to solve it (as I have no way to test).
I suppose this does no better:
template <typename T>
struct Fred : FredFromC
{
static int bla() { /*return T().bla();*/ return 0; }
Fred() { fnPointer = bla<T>; }
};
Good luck...
- Jay
|
|
| Back to top |
|
 |
Jay Nabonne Guest
|
Posted: Thu Jul 28, 2005 12:53 am Post subject: Re: why can't a template function have extern "C" linkage? |
|
|
On Thu, 28 Jul 2005 00:49:45 +0000, Jay Nabonne wrote:
| Quote: | On Thu, 28 Jul 2005 12:38:08 +1200, Ian wrote:
template <typename T
struct Fred : FredFromC
{
static int bla() { /*return T().bla();*/ return 0; }
Fred() { fnPointer = bla
|
// Oops. I meant:
Fred() { fnPointer = bla; }
| Quote: | };
Good luck...
- Jay
|
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Jul 28, 2005 1:12 am Post subject: Re: why can't a template function have extern "C" linkage? |
|
|
Ian wrote:
| Quote: | Victor Bazarov wrote:
Ian wrote:
In an ideal world, I wanted something like:
extern "C" {
template <typename T> int bla() { return T().bla(); }
}
so I could set up the C struct thus:
template <typename T
struct Fred : FredFromC
{
Fred() { fnPointer = bla
};
Where fnPointer is a member of FredFromC.
And how is it going to be used?
By the operating system as a callback into the driver. FredFromC is a
structure defined by the OS to be populated with callbacks for various
driver functions.
|
Sorry, that basically means nothing here. You should consider posting
the C code that calls whatever callbacks you are trying to define.
V
|
|
| Back to top |
|
 |
Ian Guest
|
Posted: Thu Jul 28, 2005 2:07 am Post subject: Re: why can't a template function have extern "C" linkage? |
|
|
Victor Bazarov wrote:
| Quote: | Ian wrote:
Victor Bazarov wrote:
Ian wrote:
In an ideal world, I wanted something like:
extern "C" {
template <typename T> int bla() { return T().bla(); }
}
so I could set up the C struct thus:
template <typename T
struct Fred : FredFromC
{
Fred() { fnPointer = bla
};
Where fnPointer is a member of FredFromC.
And how is it going to be used?
By the operating system as a callback into the driver. FredFromC is a
structure defined by the OS to be populated with callbacks for various
driver functions.
Sorry, that basically means nothing here. You should consider posting
the C code that calls whatever callbacks you are trying to define.
It's part of an operating system and I don't have it. |
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.
Apart form the mangled name, a template function is (well OK, can be) no
different than any other with the same signature:
#include <iostream>
template <typename T> void tem( int n ) { std::cout << n << std::endl; }
void normal( int n ) { std::cout << n*2 << std::endl; }
typedef void (*Fn)( int );
int
main()
{
Fn fn1 = &tem
Fn fn2 = &normal;
fn1( 2 );
fn2( 2 );
}
but we can't do
extern "C"{
template <typename T> void tem( int n ) {}
void normal( int n ) {}
typedef void (*Fn)( int );
}
Ian
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Jul 28, 2005 3:28 am Post subject: Re: why can't a template function have extern "C" linkage? |
|
|
Ian wrote:
| Quote: | [..]
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".
| Quote: | Apart form the mangled name, a template function is (well OK, can be)
no different than any other with the same signature:
|
(b) I think you're a bit confused. There are no "template functions".
There are only "function templates". What you have here is
a template, not a function.
(c) Templates can have linkage, but they cannot have "C" linkage because
the Standard prohibits it in 14/4.
| Quote: | #include <iostream
template
std::endl; } void normal( int n ) { std::cout << n*2 << std::endl; }
typedef void (*Fn)( int );
int
main()
{
Fn fn1 = &tem
Fn fn2 = &normal;
|
There is no need for '&' here, BTW
| Quote: |
fn1( 2 );
fn2( 2 );
}
but we can't do
extern "C"{
template <typename T> void tem( int n ) {}
void normal( int n ) {}
typedef void (*Fn)( int );
}
|
Correct, we can't.
V
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|