Is it possible to define extern "C" types inside a template?

S

Sergei

I ran into this problem. I needed to create an entry for access to a
library
of functions that are "extern C", and I just can't find the right syntax, if
it exists at all ( I am using MSVC 6.0-7.1).. Note, that basically I don't
need an "extern C" linkage,
I just need to define a type to cast a resolved function address..
Any ideas?


Basically , I need something like this..

template <typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5>
class FuncCall
{
FuncCall(string& aname):name(aname){}
private:
extern "C" typedef T0 (*function_ptr) (T1 t1, T2 t2, T3 t3, T4 t4, T5
t5 );
string name;
public:
bool call()
{
function_ptr func = (function_ptr) GetProcAddress(name);
.......
}
};

Thanks,
Sergei
 
A

Andrey Tarasevich

Sergei said:
I ran into this problem. I needed to create an entry for access to a
library
of functions that are "extern C", and I just can't find the right syntax, if
it exists at all ( I am using MSVC 6.0-7.1).. Note, that basically I don't
need an "extern C" linkage,

Unfortunately, it can't be done the way you are trying to do it. 'extern
"C"' declarations are only allowed in namespace scope, which prevents
anyone from creating 'extern "C"' declarations that depend on template
parameters.
I just need to define a type to cast a resolved function address..
Any ideas?

I can suggest a workaround. Introduce one more template parameter to you
template and supply it with a default argument

template <typename T0, typename T1, typename T2,
typename T3, typename T4, typename T5,
typename FN = T0 (*)(T1, T2, T3, T4, T5)>
class FuncCall
{
typedef FN function_ptr;
...

As long as you use this template with C++ functions, there's no need to
change anything - default argument will take care of everything. But
once you need to use it with a C function, you can do the following

extern "C" typedef int (*CFN)(int, int, int, int, int);
// This should be done in global namespace scope

...
FuncCall<int, int, int, int, int, int, CFN> fc("some_name") ;

It is not very elegant solution, since you have to do all required
'typedef's manually, but it solves the problem.
 
S

Sergei

Unfortunately, it can't be done the way you are trying to do it. 'extern
"C"' declarations are only allowed in namespace scope, which prevents
anyone from creating 'extern "C"' declarations that depend on template
parameters.

Any particular reasons for that limitations? Or is it just another
deficiency of ANSI C++?
I don't see any reason for that implementation-wise. Besides, we don't have
extern "C" declaration
here, only a definition.
I can suggest a workaround. Introduce one more template parameter to you
template and supply it with a default argument

template <typename T0, typename T1, typename T2,
typename T3, typename T4, typename T5,
typename FN = T0 (*)(T1, T2, T3, T4, T5)>
class FuncCall
{
typedef FN function_ptr;
...

As long as you use this template with C++ functions, there's no need to
change anything - default argument will take care of everything. But
once you need to use it with a C function, you can do the following

extern "C" typedef int (*CFN)(int, int, int, int, int);
// This should be done in global namespace scope

...
FuncCall<int, int, int, int, int, int, CFN> fc("some_name") ;

It is not very elegant solution, since you have to do all required
'typedef's manually, but it solves the problem.

Thanks, Andrey,

that's exactly what I did. And that workaround is working. But, as you said,
it is not elegant ( why use an extra
parameter type if it doesn' bring any new information into template ). I
really hoped that I could get away
without typedef-ing every function that I will have to implement. No big
deal though.

Sergei
 
V

Victor Bazarov

Sergei said:
syntax, if



Any particular reasons for that limitations? Or is it just another
deficiency of ANSI C++?
I don't see any reason for that implementation-wise. Besides, we don't have
extern "C" declaration
here, only a definition.

Just a reminder, every definition is also a declaration, so we _do_ have a
declaration here.

V
 
D

Denis Remezov

Sergei said:
Any particular reasons for that limitations? Or is it just another
deficiency of ANSI C++?
I don't see any reason for that implementation-wise.

[snip]

You cannot have two or more extern "C" functions with the same name in one
program, which makes sense, because there is no function name overloading in C.

Denis
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top