473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
4 4215
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
>
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: terrencel | last post by:
I was told to look at some old C code that was ported to C++. One of the file is like: ========================================= CPPClass* someCPPVar = NULL; extern "C" {
22
by: Ian | last post by:
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...
9
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
12
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external...
5
by: Daniel Rudy | last post by:
What is the purpose of having a #define inside a struct? typedef struct { uByte bLength; uByte bDescriptorType; uWord wTotalLength; uByte bNumInterface;...
1
by: arun | last post by:
Hello team, I know that extern keyword before template class or function delays the instantiation of that template declaration. However,, my question is does this utility only works for...
4
by: =?Utf-8?B?U2VhbiBDb25uZXJ5?= | last post by:
Hi, Is there a way to enable *just* the extern template processing when compiling with /Za? Thanks!
3
by: parag_paul | last post by:
Does the following typedef differ from a typedef inside a extern "C" #if defined(__cplusplus) extern "C" { #endif typedef int (*fun_point)(int);
5
by: Medvedev | last post by:
What's that preprocessor do #ifdef __cplusplus extern "C" { #endif .. .. .. #ifdef __cplusplus } #endif
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.