Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Question on usage of functional object.

Reply
Thread Tools

Question on usage of functional object.

 
 
hn.ft.pris@gmail.com
Guest
Posts: n/a
 
      01-23-2007
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!

 
Reply With Quote
 
 
 
 
Roland Pibinger
Guest
Posts: n/a
 
      01-23-2007
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
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      01-23-2007


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
retrieving CPU Usage and Memory Usage information in JAVA hvt Java 0 03-13-2007 01:09 PM
retrieving CPU Usage and Memory Usage information in JAVA hvt Java 0 03-13-2007 01:07 PM
Question on usage of functional object. hn.ft.pris@gmail.com C++ 3 01-23-2007 12:34 PM
Webchecker Usage - a problem with local usage Colin J. Williams Python 1 02-26-2004 12:28 AM
Need help on memory usage VS PF usage metfan Java 2 10-21-2003 01:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57