Topic: Function object operator() is too anonymous.


Author: boukanov@sentef1.fi.uib.no (Igor Boukanov)
Date: 1996/03/28
Raw View
   I noticed that that it impossible to write Function object class
(DWP 20.3) that can be used for example as Arithmetic operation object
and Predicate object in the same time, because both require operator()
that can not be overloaded! So why do not have instead of one anonymous
operator() a set of function obect methods like
unary_operation, binary_operation, predicate, comparison
and each function from algorithm.h will call correspondent method.
For example, find_if will call predicate method of function object and
transform will call unary_operation or binary_operation
instead of operator().

In this case one can write:

class F {
   ...
   bool predicate(int); // instead of bool operator()(int);
   int unary_operation(int); // instead of int operator()(int);
   int binary_operation(int, int); // instead of int operator()(int, int);
   bool comparison(int, int); // instead of bool operator()(int, int);
};

and somewhere use F:

vector<int> v1, v2;
F f;

...
std::find_if(v1.begin(), v1.end(), f);
 // f.predicate(int) will  be called
std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), f);
 // f.binary_operation(int, int) will be called
std::equal(v1.begin(), v1.end(), v2.begin(), f);
 // f.comparison(int, int) will be called

--
With best regards,
Igor Boukanov (igor.boukanov@fi.uib.no).


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: David Byrden <Goyra@iol.ie>
Date: 1996/03/28
Raw View
boukanov@sentef1.fi.uib.no (Igor Boukanov) wrote:

>   I noticed that that it impossible to write Function object class
>(DWP 20.3) that can be used for example as Arithmetic operation object
>and Predicate object in the same time, because both require operator()
>that can not be overloaded!

>So why do not have instead of one anonymous
>operator() a set of function obect methods like
>unary_operation, binary_operation, predicate, comparison
>and each function from algorithm.h will call correspondent method.
>For example, find_if will call predicate method of function object and
>transform will call unary_operation or binary_operation
>instead of operator().
>
>In this case one can write:
>
>class F {
>   ...
>   bool predicate(int); // instead of bool operator()(int);
>   int unary_operation(int); // instead of int operator()(int);
>   int binary_operation(int, int); // instead of int operator()(int, int);
>   bool comparison(int, int); // instead of bool operator()(int, int);
>};
>
>and somewhere use F:
>
>vector<int> v1, v2;
>F f;
>
>...
>std::find_if(v1.begin(), v1.end(), f);
> // f.predicate(int) will  be called
>std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), f);
> // f.binary_operation(int, int) will be called
>std::equal(v1.begin(), v1.end(), v2.begin(), f);
> // f.comparison(int, int) will be called
>


  Igor; perhaps you are not quite clear about how functors are used.

  They are interchangeable with ordinary function addresses; this would no
longer be possible if you were to replace operator() with a member
function called, for example, unary_operation, because function addresses
do not have it. For example;

  bool ordinary_function( int ) ;

  std::find_if(v1.begin(), v1.end(),  ordinary_function ) ;
  // works if the algorigthm calls operator()()
  // fails if the algorithm calls unary_operation()



   Also, there is no need to use a single functor class F in these two
cases, as you did;

 F f ;
 std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), f );
 std::equal(v1.begin(), v1.end(), v2.begin(), f );

you would normally use different functors, depending on what you want the
algorithm to do.

The library provides many interchangeable functors, and you can build a
collection of your own ones. There is no requirement to use only one
functor in all cases.



     The purpose of a functor is to be a "function with attached data", as
this example makes clear;

   find_if( v.begin(),  v.end(),  IsEqualTo(6)  ) ;

The functor is effectively a function which I have customised by attaching
the number 6 to it. So, I am not obliged to write a whole set of ordinary
functions like these;

   bool IsEqualTo6( int ) ;
   bool IsEqualTo7( int ) ;
   bool IsEqualTo8( int ) ;
   ..................


                            David
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]