cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : Miscellaneous : functional : unary_function
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
functional
iterator
memory
utility
functional
binary_function
unary_function
operator classes:
· divides
· equal_to
· greater
· greater_equal
· less
· less_equal
· logical_and
· logical_not
· logical_or
· minus
· modulus
· multiplies
· negate
· not_equal_to
· plus
adaptor functions:
· bind1st
· bind2nd
· mem_fun
· mem_fun_ref
· not1
· not2
· ptr_fun
types:
· binary_negate
· binder1st
· binder2nd
· const_mem_fun1_ref_t
· const_mem_fun1_t
· const_mem_fun_ref_t
· const_mem_fun_t
· mem_fun1_ref_t
· mem_fun1_t
· mem_fun_ref_t
· mem_fun_t
· pointer_to_binary_funct...
· pointer_to_unary_function
· unary_negate

-

unary_function class template
template <class Arg, class Result> struct unary_function;
<functional>

Unary function object base class

This is a base class for standard unary function objects.

Generically, function objects are instances of a class with member function operator() defined. This member function allows the object to be used with the same syntax as a regular function call, and therefore it can be used in templates instead of a pointer to a function.

In the case of unary function objects, this operator() member function takes one single parameter.

unary_function is just a base class, from which specific unary function objects are derived. It has no operator() member defined (derived classes are expected to define this) - it simply has two public data members that are typedefs of the template parameters. It is defined as:

template <class Arg, class Result>
  struct unary_function {
    typedef Arg argument_type;
    typedef Result result_type;
  };

Members

argument_type
Alias of the first template parameter, which is the type for the argument in member operator().
result_type
Alias of the second template parameter, which is the return type in member operator().

Example

// unary_function example
#include <iostream>
#include <functional>
using namespace std;

struct IsOdd : public unary_function<int,bool> {
  bool operator() (int number) {return (number%2==1);}
};

int main () {
  IsOdd IsOdd_object;
  IsOdd::argument_type input;
  IsOdd::result_type result;

  cout << "Please enter a number: ";
  cin >> input;

  result = IsOdd_object (input);

  cout << "Number " << input << " is " << (result?"odd":"even") << ".\n";

  return 0;
}

Possible output:


Please enter a number: 2
Number 2 is even.

See also

binary_function Binary function object base class (class template)

Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us