devxlogo

Function Object Bases

Function Object Bases

To simplify the process of writing custom function objects, the Standard Library provides two classes that serve as base classes of such objects: std::unary_function and std::binary_function. Both are declared in the header . As the names suggest, unary_function serves as the base of function objects taking one argument and binary_function serves as the base of function objects taking two arguments. Here’s the definition of these base classes:

 template  struct unary_function { typedef Arg argument_type; typedef Res result_type;};template  struct binary_function { typedef Arg first_argument_type; typedef Arg2 second_argument_type; typedef Res result_type;};

size=3>
These classes don’t provide any useful functionality. They are used to ensure that arguments and return values have uniform names. In the following example, a predicate (a function object returning bool) called is_vowel, which takes one argument, inherits from unary_function:

 template  class is_vowel: public unary_function{public: bool operator ()(T t) const {  if ((t=='a')||(t=='e')||(t=='i')||(t=='o')||(t=='u'))   return true;  return false; }};

size=3>

devx-admin

Share the Post: