Question on usage of functional object.

Discussion in 'C++' started by hn.ft.pris@gmail.com, Jan 23, 2007.

  1. Guest

    I've got following code test C++'s functor. For the sake of
    easy-reading, I omit some declearations.



    #include <algorithm>
    #include <functional>

    using namespace std;

    template <typename T> class Sum{
    public:
    Sum(T i=0):sum(i){};
    inline void operator () (T x){
    sum += x;
    }
    inline T output() const{
    return sum;
    }

    private:
    T sum;
    };

    int main( void ){


    vector<int> vec(10, 1);

    Sum<int> sum;

    sum = for_each(vec.begin(), vec.end(), Sum<int>()); .......(1)
    sum = for_each(vec.begin(), vec.end(), sum); .........(2)
    sum = for_each(vec.begin(), vec.end(), sum.operator()(int) ); ...(3)

    cout << sum.output() << endl;

    return 1;
    }

    It's easy to understand that (2) works, because sum.operator()(int) is
    called implicitly. (1) also works, it confuses me. Does Sum<int>()
    create an implicit class Sum object? On the other hand, Sum<int>
    doesn't work.
    (3) fails, does it means the third argument of "for_each" couldn't be a
    function pointer? What will the code be if I want to pass a function
    pointer here? Thanks for help!
    , Jan 23, 2007
    #1
    1. Advertising

  2. On 22 Jan 2007 22:50:56 -0800, hn.ft.pris wrote:
    >I've got following code test C++'s functor. For the sake of
    >easy-reading, I omit some declearations.
    >
    >#include <algorithm>
    >#include <functional>
    >
    >using namespace std;
    >
    >template <typename T> class Sum{
    >public:
    > Sum(T i=0):sum(i){};
    > inline void operator () (T x){
    > sum += x;
    > }
    > inline T output() const{
    > return sum;
    > }
    >
    >private:
    > T sum;
    >};


    This is a (probably) problematic 'stateful predicate', see:
    http://www.gotw.ca/gotw/052.htm.

    >int main( void ){
    >
    >
    > vector<int> vec(10, 1);
    >
    > Sum<int> sum;
    >
    > sum = for_each(vec.begin(), vec.end(), Sum<int>()); .......(1)
    > sum = for_each(vec.begin(), vec.end(), sum); .........(2)
    > sum = for_each(vec.begin(), vec.end(), sum.operator()(int) ); ...(3)
    >
    > cout << sum.output() << endl;
    >
    > return 1;
    >}
    >
    >It's easy to understand that (2) works, because sum.operator()(int) is
    >called implicitly. (1) also works, it confuses me. Does Sum<int>()
    >create an implicit class Sum object? On the other hand, Sum<int>
    >doesn't work.
    >(3) fails, does it means the third argument of "for_each" couldn't be a
    >function pointer? What will the code be if I want to pass a function
    >pointer here? Thanks for help!


    The third argument in (3) is neither a function object nor a pointer
    to a (free) function.

    Best wishes,
    Roland Pibinger
    Roland Pibinger, Jan 23, 2007
    #2
    1. Advertising

  3. Noah Roberts Guest


    > sum = for_each(vec.begin(), vec.end(), sum.operator()(int) ); ...(3)


    > (3) fails, does it means the third argument of "for_each" couldn't be a
    > function pointer? What will the code be if I want to pass a function
    > pointer here? Thanks for help!


    sum.operator()(int) isn't valid. You can't take an address of a member
    function from an instance, you have to take it from the class. As such
    its signature changes from what it was, (int) in this case, to
    accepting a pointer to an instance of that class...so (Sum*, int) in
    this case. To use such in an algorithm you need a binder. I use TR1
    bind, which can be found as boost::bind.

    sum = for_each(vec.begin(), vec.end(), boost::bind(&Sum::eek:perator(),
    &sum, _1));
    Noah Roberts, Jan 23, 2007
    #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. metfan
    Replies:
    2
    Views:
    4,824
    Robert Olofsson
    Oct 21, 2003
  2. Colin J. Williams

    Webchecker Usage - a problem with local usage

    Colin J. Williams, Feb 25, 2004, in forum: Python
    Replies:
    1
    Views:
    510
    Colin J. Williams
    Feb 26, 2004
  3. Replies:
    3
    Views:
    263
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Jan 23, 2007
  4. hvt
    Replies:
    0
    Views:
    1,183
  5. hvt
    Replies:
    0
    Views:
    1,440
Loading...

Share This Page