473,326 Members | 2,048 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,326 software developers and data experts.

function pointer as template argument

Hello,

I want to build a library to help exporting c++ functions to a
scripting languagge. The scripting language provides a function to
register functions like:

ANY f0()
ANY f1(ANY)
ANY f2(ANY, ANY)
ANY f3(ANY, ANY, ANY)
...

where ANY is an opaque type, with functions available to convert from
and to basic C types.

I would like to avoid writing a wrapper for any function I have to
export.
My first try (restricted to functions of one argument, but can easily
be extended):

//------------------------------------------------

template <class T_return, class T_arg, T_return (*fn)(T_arg)>
struct Translator1 {
static ANY proc(ANY arg)
{
T_arg a = from_any<T_arg>(arg); // Conversion from ANY to arg type
T_return r = to_any<T_return>((*fn)(a)); // Conversion from return
type to ANY
return r;
}
};

int foo(int a) { return a*10; }

typedef Translator1<int, int, foo> FN;
c_reg_function( FN::proc );

//-----------------------------------------------------

Ok, that works ! But it would be fine to let the compiler guess the
type of the arguments:

//--------------------------------------------------------

template <class T_return, class T_arg>
void export_fn(T_return (*fn)(T_arg))
{
c_reg_function(Translator1<T_return, T_arg, fn>::proc(arg));
}

export_fn(foo);

//-----------------------------------------------------------------

But now, something goes wrong (g++ v3.3) !!!

test.cc: En function `void export_fn(T_return (*)(T_arg)) [with
T_return = int,
T_arg = int]':
test.cc:43: instantiated from here
test.cc:33: error: `fn' is not a valid template argument
test.cc:33: error: it must be the address of a function with external
linkage
test.cc:33: error: `arg' undeclared (first use this function)
test.cc:33: error: (Each undeclared identifier is reported only once
for each
function it appears in.)
test.cc:33: error: `<type error>' is not a class type
I'm afraid that the compiler cannot expand a template class within a
template function with fewer template arguments ! I cannot use
functors because the register function expects C functions !

Any good idea ? I cannot think of any solution that avoids the use of
dirty preprocesor macros.

Thanks !!!
A reduced version of the code is provided so that you can try
yourself:

////////////////////////////////////////////
#include <iostream>

using namespace std;

typedef long ANY;

void c_reg_function(ANY (*fn)(ANY)); // external c library function

void c_reg_function(ANY (*fn)(ANY))
{
cout << "Registro: " << (*fn)(2) << endl;
}

template<class T> ANY to_any(T); // ANY conversion functions
template<class T> T from_any(ANY);

template<> int from_any(ANY a) { return a; }
template<> ANY to_any(int a) { return a; }

template <class T_return, class T_arg, T_return (*fn)(T_arg)>
struct Translator1 {
static ANY proc(ANY arg)
{
T_arg a = from_any<T_arg>(arg);
T_return r = to_any<T_return>((*fn)(a));
return r;
}
};

template <class T_return, class T_arg>
void export_fn(T_return (*fn)(T_arg))
{
c_reg_function(Translator1<T_return, T_arg, fn>::proc(arg));
}

int foo(int a) { return a*10; }

int main(int argc, char** argv)
{
typedef Translator1<int, int, foo> FN;
c_reg_function( FN::proc );

export_fn(foo);

}
/////////////////////////////////////7
Jul 22 '05 #1
8 11332
"vpadial" <vi***********@yahoo.es> wrote in message
news:e9*************************@posting.google.co m...
Hello,

I want to build a library to help exporting c++ functions to a
scripting languagge. The scripting language provides a function to
register functions like:

ANY f0()
ANY f1(ANY)
ANY f2(ANY, ANY)
ANY f3(ANY, ANY, ANY)
...

where ANY is an opaque type, with functions available to convert from
and to basic C types.

I would like to avoid writing a wrapper for any function I have to
export.
My first try (restricted to functions of one argument, but can easily
be extended):

//------------------------------------------------

template <class T_return, class T_arg, T_return (*fn)(T_arg)>
struct Translator1 {
static ANY proc(ANY arg)
{
T_arg a = from_any<T_arg>(arg); // Conversion from ANY to arg type
T_return r = to_any<T_return>((*fn)(a)); // Conversion from return
type to ANY
return r;
}
};

int foo(int a) { return a*10; }

typedef Translator1<int, int, foo> FN;
c_reg_function( FN::proc );

//-----------------------------------------------------

Ok, that works ! But it would be fine to let the compiler guess the
type of the arguments:

//--------------------------------------------------------

template <class T_return, class T_arg>
void export_fn(T_return (*fn)(T_arg))
{
c_reg_function(Translator1<T_return, T_arg, fn>::proc(arg));
}

export_fn(foo);

//-----------------------------------------------------------------

But now, something goes wrong (g++ v3.3) !!!

test.cc: En function `void export_fn(T_return (*)(T_arg)) [with
T_return = int,
T_arg = int]':
test.cc:43: instantiated from here
test.cc:33: error: `fn' is not a valid template argument
test.cc:33: error: it must be the address of a function with external
linkage
test.cc:33: error: `arg' undeclared (first use this function)
test.cc:33: error: (Each undeclared identifier is reported only once
for each
function it appears in.)
test.cc:33: error: `<type error>' is not a class type
I'm afraid that the compiler cannot expand a template class within a
template function with fewer template arguments ! I cannot use
functors because the register function expects C functions !


It's not complaining about the number of arguments. The value of "fn" won't
be known until runtime. You can't expect the compiler to instantiate a
template based on its value if it can't possibly know the value.

Why is it a template argument in the first place? Couldn't you just write
it like this:

template <class T_return, class T_arg>
struct Translator1 {
static ANY proc(ANY arg, T_return (*fn)(T_arg))
{
T_arg a = from_any<T_arg>(arg);
T_return r = to_any<T_return>((*fn)(a));
return r;
}
};

template <class T_return, class T_arg>
void export_fn(T_return (*fn)(T_arg))
{
c_reg_function(Translator1<T_return, T_arg, fn>::proc(arg,fn));
}

or do you plan on doing some specializations based on its value?

--
David Hilsee
Jul 22 '05 #2
"David Hilsee" <da*************@yahoo.com> wrote in message
news:rv********************@comcast.com...
[...]
c_reg_function(Translator1<T_return, T_arg, fn>::proc(arg,fn));


Sorry, I meant

c_reg_function(Translator1<T_return, T_arg>::proc(arg,fn));

--
David Hilsee

Jul 22 '05 #3
"David Hilsee" <da*************@yahoo.com> wrote in message
news:rv********************@comcast.com...
"vpadial" <vi***********@yahoo.es> wrote in message
news:e9*************************@posting.google.co m...
Hello,

I want to build a library to help exporting c++ functions to a
scripting languagge. The scripting language provides a function to
register functions like:

ANY f0()
ANY f1(ANY)
ANY f2(ANY, ANY)
ANY f3(ANY, ANY, ANY)
...

where ANY is an opaque type, with functions available to convert from
and to basic C types.

I would like to avoid writing a wrapper for any function I have to
export.
My first try (restricted to functions of one argument, but can easily
be extended):

//------------------------------------------------

template <class T_return, class T_arg, T_return (*fn)(T_arg)>
struct Translator1 {
static ANY proc(ANY arg)
{
T_arg a = from_any<T_arg>(arg); // Conversion from ANY to arg type
T_return r = to_any<T_return>((*fn)(a)); // Conversion from return
type to ANY
return r;
}
};

int foo(int a) { return a*10; }

typedef Translator1<int, int, foo> FN;
c_reg_function( FN::proc );

//-----------------------------------------------------

Ok, that works ! But it would be fine to let the compiler guess the
type of the arguments:

//--------------------------------------------------------

template <class T_return, class T_arg>
void export_fn(T_return (*fn)(T_arg))
{
c_reg_function(Translator1<T_return, T_arg, fn>::proc(arg));
}

export_fn(foo);

//-----------------------------------------------------------------

But now, something goes wrong (g++ v3.3) !!!

test.cc: En function `void export_fn(T_return (*)(T_arg)) [with
T_return = int,
T_arg = int]':
test.cc:43: instantiated from here
test.cc:33: error: `fn' is not a valid template argument
test.cc:33: error: it must be the address of a function with external
linkage
test.cc:33: error: `arg' undeclared (first use this function)
test.cc:33: error: (Each undeclared identifier is reported only once
for each
function it appears in.)
test.cc:33: error: `<type error>' is not a class type
I'm afraid that the compiler cannot expand a template class within a
template function with fewer template arguments ! I cannot use
functors because the register function expects C functions !
It's not complaining about the number of arguments. The value of "fn"

won't be known until runtime. You can't expect the compiler to instantiate a
template based on its value if it can't possibly know the value.

Why is it a template argument in the first place? Couldn't you just write
it like this:

template <class T_return, class T_arg>
struct Translator1 {
static ANY proc(ANY arg, T_return (*fn)(T_arg))
{
T_arg a = from_any<T_arg>(arg);
T_return r = to_any<T_return>((*fn)(a));
return r;
}
};

template <class T_return, class T_arg>
void export_fn(T_return (*fn)(T_arg))
{
c_reg_function(Translator1<T_return, T_arg, fn>::proc(arg,fn));
}
[...]


After rereading your post several times, I realized that I did not fully
understand what you were trying to do. I was confused by the sample code
that appeared to be an attempt at invoking "proc" (Translator1<T_return,
T_arg, fn>::proc(arg)). I think the line "c_reg_function( FN::proc );"
should have instead been "c_reg_function( &FN::proc );" For some reason, I
thought you had omitted some arguments to the function call on that line.
Sorry for any confusion my responses may have caused.

Even so, I can't think of any way that you could have the compiler easily
and automatically determine the return type and arguments in the way that
you need them to be determined. The function template you wrote won't work
for the reason a mentioned before: the value is not known at compile time.

--
David Hilsee
Jul 22 '05 #4
"David Hilsee" <da*************@yahoo.com> wrote in message news:<Kc********************@comcast.com>...
"David Hilsee" <da*************@yahoo.com> wrote in message
news:rv********************@comcast.com...
[...]
c_reg_function(Translator1<T_return, T_arg, fn>::proc(arg,fn));


Sorry, I meant

c_reg_function(Translator1<T_return, T_arg>::proc(arg,fn));


The problem is that c_reg_function expects a pointer to a function (ANY (*fn)(ANY))
Jul 22 '05 #5
On 6 Oct 2004 14:58:45 -0700, vi***********@yahoo.es (vpadial) wrote:
Hello,

I want to build a library to help exporting c++ functions to a
scripting languagge. The scripting language provides a function to
register functions like:

ANY f0()
ANY f1(ANY)
ANY f2(ANY, ANY)
ANY f3(ANY, ANY, ANY)
...

where ANY is an opaque type, with functions available to convert from
and to basic C types.

I would like to avoid writing a wrapper for any function I have to
export.
My first try (restricted to functions of one argument, but can easily
be extended):

//------------------------------------------------

template <class T_return, class T_arg, T_return (*fn)(T_arg)>
struct Translator1 {
static ANY proc(ANY arg)
{
T_arg a = from_any<T_arg>(arg); // Conversion from ANY to arg type
T_return r = to_any<T_return>((*fn)(a)); // Conversion from return
type to ANY
return r;
}
};

int foo(int a) { return a*10; }

typedef Translator1<int, int, foo> FN;
c_reg_function( FN::proc );

//-----------------------------------------------------

Ok, that works ! But it would be fine to let the compiler guess the
type of the arguments:

//--------------------------------------------------------

template <class T_return, class T_arg>
void export_fn(T_return (*fn)(T_arg))
{
c_reg_function(Translator1<T_return, T_arg, fn>::proc(arg));
}

export_fn(foo);

//-----------------------------------------------------------------

But now, something goes wrong (g++ v3.3) !!!

test.cc: En function `void export_fn(T_return (*)(T_arg)) [with
T_return = int,
T_arg = int]':
test.cc:43: instantiated from here
test.cc:33: error: `fn' is not a valid template argument
test.cc:33: error: it must be the address of a function with external
linkage
test.cc:33: error: `arg' undeclared (first use this function)
test.cc:33: error: (Each undeclared identifier is reported only once
for each
function it appears in.)
test.cc:33: error: `<type error>' is not a class type
I'm afraid that the compiler cannot expand a template class within a
template function with fewer template arguments ! I cannot use
functors because the register function expects C functions !

Any good idea ? I cannot think of any solution that avoids the use of
dirty preprocesor macros.


The problem is that your function argument "fn" has a runtime value,
but template arguments are required to be known at compile time.

I can't think of a standard C++ solution, but I can think of a gcc
specific solution - typeof. e.g.

template <typename FuncType, FuncType func>
struct Translator1
{
//need to write FuncArgs, using partial specialization
typedef FuncArgs<FuncType>::first_argument_type T_arg;
typedef FuncArgs<FuncType>::return_type T_return;

static ANY proc(ANY arg)
{
T_arg a = from_any<T_arg>(arg); // Conversion from ANY to arg type
T_return r = to_any<T_return>((*fn)(a));
return r;
}
};

c_reg_function(&Translator1<typeof(&foo), foo>::proc);

Whether you can put up with the non-portability is up to you.

Tom
Jul 22 '05 #6
On 6 Oct 2004 14:58:45 -0700, vi***********@yahoo.es (vpadial) wrote:
Hello,

I want to build a library to help exporting c++ functions to a
scripting languagge.


Actually, what you're doing has been done several times before. This
book would be helpful:
http://www.informit.com/title/0321227255

Here's an example of a library that binds C++ and Python:
http://www.boost.org/libs/python/doc/index.html

Jul 22 '05 #7
Tom Widmer <to********@hotmail.com> wrote in message news:<k7********************************@4ax.com>. ..

The problem is that your function argument "fn" has a runtime value,
but template arguments are required to be known at compile time.

I can't think of a standard C++ solution, but I can think of a gcc
specific solution - typeof. e.g.

template <typename FuncType, FuncType func>
struct Translator1
{
//need to write FuncArgs, using partial specialization
typedef FuncArgs<FuncType>::first_argument_type T_arg;
typedef FuncArgs<FuncType>::return_type T_return;

static ANY proc(ANY arg)
{
T_arg a = from_any<T_arg>(arg); // Conversion from ANY to arg type
T_return r = to_any<T_return>((*fn)(a));
return r;
}
};

c_reg_function(&Translator1<typeof(&foo), foo>::proc);

Whether you can put up with the non-portability is up to you.


Thanks Tom, but I can't get it working. Could you explain better the
use of FuncArgs ? How can I specialize types of a template class ?
Must I specialize FuncArgs for any new function signature ?

Thanks in advance
Jul 22 '05 #8
vi***********@yahoo.es (vpadial) wrote in message news:<e9*************************@posting.google.c om>...
Tom Widmer <to********@hotmail.com> wrote in message news:<k7********************************@4ax.com>. ..

The problem is that your function argument "fn" has a runtime value,
but template arguments are required to be known at compile time.

I can't think of a standard C++ solution, but I can think of a gcc
specific solution - typeof. e.g.

template <typename FuncType, FuncType func>
struct Translator1
{
//need to write FuncArgs, using partial specialization
typedef FuncArgs<FuncType>::first_argument_type T_arg;
typedef FuncArgs<FuncType>::return_type T_return;

static ANY proc(ANY arg)
{
T_arg a = from_any<T_arg>(arg); // Conversion from ANY to arg type
T_return r = to_any<T_return>((*fn)(a));
return r;
}
};

c_reg_function(&Translator1<typeof(&foo), foo>::proc);

Whether you can put up with the non-portability is up to you.


Thanks Tom, but I can't get it working. Could you explain better the
use of FuncArgs ? How can I specialize types of a template class ?
Must I specialize FuncArgs for any new function signature ?

Thanks in advance


Don't bother. I've seen Boost provides function_traits that I guess is
equivalent to your FuncArgs class:
http://www.boost.org/libs/type_traits/#function_traits

Thanks for your help
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Surya Kiran | last post by:
Hi all, I've written a function template. say template <class T> fn (T var) { ... } Is there any way, from within the function, can we check what type of argument we've passed on to the...
5
by: Suzanne Vogel | last post by:
Is it possible to store a pointer to a template function? eg, template<class T> fooFunc() {...} fptr = fooFunc; // <-- I couldn't find any info here, not even in the forums:...
14
by: John Harrison | last post by:
Can anyone explain why this code fails to compile? #include <iostream> void func() { std::cout << "success!\n"; } struct S
3
by: jimjim | last post by:
Hello, My question concerns as to how a pointer is passed by reference as a function argument. The following is from code taken from the MICO implementation of the CORBA specification. in...
4
by: Vijai Kalyan | last post by:
I was decomposing a task into different policies. Essentially, there is a general option obtained from a server and user options obtained from configuration variables. The two options are...
4
by: infogoogle | last post by:
Hello, i'm having problems with the type of a template function: This code: class A {}; class B : A {}; template<class T B* fnull() { return 0; };
17
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
2
by: webinfinite | last post by:
This is my first post here, please tell me if I did anything wrong. in the following code snippet: 1. template <class In, class Pred> 2. In find_if(In begin, In end, Pred f) { 3. while (begin...
5
by: Fei Liu | last post by:
Hello, I just hit a strange problem regarding SFINAE. The following code causes compile error (void cannot be array element type), I thought SFINA should match test(...) version instead and not...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.