how to pass a function which has variable arguments as template argument?
-
I want to write a adapter which can convert non static member functions to C-sytle function pointers. Here is what I got now(see the following code snippet), but the current solution is not general. I want to make
int (T::*Func)(int)
accept variable arguments. but i'm stuck here? anybody can help me? and it's necessary to make
int f()
and
int display
have the same signature. The final goal is Interfacing C++ member functions with C libraries.
class StoreVals
{
int val;
public:
int display(int k) { cout << k << endl; return 0; }
};template
class CObjectT
{
public:
/*
the signagure of f(...) should change by the argument of template. they must be the same, but i don't know how to achieve this goal
*/
static int f(int i)
{
T obj;
return (obj.*Func)(i);
}
};void main()
{
CObjectT::f(7);
auto function_t = &CObjectT::f;
cout << typeid(function_t).name() << endl; // now it's a c-style function pointer
} -
I want to write a adapter which can convert non static member functions to C-sytle function pointers. Here is what I got now(see the following code snippet), but the current solution is not general. I want to make
int (T::*Func)(int)
accept variable arguments. but i'm stuck here? anybody can help me? and it's necessary to make
int f()
and
int display
have the same signature. The final goal is Interfacing C++ member functions with C libraries.
class StoreVals
{
int val;
public:
int display(int k) { cout << k << endl; return 0; }
};template
class CObjectT
{
public:
/*
the signagure of f(...) should change by the argument of template. they must be the same, but i don't know how to achieve this goal
*/
static int f(int i)
{
T obj;
return (obj.*Func)(i);
}
};void main()
{
CObjectT::f(7);
auto function_t = &CObjectT::f;
cout << typeid(function_t).name() << endl; // now it's a c-style function pointer
}Falconapollo wrote:
The final goal is Interfacing C++ member functions with C libraries.
If you want your C++ code to call C libraries then you use extern "C" { ...// C code and/or declarations. } If you want C libraries to call your C++ code then you must provide a C interface to it.