473,890 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ret urn (*fn)(T_arg))
{
c_reg_function( Translator1<T_r eturn, T_arg, fn>::proc(arg)) ;
}

export_fn(foo);

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

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

test.cc: En function `void export_fn(T_ret urn (*)(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_ret urn (*fn)(T_arg))
{
c_reg_function( Translator1<T_r eturn, 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 11370
"vpadial" <vi***********@ yahoo.es> wrote in message
news:e9******** *************** **@posting.goog le.com...
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_ret urn (*fn)(T_arg))
{
c_reg_function( Translator1<T_r eturn, T_arg, fn>::proc(arg)) ;
}

export_fn(foo);

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

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

test.cc: En function `void export_fn(T_ret urn (*)(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_ret urn (*fn)(T_arg))
{
c_reg_function( Translator1<T_r eturn, T_arg, fn>::proc(arg,f n));
}

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******** ************@co mcast.com...
[...]
c_reg_function( Translator1<T_r eturn, T_arg, fn>::proc(arg,f n));


Sorry, I meant

c_reg_function( Translator1<T_r eturn, T_arg>::proc(ar g,fn));

--
David Hilsee

Jul 22 '05 #3
"David Hilsee" <da************ *@yahoo.com> wrote in message
news:rv******** ************@co mcast.com...
"vpadial" <vi***********@ yahoo.es> wrote in message
news:e9******** *************** **@posting.goog le.com...
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_ret urn (*fn)(T_arg))
{
c_reg_function( Translator1<T_r eturn, T_arg, fn>::proc(arg)) ;
}

export_fn(foo);

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

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

test.cc: En function `void export_fn(T_ret urn (*)(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_ret urn (*fn)(T_arg))
{
c_reg_function( Translator1<T_r eturn, T_arg, fn>::proc(arg,f n));
}
[...]


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******* *************@c omcast.com>...
"David Hilsee" <da************ *@yahoo.com> wrote in message
news:rv******** ************@co mcast.com...
[...]
c_reg_function( Translator1<T_r eturn, T_arg, fn>::proc(arg,f n));


Sorry, I meant

c_reg_function( Translator1<T_r eturn, T_arg>::proc(ar g,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***********@y ahoo.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_ret urn (*fn)(T_arg))
{
c_reg_function( Translator1<T_r eturn, T_arg, fn>::proc(arg)) ;
}

export_fn(foo) ;

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

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

test.cc: En function `void export_fn(T_ret urn (*)(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<FuncTy pe>::first_argu ment_type T_arg;
typedef FuncArgs<FuncTy pe>::return_typ e 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<ty peof(&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***********@y ahoo.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********@hot mail.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<FuncTy pe>::first_argu ment_type T_arg;
typedef FuncArgs<FuncTy pe>::return_typ e 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<ty peof(&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***********@y ahoo.es (vpadial) wrote in message news:<e9******* *************** ***@posting.goo gle.com>...
Tom Widmer <to********@hot mail.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<FuncTy pe>::first_argu ment_type T_arg;
typedef FuncArgs<FuncTy pe>::return_typ e 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<ty peof(&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
4596
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 function (double, float etc) ?? Thanks in advance,
5
2970
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: http://www.function-pointer.org/
14
1678
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
1870
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 include files: typedef Context *Context_ptr; typedef ObjOut<Context> Context_out;
4
4264
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 complementary to one another. So I proceeded to decompose the tasks that deal with user options into two functions. Each of the functions do something and towards the end they do supplementary tasks that depend on the server option. The whole things...
4
2297
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
2244
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 an algorithm that modifies this datastructure. The basic outline of the algorithm is independent of the type of property. So I implemented a generic version of the algorithm and a function object for
5
2277
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 to functions are hard to inline for the compiler, and so you won't be able to avoid function call overhead. This is an important aspect when you are calling a function very frequently for evaluation reason: think of the problem of performing...
2
2383
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 != end && !f(*begin)) 4. ++begin; 5. return begin; 6. }
5
2084
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 issue any error. If I replace U with U(*), then the code compiles again. I couldn't make the sense out of it. What's the magic with (*)? Note that function<int>::yes returns correct result 0 even with U. Please help me out.
0
9978
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10918
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8015
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7169
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4674
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4270
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.