Template Function use as function argument problem,thanks.

L

linarin

#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,pFunc>);
}

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

dasjotre

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,pFunc>);

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,pFunc>);
}

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
 
L

linarin

Oh,thanks.

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

have you any idea?
 
D

dasjotre

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top