templated function as parameter of another templated function

Discussion in 'C++' started by Amadeus W. M., Jul 4, 2006.

  1. I have a bunch of templated functions:

    template <class Type_t>
    double f2(Type_t x) { return 2*x; }

    template <class Type_t>
    double f3(Type_t x) { return 3*x; }



    and a wrapper function, also templated, which I want to take f2 and
    f3 as arguments:

    template <class Type_t, double (*f)(Type_t)>
    double foobar(Type_t x) {
    double y=0, z=0;

    // compute something complicated from x first,
    // the same for all function pointers f.
    z = 4*x; // over-simplification here.

    // main computation.
    y=f(z);

    return y;
    }


    Then,

    void some_function(){
    double x=5, y=0;

    y=foobar<double,f2>(x);
    cout << y << endl;

    y=foobar<double,f3>(x);
    cout << y << endl;
    }

    works just fine.



    The problem is what happens when all these are members of a class:


    class A
    {
    public:

    template <class Type_t, double (*f)(Type_t)>
    double foobar(Type_t x) {
    double y=0, z=0;

    // compute something complicated from x first,
    // the same for all function pointers f.
    z = 4*x; // over-simplification here.

    // main computation.
    y=f(z);

    return y;
    }




    template <class Type_t>
    double f2(Type_t x) { return 2*x; }

    template <class Type_t>
    double f3(Type_t x) { return 3*x; }



    void some_function(){
    double x=5, y=0;

    y=foobar<double,A::f2>(x);
    cout << y << endl;

    y=foobar<double,A::f3>(x);
    cout << y << endl;
    }


    };


    This doesn't compile. With g++-4.0.2 I get these errors:

    g++ -g -Wall -o templtemplfunc templtemplfunc.C
    templtemplfunc.C: In member function 'void A::some_function()':
    templtemplfunc.C:39: error: no matching function for call to 'A::foobar(double&)'
    templtemplfunc.C:42: error: no matching function for call to 'A::foobar(double&)'

    Compilation exited abnormally with code 1 at Tue Jul 4 13:01:55


    How exactly do I call A::foobar() from within A::some_function()?
    Is what I want to do possible in the first place?

    Thanks!
     
    Amadeus W. M., Jul 4, 2006
    #1
    1. Advertising

  2. Amadeus W. M.

    Guest

    Amadeus W. M. wrote:
    > I have a bunch of templated functions:
    >
    > template <class Type_t>
    > double f2(Type_t x) { return 2*x; }
    >
    > template <class Type_t>
    > double f3(Type_t x) { return 3*x; }
    >
    >
    >
    > and a wrapper function, also templated, which I want to take f2 and
    > f3 as arguments:
    >
    > template <class Type_t, double (*f)(Type_t)>
    > double foobar(Type_t x) {
    > double y=0, z=0;
    >
    > // compute something complicated from x first,
    > // the same for all function pointers f.
    > z = 4*x; // over-simplification here.
    >
    > // main computation.
    > y=f(z);
    >
    > return y;
    > }
    >
    >
    > Then,
    >
    > void some_function(){
    > double x=5, y=0;
    >
    > y=foobar<double,f2>(x);
    > cout << y << endl;
    >
    > y=foobar<double,f3>(x);
    > cout << y << endl;
    > }
    >
    > works just fine.
    >
    >
    >
    > The problem is what happens when all these are members of a class:
    >
    >
    > class A
    > {
    > public:
    >
    > template <class Type_t, double (*f)(Type_t)>
    > double foobar(Type_t x) {
    > double y=0, z=0;
    >
    > // compute something complicated from x first,
    > // the same for all function pointers f.
    > z = 4*x; // over-simplification here.
    >
    > // main computation.
    > y=f(z);
    >
    > return y;
    > }
    >
    >
    >
    >
    > template <class Type_t>
    > double f2(Type_t x) { return 2*x; }
    >
    > template <class Type_t>
    > double f3(Type_t x) { return 3*x; }
    >
    >
    >
    > void some_function(){
    > double x=5, y=0;
    >
    > y=foobar<double,A::f2>(x);
    > cout << y << endl;
    >
    > y=foobar<double,A::f3>(x);
    > cout << y << endl;
    > }
    >
    >
    > };
    >
    >
    > This doesn't compile. With g++-4.0.2 I get these errors:
    >
    > g++ -g -Wall -o templtemplfunc templtemplfunc.C
    > templtemplfunc.C: In member function 'void A::some_function()':
    > templtemplfunc.C:39: error: no matching function for call to 'A::foobar(double&)'
    > templtemplfunc.C:42: error: no matching function for call to 'A::foobar(double&)'
    >
    > Compilation exited abnormally with code 1 at Tue Jul 4 13:01:55
    >
    >
    > How exactly do I call A::foobar() from within A::some_function()?
    > Is what I want to do possible in the first place?
    >
    > Thanks!


    Member functions wouuldnt work in that manner because member functions
    have an inherent "this" paramter and therefore the function signatures
    are different from a regular function.

    So, you can either make the member function as static and pass the
    object as a parameter or else use mem_fn adaptor.
     
    , Jul 4, 2006
    #2
    1. Advertising

  3. On Tue, 04 Jul 2006 12:41:00 -0700, amparikh wrote:

    >
    > Amadeus W. M. wrote:
    >> I have a bunch of templated functions:
    >>
    >> template <class Type_t>
    >> double f2(Type_t x) { return 2*x; }
    >>
    >> template <class Type_t>
    >> double f3(Type_t x) { return 3*x; }
    >>
    >>
    >>
    >> and a wrapper function, also templated, which I want to take f2 and
    >> f3 as arguments:
    >>
    >> template <class Type_t, double (*f)(Type_t)>
    >> double foobar(Type_t x) {
    >> double y=0, z=0;
    >>
    >> // compute something complicated from x first,
    >> // the same for all function pointers f.
    >> z = 4*x; // over-simplification here.
    >>
    >> // main computation.
    >> y=f(z);
    >>
    >> return y;
    >> }
    >>
    >>
    >> Then,
    >>
    >> void some_function(){
    >> double x=5, y=0;
    >>
    >> y=foobar<double,f2>(x);
    >> cout << y << endl;
    >>
    >> y=foobar<double,f3>(x);
    >> cout << y << endl;
    >> }
    >>
    >> works just fine.
    >>
    >>
    >>
    >> The problem is what happens when all these are members of a class:
    >>
    >>
    >> class A
    >> {
    >> public:
    >>
    >> template <class Type_t, double (*f)(Type_t)>
    >> double foobar(Type_t x) {
    >> double y=0, z=0;
    >>
    >> // compute something complicated from x first,
    >> // the same for all function pointers f.
    >> z = 4*x; // over-simplification here.
    >>
    >> // main computation.
    >> y=f(z);
    >>
    >> return y;
    >> }
    >>
    >>
    >>
    >>
    >> template <class Type_t>
    >> double f2(Type_t x) { return 2*x; }
    >>
    >> template <class Type_t>
    >> double f3(Type_t x) { return 3*x; }
    >>
    >>
    >>
    >> void some_function(){
    >> double x=5, y=0;
    >>
    >> y=foobar<double,A::f2>(x);
    >> cout << y << endl;
    >>
    >> y=foobar<double,A::f3>(x);
    >> cout << y << endl;
    >> }
    >>
    >>
    >> };
    >>
    >>
    >> This doesn't compile. With g++-4.0.2 I get these errors:
    >>
    >> g++ -g -Wall -o templtemplfunc templtemplfunc.C
    >> templtemplfunc.C: In member function 'void A::some_function()':
    >> templtemplfunc.C:39: error: no matching function for call to 'A::foobar(double&)'
    >> templtemplfunc.C:42: error: no matching function for call to 'A::foobar(double&)'
    >>
    >> Compilation exited abnormally with code 1 at Tue Jul 4 13:01:55
    >>
    >>
    >> How exactly do I call A::foobar() from within A::some_function()?
    >> Is what I want to do possible in the first place?
    >>
    >> Thanks!

    >
    > Member functions wouuldnt work in that manner because member functions
    > have an inherent "this" paramter and therefore the function signatures
    > are different from a regular function.
    >


    I found that out too. A member function must have an object to be
    called upon. To call a member without an object, the member must be static.

    > So, you can either make the member function as static and pass the
    > object as a parameter or else use mem_fn adaptor.


    I made the f2 and f3 functions in the above example static members, and
    all is well, without passing an object as parameter.

    Thanks!
     
    Amadeus W. M., Jul 4, 2006
    #3
    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. RA Scheltema
    Replies:
    3
    Views:
    410
    RA Scheltema
    Jan 6, 2004
  2. Marijn
    Replies:
    5
    Views:
    479
    Marijn
    Feb 13, 2004
  3. Replies:
    0
    Views:
    2,246
  4. Replies:
    1
    Views:
    310
  5. chhenning
    Replies:
    5
    Views:
    373
    chhenning
    Feb 13, 2008
Loading...

Share This Page