Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template Function use as function argument problem,thanks.

Reply
Thread Tools

Template Function use as function argument problem,thanks.

 
 
linarin
Guest
Posts: n/a
 
      06-13-2007
#include <iostream>
using namespace std;

typedef bool (*CallableFunction)(int argc,char* argv[]);

void DefineMyFunction(const char* name,CallableFunction func){
//here do the define action.
}

template<typename R,typename A,R (*pFunc)(A)>
bool CallableFunctionProxy(int argc,char* argv[]){
A a = A(); // this is demo only
pFunc(a);
return true;
}

template<typename R,typename A>
void Def(const char* name,R (*pFunc)(A)){
DefineMyFunction(name,CallableFunctionProxy<R,A,pF unc>);
}

int alert(int){
cout << "From alert" << endl;
return 0;
}

int main(int argc,char* argv[])
{
CallableFunctionProxy<int,int,alert>(argc,argv);
//Def("alert",alert); //error here
return 0;
}


this program compile and execute as respected.
but when I uncomment the line marked with "error here" in function
"main" the following compile error occur, could any one known what is
error:

D:\Program\CodeBlocks\Projects\firstCpp\main.cpp:2 9: instantiated from
here
D:\Program\CodeBlocks\Projects\firstCpp\main.cpp:1 8: error: `pFunc' is
not a valid template argument
D:\Program\CodeBlocks\Projects\firstCpp\main.cpp:1 8: error: it must be
the address of a function with external linkage
D:\Program\CodeBlocks\Projects\firstCpp\main.cpp:1 8: error: `pFunc' is
not a valid template argument
D:\Program\CodeBlocks\Projects\firstCpp\main.cpp:1 8: error: it must be
the address of a function with external linkage
D:\Program\CodeBlocks\Projects\firstCpp\main.cpp:1 8: error: `pFunc' is
not a valid template argument
D:\Program\CodeBlocks\Projects\firstCpp\main.cpp:1 8: error: it must be
the address of a function with external linkage
D:\Program\CodeBlocks\Projects\firstCpp\main.cpp:1 8: error: no matches
converting function `CallableFunctionProxy' to type `bool (*)(int,
char**)'
D:\Program\CodeBlocks\Projects\firstCpp\main.cpp:1 0: error: candidates
are: template<class R, class A, R (*pFunc)(A)> bool
CallableFunctionProxy(int, char**)
:: === Build finished: 8 errors, 1 warnings ===

 
Reply With Quote
 
 
 
 
dasjotre
Guest
Posts: n/a
 
      06-13-2007
On 13 Jun, 07:23, linarin <(E-Mail Removed)> wrote:

> template<typename R,typename A>
> void Def(const char* name,R (*pFunc)(A)){


pFunc here is a run-time value, it can not be used
as template parameter.

> DefineMyFunction(name,CallableFunctionProxy<R,A,pF unc>);


you could change Def to take pFunc as a template
parameter the same way you use it in
CallableFunctionProxy

template<typename R,typename A, R (*pFunc)(A)>
void Def(const char* name){
DefineMyFunction(name,CallableFunctionProxy<R,A,pF unc>);
}

but then you have to call it

Def<int, int, &alert>("alert");

(I have no idea what you're trying to achieve)
you should check boost::function (or std::tr1::function
if you have tr1)

DS

 
Reply With Quote
 
 
 
 
linarin
Guest
Posts: n/a
 
      06-14-2007
Oh,thanks.

but how boost.python is implemented. I am trying port the
boost.python to SpiderMonkey.

have you any idea?

 
Reply With Quote
 
dasjotre
Guest
Posts: n/a
 
      06-14-2007
On 14 Jun, 01:24, linarin <(E-Mail Removed)> wrote:
> Oh,thanks.
>
> but how boost.python is implemented. I am trying port the
> boost.python to SpiderMonkey.
>
> have you any idea?


no, but you can download boost code and check
for yourself.

DS

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function pointer as template argument - does not work for function template avasilev C++ 4 12-22-2011 02:11 PM
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
Template argument as template argument nw C++ 0 04-14-2008 01:36 PM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
Template function as argument to another template function Jim West C++ 3 10-07-2004 07:21 PM



Advertisments