Connecting Tech Pros Worldwide Forums | Help | Site Map

beginners question on unary_function<>

Member
 
Join Date: Nov 2007
Posts: 57
#1: Jul 7 '09
Hi,

im failing with my first try to use unary_function, i got something (which i partly borrowed from another forum):

Expand|Select|Wrap|Line Numbers
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iostream>
  4. using namespace std;
  5. template <typename T> class is_good : public std::unary_function<T, bool> { 
  6. public:
  7.     is_good(const T & arg) : _val(arg) { }
  8.     ~is_good() { }
  9.     bool operator()(const T p) const {
  10.         return p == _val;
  11.     }
  12. private:
  13.     T _val;
  14. };
  15. //int doit ( 
  16. //    const int* a, 
  17. //    unsigned int size,
  18. //    std::unary_function< int, bool > condition ) 
  19. //{
  20. //    int sum = 0;
  21. //    for ( unsigned int i =0 ; i < size; ++i )
  22. //    {
  23. //        if ( condition(a[i]) )
  24. //            sum += a[i];
  25. //    }
  26. //    return sum;
  27. //}
  28.  
  29.  
  30. void main()
  31. {
  32.     int a[10] = {4, 9, 5, 6, 9, 10, 9, 255, 60, 0};
  33.     int* x = std::find_if(a, a+10, is_good<int>(10));
  34.     //doit( a, 10, is_good<int>(12) );
  35. }
which works, but when i comment in the 'doit' part the compiler complains, that condition(a[i]) does not evaluate to a function ....

i suppose its a trivial error?! anybody?

Banfa's Avatar
Administrator
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,460
#2: Jul 7 '09

re: beginners question on unary_function<>


std::unary_function< int, bool > is not a predicate, that is there is no member defined in unary_function

std::unary_function< int, bool >::operator()(...);

so a variable of that type can not be used in a call

condition(...)
Member
 
Join Date: Nov 2007
Posts: 57
#3: Jul 7 '09

re: beginners question on unary_function<>


alright!
thanks. of course

so now i do:
Expand|Select|Wrap|Line Numbers
  1. template< typename PREDICATE >
  2. int doit ( 
  3.     const int* a, 
  4.     unsigned int size,
  5.     PREDICATE predicate ) 
  6. {
  7.     int sum = 0;
  8.     for ( unsigned int i =0 ; i < size; ++i )
  9.     {
  10.         if ( predicate(a[i]) )
  11.             sum += a[i];
  12.     }
  13.     return sum;
  14. }
which does what i want. thanks banfa
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,553
#4: Jul 7 '09

re: beginners question on unary_function<>


I just wanted to be sure everyone knows the STL-speak for these terms:

1) Generator.
A function that takes no arguments.

2) unary function
A function that takes one argument.

If this function returns a bool is is called a predicate.

3) binary function
A function that takes two arguments.

If this function returns a bool it is called a binary predicate.


So you have to be careful reading STL documentation. All you will see, for example, is the word predicate. It is assumed you know this to be a unary function that returns a bool.

Also be aware that there are classes named unary_function and binary_function that are to help you in writing adaptable functors and function adapters:

Expand|Select|Wrap|Line Numbers
  1. template<class T>
  2. class Y : public unary_function<T,T>
  3. {
  4.  
  5. };
  6.  
  7. template<class T>
  8. class Z : public binary_function<T,T,T>
  9. {
  10.  
  11. };
These adaprtable functors and function adapters are needed since STL supports only zero, one or two arguments. If you need more, then you write an adaptable functor.
Member
 
Join Date: Nov 2007
Posts: 57
#5: Jul 8 '09

re: beginners question on unary_function<>


speaking of terminology, i notice that you emphasise the terms "adaptable functors" and "function adapters". whats behind that?

my guess is
- functor is a class with an operator()
- its called adaptable, if its derived from std::unary_function (or binary), ie has typedefs for args and returntypes

correct me if im wrong. and whats a function adapter?
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,553
#6: Jul 11 '09

re: beginners question on unary_function<>


binder1st is a function adapter.

That is, a function adapter is a class that assumes the existence of the typdefs for result_type, first_argument_type and second_argument_type.

If you have an adaptable function, you can use a function adapter to reduce the number of arguments in the adapable function. Like with multiplies. This is a binary function but you can use it as a unary function if you first create a function adapter object that contains one of the multiplies arguments as member data. The operator() of the function adapter object need only to have one argument (a unary function) since the other argument can be obtained from the function adapter member data. In this way you can multiply using a function with only one arguiment.
Member
 
Join Date: Nov 2007
Posts: 57
#7: Jul 12 '09

re: beginners question on unary_function<>


interessting.

thanks.
Reply