Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ Function_Objects(3C++) — Sun WorkShop 5.0

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Function_Objects(3C++)

Standard C++ Library
Copyright 1998, Rogue Wave Software, Inc.

 

NAME

 
Function_Objects
 
 - Function objects are objects with an operator() defined.   They are used as arguments to templatized algorithms, in place of pointers to functions. 
 
 
 

SYNOPSIS

 
 

#include<functional>
 
 
 

DESCRIPTION

 
 
Function objects are objects with an operator() defined. They are important for the effective use of the standard library’s generic algorithms, because the interface for each algorithmic template can accept either an object with an operator() defined, or a pointer to a function. The Standard C++ Library includes both a standard set of function objects, and a pair of classes that you can use as the base for creating your own function objects. 
 
Function objects that take one argument are called unary function objects. Unary function objects must include the typedefs argument_type and result_type. Similarly, function objects that take two arguments are called binary function objects and, as such, must include the typedefs first_argument_type, second_argument_type, and result_type. 
 
The classes unary_function and binary_function make the task of creating templatized function objects easier. The necessary typedefs for a unary or binary function object are included by inheriting from the appropriate function object class. 
 
The function objects in the standard library are listed below, together with a brief description of their operation. This class reference also includes an alphabetic entry for each function.
 
 
 
NAMEOPERATION
 
 
arithmetic functions
 
plusaddition x + y
 
minussubtraction x - y
 
multipliesmultiplication x ∗ y
 
dividesdivision x / y
 
modulusremainder x % y
 
negatenegation - x
 
  comparison functions
 
equal_toequality test x == y
 
not_equal_toinequality test x != y
 
greatergreater comparison x > y
 
lessless-than comparison x < y
 
greater_equalgreater than or equal comparison x >= y
 
less_equalless than or equal comparison x <= y
 
 
logical functions
 
logical_andlogical conjunction x && y
 
logical_orlogical disjunction x || y
 
logical_notlogical negation ! x
 
 

INTERFACE

 
 
 

template <class Arg, class Result>
struct unary_function{

typedef Arg argument_type;
typedef Result result_type;

};

 

template <class Arg1, class Arg2, class Result>
struct binary_function{

typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;

};

 

// Arithmetic Operations

 

template<class T>
struct plus : binary_function<T, T, T> {

T operator() (const T&, const T&) const;

};
 
template <class T>
struct minus : binary_function<T, T, T> {

T operator() (const T&, const T&) const;

};
 
template <class T>
struct multiplies : binary_function<T, T, T> {

T operator() (const T&, const T&) const;

};
 
template <class T>
struct divides : binary_function<T, T, T> {

T operator() (const T&, const T&) const;

};
 
template <class T>
struct modulus : binary_function<T, T, T> {

T operator() (const T&, const T&) const;

};
 
template <class T>
struct negate : unary_function<T, T> {

T operator() (const T&) const;

};
 

// Comparisons

 
template <class T>
struct equal_to : binary_function<T, T, bool> {

bool operator() (const T&, const T&) const;

};
 
template <class T>
struct not_equal_to : binary_function<T, T, bool> {

bool operator() (const T&, const T&) const;

};
 
template <class T>
struct greater : binary_function<T, T, bool> {

bool operator() (const T&, const T&) const;

};
 
template <class T>
struct less : binary_function<T, T, bool> {

bool operator() (const T&, const T&) const;

};
 
template <class T>
struct greater_equal : binary_function<T, T, bool> {

bool operator() (const T&, const T&) const;

};
 
template <class T>
struct less_equal : binary_function<T, T, bool> {

bool operator() (const T&, const T&) const;

};
 

// Logical Comparisons

 
template <class T>
struct logical_and : binary_function<T, T, bool> {

bool operator() (const T&, const T&) const;

};
 
template <class T>
struct logical_or : binary_function<T, T, bool> {

bool operator() (const T&, const T&) const;

};
 
template <class T>
struct logical_not : unary_function<T, T, bool> {

bool operator() (const T&, const T&) const;

};
 
 
 

EXAMPLE

 
 
 
//
// funct_ob.cpp
//

#include<functional>
#include<deque>
#include<vector>
#include<algorithm>
#include <iostream>

using namespace std;
 

//Create a new function object from unary_function

template<class Arg>
class factorial : public unary_function<Arg, Arg>

{

public:

 

Arg operator()(const Arg& arg)

{

Arg a = 1;
for(Arg i = 2; i <= arg; i++)

a ∗= i;

return a;

}

};

 
int main()

{

//Initialize a deque with an array of ints

int init[7] = {1,2,3,4,5,6,7};
deque<int> d(init, init+7);

 

//Create an empty vector to store the factorials

vector<int> v((size_t)7);

 

//Transform the numbers in the deque to their factorials
//and store in the vector

transform(d.begin(), d.end(), v.begin(),

factorial<int>());

 

//Print the results

cout << "The following numbers: " << endl << "     ";
copy(d.begin(),d.end(),

ostream_iterator<int,char>(cout," "));

 

cout << endl << endl;
cout << "Have the factorials: " << endl << "     ";
copy(v.begin(),v.end(),

ostream_iterator<int,char>(cout," "));

 

return 0;

}
 

Program Output
 
 
 

 
The following numbers:

1 2 3 4 5 6 7

Have the factorials:

1 2 6 24 120 720 5040
 
 
 

WARNINGS

 
 
If your compiler does not support default template parameters, then you always need to supply the Allocator template argument. For instance, you have to write:
 
vector<int, allocator<int> > and deque<int, allocator<int> >
 
instead of:
 
vector<int> and deque<int>
 
If your compiler does not support namespaces, then you do not need the using declaration for std. 
 
 
 

SEE ALSO

 
 
binary_function, unary_function
 

Rogue Wave Software  —  Last change: 02 Apr 1998

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026