DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

functional(C++std)


binary_function , binary_negate , bind1st , bind2nd , binder1st , binder2nd , const_mem_fun_t , const_mem_fun_ref_t , const_mem_fun1_t , const_mem_fun1_ref_t , divides , equal_to , greater , greater_equal , less , less_equal , logical_and , logical_not , logical_or , mem_fun , mem_fun_t , mem_fun_ref , mem_fun_ref_t , mem_fun1_t , mem_fun1_ref_t , minus , modulus , multiplies , negate , not1 , not2 , not_equal_to , plus , pointer_to_binary_function , pointer_to_unary_function , ptr_fun , unary_function , unary_negate - defines several templates that help construct predicates for the templates defined in <algorithm> and <numeric> (standard template library)

Synopsis

   namespace std {
   template<class Arg, class Result>
       struct unary_function;
   template<class Arg1, class Arg2, class Result>
       struct binary_function;
   template<class T>
       struct plus;
   template<class T>
       struct minus;
   template<class T>
       struct multiplies;
   template<class T>
       struct divides;
   template<class T>
       struct modulus;
   template<class T>
       struct negate;
   template<class T>
       struct equal_to;
   template<class T>
       struct not_equal_to;
   template<class T>
       struct greater;
   template<class T>
       struct less;
   template<class T>
       struct greater_equal;
   template<class T>
       struct less_equal;
   template<class T>
       struct logical_and;
   template<class T>
       struct logical_or;
   template<class T>
       struct logical_not;
   template<class Pred>
       struct unary_negate;
   template<class Pred>
       struct binary_negate;
   template<class Pred>
       class binder1st;
   template<class Pred>
       class binder2nd;
   template<class Arg, class Result>
       class pointer_to_unary_function;
   template<class Arg1, class Arg2, class Result>
       class pointer_to_binary_function;
   template<class R, class T>
       struct mem_fun_t;
   template<class R, class T, class A>
       struct mem_fun1_t;
   template<class R, class T>
       struct const_mem_fun_t;
   template<class R, class T, class A>
       struct const_mem_fun1_t;
   template<class R, class T>
       struct mem_fun_ref_t;
   template<class R, class T, class A>
       struct mem_fun1_ref_t;
   template<class R, class T>
       struct const_mem_fun_ref_t;
   template<class R, class T, class A>
       struct const_mem_fun1_ref_t;

           // TEMPLATE FUNCTIONS
   template<class Pred>
       unary_negate<Pred> not1(const Pred& pr);
   template<class Pred>
       binary_negate<Pred> not2(const Pred& pr);
   template<class Pred, class T>
       binder1st<Pred> bind1st(const Pred& pr, const T& x);
   template<class Pred, class T>
       binder2nd<Pred> bind2nd(const Pred& pr, const T& x);
   template<class Arg, class Result>
       pointer_to_unary_function<Arg, Result>
           ptr_fun(Result (*)(Arg));
   template<class Arg1, class Arg2, class Result>
       pointer_to_binary_function<Arg1, Arg2, Result>
           ptr_fun(Result (*)(Arg1, Arg2));
   template<class R, class T>
       mem_fun_t<R, T> mem_fun(R (T::*pm)());
   template<class R, class T, class A>
       mem_fun1_t<R, T, A> mem_fun(R (T::*pm)(A arg));
   template<class R, class T>
       const_mem_fun_t<R, T> mem_fun(R (T::*pm)() const);
   template<class R, class T, class A>
       const_mem_fun1_t<R, T, A> mem_fun(R (T::*pm)(A arg) const);
   template<class R, class T>
       mem_fun_ref_t<R, T> mem_fun_ref(R (T::*pm)());
   template<class R, class T, class A>
       mem_fun1_ref_t<R, T, A>
           mem_fun_ref(R (T::*pm)(A arg));
   template<class R, class T>
       const_mem_fun_ref_t<R, T> mem_fun_ref(R (T::*pm)() const);
   template<class R, class T, class A>
       const_mem_fun1_ref_t<R, T, A>
           mem_fun_ref(R (T::*pm)(A arg) const);
       };

Description

Include the STL standard header <functional> to define several templates that help construct function objects, objects of a type that defines operator(). A function object can thus be a function pointer, but in the more general case the object can store additional information that can be used during a function call.

binary_function

   template<class Arg1, class Arg2, class Result>
       struct binary_function {
       typedef Arg1 first_argument_type;
       typedef Arg2 second_argument_type;
       typedef Result result_type;
       };

The template class serves as a base for classes that define a member function of the form:

   result_type operator()(const first_argument_type&,
       const second_argument_type&) const

Hence, all such binary functions can refer to their first argument type as first_argument_type, their second argument type as second_argument_type, and their return type as result_type.

binary_negate

   template<class Pred>
       class binary_negate
           : public binary_function<
               typename Pred::first_argument_type,
               typename Pred::second_argument_type, bool> {
   public:
       explicit binary_negate(const Pred& pr);
       bool operator()(
           const typename Pred::first_argument_type& x,
           const typename Pred::second_argument_type& y) const;
       };

The template class stores a copy of pr, which must be a binary function object. It defines its member function operator() as returning !pr(x, y).

bind1st

   template<class Pred, class T>
       binder1st<Pred> bind1st(const Pred& pr, const T& x);

The function returns binder1st<Pred>(pr, typename Pred::first_argument_type(x)).

bind2nd

   template<class Pred, class T>
       binder2nd<Pred> bind2nd(const Pred& pr, const T& y);

The function returns binder2nd<Pred>(pr, typename Pred::second_argument_type(y)).

binder1st

   template<class Pred>
       class binder1st
           : public unary_function<
               typename Pred::second_argument_type,
               typename Pred::result_type> {
   public:
       typedef typename Pred::second_argument_type argument_type;
       typedef typename Pred::result_type result_type;
       binder1st(const Pred& pr,
           const typename Pred::first_argument_type& x);
       result_type operator()(const argument_type& y) const;
   protected:
       Pred op;
       typename Pred::first_argument_type value;
       };

The template class stores a copy of pr, which must be a binary function object, in op, and a copy of x in value. It defines its member function operator() as returning op(value, y).

binder2nd

   template<class Pred>
       class binder2nd
           : public unary_function<
               typename Pred::first_argument_type,
               typename Pred::result_type> {
   public:
       typedef typename Pred::first_argument_type argument_type;
       typedef typename Pred::result_type result_type;
       binder2nd(const Pred& pr,
           const typename Pred::second_argument_type& y);
       result_type operator()(const argument_type& x) const;
   protected:
       Pred op;
       typename Pred::second_argument_type value;
       };

The template class stores a copy of pr, which must be a binary function object, in op, and a copy of y in value. It defines its member function operator() as returning op(x, value).

const_mem_fun_t

   template<class R, class T>
       struct const_mem_fun_t
           : public unary_function<T *, R> {
       explicit const_mem_fun_t(R (T::*pm)() const);
       R operator()(const T *p) const;
       };

The template class stores a copy of pm, which must be a pointer to a member function of class T, in a private member object. It defines its member function operator() as returning (p->*pm)() const.

const_mem_fun_ref_t

   template<class R, class T>
       struct const_mem_fun_ref_t
           : public unary_function<T, R> {
       explicit const_mem_fun_t(R (T::*pm)() const);
       R operator()(const T& x) const;
       };

The template class stores a copy of pm, which must be a pointer to a member function of class T, in a private member object. It defines its member function operator() as returning (x.*Pm)() const.

const_mem_fun1_t

   template<class R, class T, class A>
       struct const_mem_fun1_t
           : public binary_function<T *, A, R> {
       explicit const_mem_fun1_t(R (T::*pm)(A) const);
       R operator()(const T *p, A arg) const;
       };

The template class stores a copy of pm, which must be a pointer to a member function of class T, in a private member object. It defines its member function operator() as returning (p->*pm)(arg) const.

const_mem_fun1_ref_t

   template<class R, class T, class A>
       struct const_mem_fun1_ref_t
           : public binary_function<T, A, R> {
       explicit const_mem_fun1_ref_t(R (T::*pm)(A) const);
       R operator()(const T& x, A arg) const;
       };

The template class stores a copy of pm, which must be a pointer to a member function of class T, in a private member object. It defines its member function operator() as returning (x.*pm)(arg) const.

divides

   template<class T>
       struct divides : public binary_function<T, T, T> {
       T operator()(const T& x, const T& y) const;
       };

The template class defines its member function as returning x / y.

equal_to

   template<class T>
       struct equal_to
           : public binary_function<T, T, bool> {
       bool operator()(const T& x, const T& y) const;
       };

The template class defines its member function as returning x == y.

greater

   template<class T>
       struct greater : public binary_function<T, T, bool> {
       bool operator()(const T& x, const T& y) const;
       };

The template class defines its member function as returning x > y. The member function defines a total ordering, even if T is an object pointer type.

greater_equal

   template<class T>
       struct greater_equal
           : public binary_function<T, T, bool> {
       bool operator()(const T& x, const T& y) const;
       };

The template class defines its member function as returning x >= y. The member function defines a total ordering, even if T is an object pointer type.

less

   template<class T>
       struct less : public binary_function<T, T, bool> {
       bool operator()(const T& x, const T& y) const;
       };

The template class defines its member function as returning x < y. The member function defines a total ordering, even if T is an object pointer type.

less_equal

   template<class T>
       struct less_equal
           : public binary_function<T, T, bool> {
       bool operator()(const T& x, const T& y) const;
       };

The template class defines its member function as returning x <= y. The member function defines a total ordering, even if T is an object pointer type.

logical_and

   template<class T>
       struct logical_and
           : public binary_function<T, T, bool> {
       bool operator()(const T& x, const T& y) const;
       };

The template class defines its member function as returning x && y.

logical_not

   template<class T>
       struct logical_not : public unary_function<T, bool> {
       bool operator()(const T& x) const;
       };

The template class defines its member function as returning !x.

logical_or

   template<class T>
       struct logical_or
           : public binary_function<T, T, bool> {
       bool operator()(const T& x, const T& y) const;
       };

The template class defines its member function as returning x || y.

mem_fun

   template<class R, class T>
       mem_fun_t<R, T> mem_fun(R (T::*pm)());
   template<class R, class T, class A>
       mem_fun1_t<R, T, A> mem_fun(R (T::*pm)(A));
   template<class R, class T>
       const_mem_fun_t<R, T>
           mem_fun(R (T::*pm)() const);
   template<class R, class T, class A>
       const_mem_fun1_t<R, T, A>
           mem_fun(R (T::*pm)(A) const);

The template function returns pm cast to the return type.

mem_fun_ref

   template<class R, class T>
       mem_fun_ref_t<R, T> mem_fun_ref(R (T::*pm)());
   template<class R, class T, class A>
       mem_fun1_ref_t<R, T, A> mem_fun_ref(R (T::*pm)(A));
   template<class R, class T>
       const_mem_fun_ref_t<R, T> mem_fun_ref(R (T::*pm)() const);
   template<class R, class T, class A>
       const_mem_fun1_ref_t<R, T, A> mem_fun_ref(R (T::*pm)(A) const);

The template function returns pm cast to the return type.

mem_fun_t

   template<class R, class T>
       struct mem_fun_t : public unary_function<T *, R> {
       explicit mem_fun_t(R (T::*pm)());
       R operator()(T *p) const;
       };

The template class stores a copy of pm, which must be a pointer to a member function of class T, in a private member object. It defines its member function operator() as returning (p->*pm)().

mem_fun_ref_t

   template<class R, class T>
       struct mem_fun_ref_t
           : public unary_function<T, R> {
       explicit mem_fun_t(R (T::*pm)());
       R operator()(T& x) const;
       };

The template class stores a copy of pm, which must be a pointer to a member function of class T, in a private member object. It defines its member function operator() as returning (x.*Pm)().

mem_fun1_t

   template<class R, class T, class A>
       struct mem_fun1_t
           : public binary_function<T *, A, R> {
       explicit mem_fun1_t(R (T::*pm)(A));
       R operator()(T *p, A arg) const;
       };

The template class stores a copy of pm, which must be a pointer to a member function of class T, in a private member object. It defines its member function operator() as returning (p->*pm)(arg).

mem_fun1_ref_t

   template<class R, class T, class A>
       struct mem_fun1_ref_t
           : public binary_function<T, A, R> {
       explicit mem_fun1_ref_t(R (T::*pm)(A));
       R operator()(T& x, A arg) const;
       };

The template class stores a copy of pm, which must be a pointer to a member function of class T, in a private member object. It defines its member function operator() as returning (x.*pm)(arg).

minus

   template<class T>
       struct minus : public binary_function<T, T, T> {
       T operator()(const T& x, const T& y) const;
       };

The template class defines its member function as returning x - y.

modulus

   template<class T>
       struct modulus : public binary_function<T, T, T> {
       T operator()(const T& x, const T& y) const;
       };

The template class defines its member function as returning x % y.

multiplies

   template<class T>
       struct multiplies : public binary_function<T, T, T> {
       T operator()(const T& x, const T& y) const;
       };

The template class defines its member function as returning x * y.

negate

   template<class T>
       struct negate : public unary_function<T, T> {
       T operator()(const T& x) const;
       };

The template class defines its member function as returning -x.

not1

   template<class Pred>
       unary_negate<Pred> not1(const Pred& pr);

The template function returns unary_negate<Pred>(pr).

not2

   template<class Pred>
       binary_negate<Pred> not2(const Pred& pr);

The template function returns binary_negate<Pred>(pr).

not_equal_to

   template<class T>
       struct not_equal_to
           : public binary_function<T, T, bool> {
       bool operator()(const T& x, const T& y) const;
       };

The template class defines its member function as returning x != y.

plus

   template<class T>
       struct plus : public binary_function<T, T, T> {
       T operator()(const T& x, const T& y) const;
       };

The template class defines its member function as returning x + y.

pointer_to_binary_function

   template<class Arg1, class Arg2, class Result>
       class pointer_to_binary_function
           : public binary_function<Arg1, Arg2, Result> {
   public:
       explicit pointer_to_binary_function(
           Result (*pf)(Arg1, Arg2));
       Result operator()(const Arg1 x, const Arg2 y) const;
       };

The template class stores a copy of pf. It defines its member function operator() as returning (*pf)(x, y).

pointer_to_unary_function

   template<class Arg, class Result>
       class pointer_to_unary_function
           : public unary_function<Arg, Result> {
   public:
       explicit pointer_to_unary_function(
           Result (*pf)(Arg));
       Result operator()(const Arg x) const;
       };

The template class stores a copy of pf. It defines its member function operator() as returning (*pf)(x).

ptr_fun

   template<class Arg, class Result>
       pointer_to_unary_function<Arg, Result>
           ptr_fun(Result (*pf)(Arg));
   template<class Arg1, class Arg2, class Result>
       pointer_to_binary_function<Arg1, Arg2, Result>
           ptr_fun(Result (*pf)(Arg1, Arg2));

The first template function returns pointer_to_unary_function<Arg, Result>(pf).

The second template function returns pointer_to_binary_function<Arg1, Arg2, Result>(pf).

unary_function

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

The template class serves as a base for classes that define a member function of the form:

   result_type operator()(const argument_type&) const

Hence, all such unary functions can refer to their sole argument type as argument_type and their return type as result_type.

unary_negate

   template<class Pred>
       class unary_negate
           : public unary_function<
               typename Pred::argument_type,
               bool> {
   public:
       explicit unary_negate(const Pred& pr);
       bool operator()(
           const typename Pred::argument_type& x) const;
       };

The template class stores a copy of pr, which must be a unary function object. It defines its member function operator() as returning !pr(x).

References

utility(C++std)
18 February 2000
© 2000 The Santa Cruz Operation, Inc. All rights reserved.

Copyright © 1992-1996 by P.J. Plauger. Portions derived from work copyright © 1994 by Hewlett-Packard Company. All rights reserved.