Passing member function pointers are template parameters

Discussion in 'C++' started by Dilip, Nov 6, 2006.

  1. Dilip

    Dilip Guest

    I am stuck with a syntactical issue and would appreciate some help:

    I have a class with a templated mem fn like so:

    class A
    {
    };

    template<typename Ret, typename P1, Ret (A::*fp)(P1*)>
    void A::somefunc()
    {
    P1* p = getP1FromSomewhere();
    someotherfunc(p, fp);
    }

    // should I re-write the whole template shebang for someotherfunc?
    // I tried (probably w/o understanding completely)
    template<typename X, typename T>
    void A::someotherfunc(X* p1, T func)
    {
    // fails with compiler error the statement does not evaluate to a
    function being called
    // with one parameter
    func(p1);
    }

    What is the correct way to pass function pointers in a cascade?

    P.s: I am mucking around with legacy code and although there might be
    better ways of acheiving what I want, I am not in a position to make
    whole-sale code changes
    Dilip, Nov 6, 2006
    #1
    1. Advertising

  2. Dilip wrote:
    > I am stuck with a syntactical issue and would appreciate some help:
    >
    > I have a class with a templated mem fn like so:
    >
    > class A
    > {
    > };
    >
    > template<typename Ret, typename P1, Ret (A::*fp)(P1*)>
    > void A::somefunc()
    > {
    > P1* p = getP1FromSomewhere();
    > someotherfunc(p, fp);
    > }
    >
    > // should I re-write the whole template shebang for someotherfunc?
    > // I tried (probably w/o understanding completely)
    > template<typename X, typename T>
    > void A::someotherfunc(X* p1, T func)
    > {
    > // fails with compiler error the statement does not evaluate to a
    > function being called
    > // with one parameter
    > func(p1);
    > }
    >
    > What is the correct way to pass function pointers in a cascade?


    Yes, but it's not a correct way to use a pointer to member. You need
    an instance of the class. Did you mean to write

    (this->*func)(p1);

    (or some such)?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 6, 2006
    #2
    1. Advertising

  3. Dilip

    Guest

    Dilip wrote:
    > I am stuck with a syntactical issue and would appreciate some help:
    >
    > I have a class with a templated mem fn like so:
    >
    > class A
    > {
    > };
    >
    > template<typename Ret, typename P1, Ret (A::*fp)(P1*)>
    > void A::somefunc()
    > {
    > P1* p = getP1FromSomewhere();
    > someotherfunc(p, fp);
    > }
    >
    > // should I re-write the whole template shebang for someotherfunc?
    > // I tried (probably w/o understanding completely)
    > template<typename X, typename T>
    > void A::someotherfunc(X* p1, T func)
    > {
    > // fails with compiler error the statement does not evaluate to a
    > function being called
    > // with one parameter
    > func(p1);
    > }
    >
    > What is the correct way to pass function pointers in a cascade?
    >
    > P.s: I am mucking around with legacy code and although there might be
    > better ways of acheiving what I want, I am not in a position to make
    > whole-sale code changes


    Not 100% sure about your class requirements as the defintion is
    empty...but here is what I could come up with based on the function
    sgnatures you had..

    class P1
    {
    };

    class P
    {
    public:
    void Pfunc(P1* p1_ptr)
    {
    };
    };

    class A
    {
    public:
    template<typename Ret, typename tP1, typename tP2, Ret
    (tP1::*fp)(tP2*)>
    void somefunc(tP1* p) ;

    template<typename X, typename T>
    void someotherfunc(X* p1, T func);
    };


    template<typename Ret, typename tP1, typename tP2, Ret
    (tP1::*fp)(tP2*)>
    void A::somefunc(tP1* p)
    {
    someotherfunc(p, fp);
    }


    template<typename X, typename T>
    void A::someotherfunc(X* p, T func)
    {
    P1* p1new = new P1;
    (p->*func)(p1new);
    delete p1new;
    }
    , Nov 6, 2006
    #3
  4. Dilip

    Dilip Guest

    Victor Bazarov wrote:
    > > What is the correct way to pass function pointers in a cascade?

    >
    > Yes, but it's not a correct way to use a pointer to member. You need
    > an instance of the class. Did you mean to write
    >
    > (this->*func)(p1);


    Thanks! Thats exactly what I was looking for.
    Dilip, Nov 6, 2006
    #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. Newsgroup - Ann
    Replies:
    5
    Views:
    594
    John Carson
    Jul 30, 2003
  2. Replies:
    8
    Views:
    546
    PJP of NYC
    May 24, 2005
  3. Jason
    Replies:
    2
    Views:
    495
    Jonathan Mcdougall
    May 13, 2006
  4. Replies:
    18
    Views:
    526
    Diego Martins
    Feb 26, 2007
  5. Azdo
    Replies:
    2
    Views:
    429
Loading...

Share This Page