parameter deduction in function object

Discussion in 'C++' started by Marcel Sebbao, Mar 29, 2005.

  1. I want to use a function object for various callbacks, and also as a
    normal function.

    But this function is a template. It is fine, but then I need to
    specify all the time the template
    parameter although it is always the same as the constructor argument:


    e.g:

    template<typename C>
    class func {
    const C& refClass;
    public:
    func(const C& aClass) : c(refClass) {}
    double operator () (const double& x) const {
    return c.(x);
    };
    };

    class myClass {
    int a,b;
    public:
    myClass(const int A, const int B) : a(A), b(B) {}
    double ez(const double& x) const {
    return pow(cos(x), a) + pow(sin(x), b);
    }
    };

    myClass mc(2,3);
    func<myClass> f(mc);
    for (int i=0; i<100; ++i)
    std::cout << f(i*0.01) << ' ';
    std::cout << std::endl;


    Well is there any way to declare func such as:
    func f(mc) ?
    Marcel Sebbao, Mar 29, 2005
    #1
    1. Advertising

  2. Marcel Sebbao

    David Harmon Guest

    On 29 Mar 2005 08:39:22 -0800 in comp.lang.c++,
    (Marcel Sebbao) wrote,
    >I want to use a function object for various callbacks, and also as a
    >normal function.
    >
    >But this function is a template. It is fine, but then I need to
    >specify all the time the template
    >parameter although it is always the same as the constructor argument:


    Parameter deduction is done for straight function templates, but
    not for class templates. That is the reason why, for example,
    std::bind1st() exists as a helper to construct a std::binder1st
    instance. Perhaps you want to follow that approach.
    David Harmon, Mar 29, 2005
    #2
    1. Advertising

  3. Marcel Sebbao wrote:
    > I want to use a function object for various callbacks, and also as a
    > normal function.
    >
    > But this function is a template. It is fine, but then I need to
    > specify all the time the template
    > parameter although it is always the same as the constructor argument:
    >
    >
    > e.g:
    >
    > template<typename C>
    > class func {
    > const C& refClass;
    > public:
    > func(const C& aClass) : c(refClass) {}
    > double operator () (const double& x) const {
    > return c.(x);

    ^
    What's the dot doing there? Did you mean to call a member function?

    > };
    > };
    >
    > class myClass {
    > int a,b;
    > public:
    > myClass(const int A, const int B) : a(A), b(B) {}
    > double ez(const double& x) const {
    > return pow(cos(x), a) + pow(sin(x), b);
    > }
    > };
    >
    > myClass mc(2,3);
    > func<myClass> f(mc);
    > for (int i=0; i<100; ++i)
    > std::cout << f(i*0.01) << ' ';
    > std::cout << std::endl;
    >
    >
    > Well is there any way to declare func such as:
    > func f(mc) ?


    No. 'func' is a template, you need template arguments to instantiate it.

    You _could_ wrap your loop in a function where 'f(mc)' is going to be one
    of the arguments, and then have a helper function which will return the
    object, something like

    template<class C> func<C> f_helper(const C& c) { return func<C>(c); }

    template<class F>
    void loop_wrapper(F f)
    {
    for (int i = 0; i < 100; ++i)
    std::cout << f(i*0.01) << " ";
    }

    ....
    myClass mc(2,3);
    loop_wrapper(f_helper(mc));

    V
    Victor Bazarov, Mar 29, 2005
    #3
  4. Marcel Sebbao

    Guest

    > return c.(x);

    ^
    > What's the dot doing there? Did you mean to call a member function?


    Sorry I meant "return c.ez(x);"

    Thanks for the propositions. I thought the use of the class in both the
    constructor and the template parameter was redundant. The wrapper idea
    is one I did not think about.
    , Mar 29, 2005
    #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. Neelesh

    Template parameter Deduction

    Neelesh, Nov 10, 2005, in forum: C++
    Replies:
    4
    Views:
    426
    John Carson
    Nov 10, 2005
  2. Fei Liu
    Replies:
    0
    Views:
    408
    Fei Liu
    Oct 25, 2007
  3. Fei Liu
    Replies:
    4
    Views:
    746
    Victor Bazarov
    Oct 26, 2007
  4. er
    Replies:
    5
    Views:
    1,228
  5. Replies:
    2
    Views:
    311
Loading...

Share This Page