Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Question on usage of functional object. (http://www.velocityreviews.com/forums/t459986-question-on-usage-of-functional-object.html)

 hn.ft.pris@gmail.com 01-23-2007 06:50 AM

Question on usage of functional object.

I've got following code test C++'s functor. For the sake of

#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!

 Roland Pibinger 01-23-2007 10:05 AM

Re: Question on usage of functional object.

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
>
>#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

 Noah Roberts 01-23-2007 03:49 PM

Re: Question on usage of functional object.

> 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::operator(),
&sum, _1));

 All times are GMT. The time now is 09:42 PM.