Template Function use as function argument problem,thanks.

Discussion in 'C++' started by linarin, Jun 13, 2007.

  1. linarin

    linarin Guest

    #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 ===
    linarin, Jun 13, 2007
    #1
    1. Advertising

  2. linarin

    dasjotre Guest

    On 13 Jun, 07:23, linarin <> 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,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
    dasjotre, Jun 13, 2007
    #2
    1. Advertising

  3. linarin

    linarin Guest

    Oh,thanks.

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

    have you any idea?
    linarin, Jun 14, 2007
    #3
  4. linarin

    dasjotre Guest

    On 14 Jun, 01:24, linarin <> 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
    dasjotre, Jun 14, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim West
    Replies:
    3
    Views:
    746
    Jim West
    Oct 7, 2004
  2. Vijai Kalyan
    Replies:
    4
    Views:
    689
    Vijai Kalyan
    Nov 8, 2005
  3. nw
    Replies:
    0
    Views:
    298
  4. nguillot
    Replies:
    5
    Views:
    514
  5. avasilev
    Replies:
    4
    Views:
    564
    avasilev
    Dec 22, 2011
Loading...

Share This Page