By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

Template Function use as function argument problem,thanks.

P: n/a
#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 ===

Jun 13 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 13 Jun, 07:23, linarin <lin.jian...@gmail.comwrote:
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

Jun 13 '07 #2

P: n/a
Oh,thanks.

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

have you any idea?

Jun 14 '07 #3

P: n/a
On 14 Jun, 01:24, linarin <lin.jian...@gmail.comwrote:
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

Jun 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.