By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Sergei wrote:
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.

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);
......
}
};


--
Best regards,
Andrey Tarasevich

Jul 22 '05 #2

P: n/a
>
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.


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 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.

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, 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
Jul 22 '05 #3

P: n/a
Sergei wrote:
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.

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
Jul 22 '05 #4

P: n/a
Sergei wrote:
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.


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
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.