Click here to Skip to main content
Click here to Skip to main content
Go to top

A set of useful functors for creating and manipulating functors

, 3 Mar 2003
Rate this:
Please Sign up or sign in to vote.
Create functors from functions, member functions and manipulating functors.

Introduction

Functors (function objects) play an important role in the Standard Template Library (STL). I have created several functors and supporting functions to manipulate functors such that new functors which are slightly modified from existing functors can easily be produced without rewriting the existing functors. This is in line with the principle of code reuse.

Limitations

The following functors use partial specialization of template classes. Thus it cannot be compiled using Visual C++ since it does not support partial specialization. Anyone is welcome to port the code to Visual C++. You just need to change all the functors' name so that they will be unique, e.g. FunctionToFunctor1Arg, FunctionToFunctor2Arg, etc.

Conversion to functors

To manipulate functors, you must have functors in the first place. However a lot of useful code in legacy libraries are not functors. They are mostly functions. Some may be member functions. Even if there are functors, they may not be STL compliant (i.e. they do not typedef their return type and arguments). It is not always possible to convert these to STL compliant functors as the source code may not be available or it is very tedious to do manually.

To enable ease of converting these functions to STL compliant functors, four functors with partial specialization are created. In addition, an overloaded supporting function is provided to ease the usage of these four functors. The functors are: FunctionToFunctor, MemberFunctionToFunctor, ConstMemberFunctionToFunctor and FunctorToFunctor. The supporting function is to_functor. These functors are capable of converting functions, member functions or functors with up to ten arguments to the corresponding STL compliant functor. The number of arguments supporting is capped at ten because not a lot of functions have more than ten arguments.

Usage

The usage for FunctionToFunctor is FunctionToFunctor<Arg1T,Arg2T,....,ArgNT,RetT>, where Arg1T..ArgNT (N <=10) is the type for the various arguments of the function and RetT is the return type of the function. FunctionToFunctor's constructor requires the function's name.

The usage for MemberFunctionToFunctor is MemberFunctionToFunctor <ObjectT,Arg1T,Arg2T,....,ArgN,RetT>, where ObjectT is the type for the object containing the member function, Arg1T..ArgNT (N <=10) is the type for the various arguments of the function and RetT is the return type of the function. MemberFunctionToFunctor's constructor requires an instance of the object and the member function's name.

The usage for ConstMemberFunctionToFunctor is ConstMemberFunctionToFunctor <ObjectT,Arg1T,Arg2T,....,ArgN,RetT>, where ObjectT is the type for the object containing the member function, Arg1T..ArgNT (N <=10) is the type for the various arguments of the function and RetT is the return type of the function. ConstMemberFunctionToFunctor's constructor requires an instance of the object and the const member function's name.

The usage for FunctorToFunctor is FunctorToFunctor <FunctorT,Arg1T,Arg2T,....,ArgNT,RetT>, where FunctorT is the type for the functor, Arg1T..ArgNT (N <=10) is the type for the various arguments of the function and RetT is the return type of the function. FunctorToFunctor's constructor requires an instance of the functor.

The usage for to_functor is to_functor(function_name) for a function, to_functor(Object_instance, Object::function_name) for a member function and to_functor<Arg1T,Arg2T,...., ArgNT,RetT>(functor_instance) for a functor.

Example

  int Square(int x) {return x*x;}
  FunctionToFunctor<int,int> square(Square);
  cout << square(3) << endl;  // Output is 9
    
  void Cube(const double& x, double& y) {y = x*x*x;}
  FunctionToFunctor<const double&,double&,void> cube(Cube);
  double z = 0.0;
  cube(2,z);
  cout << z << endl;  // Output is 8
  
  struct Test
  {
      bool IsPositive(double x) {return x > 0;}
      bool IsEven(int x) const {return !(x%2);}
  };
  
  MemberFunctionToFunctor<Test,double,bool> isPos(Test(), Test::IsPositive);
  cout << isPos(3.2) << endl;  // Output is 1
  ConstMemberFunctionToFunctor<Test,int,bool> isEven(Test(), Test::IsEven);
  cout << isEven(4) << endl; // Output is 1
  
  struct IsNegative
  { 
      bool operator()(int x) {return x < 0;}
  };
  
  FunctorToFunctor<IsNegative,int,bool> isNeg(IsNegative());
  cout << isNeg(-2) << endl;  // Output is 1
  
  cout << to_functor(Square)(2) << endl; // Output is 4
  cout << to_functor(Test(), Test::IsPositive)(2.2) << endl; // Output is 1
  cout << to_functor<int,bool>(IsNegative())(4) << endl;  // Output is 0

Alter results from a functor

Sometimes it is useful to be able to manipulate results from a functor. Let's say you want to create a functor which is similar to an existing functor except that its result is always 5 times larger. To accomplish this, AlterFunctorResult was created.

Usage

The usage for AlterFunctorResult is AlterFunctorResult<FunctorT,Arg1T,...ArgNT,RetT,BinaryOperationT> where FunctorT, ArgNT has the same definition as above and BinaryOperationT is a functor which performs binary operations, taking in two inputs and outputs a single output. Examples are the STL plus<>, minus<>, multiplies<> and divides<>. AlterFunctorResult's constructor requires an instance of the functor to be modified, the input to modify the output of the existing functor and an instance of the binary operation functor.

Example

struct Cube  
{ 
  double operator()(double x) {return x*x;}  
}; 
AlterFunctorResult<Cube,double,double,multiplies<double> > 
cubeMultFive(Cube(), 5.0, multiplies<double>());  cout << 
cubeMultFive(3) << endl;  // Output is 135

Set arguments of functors with default values

The last set of functors perform the same operation as the functions bind1st and bind2nd of the STL library. That is, it allows an argument of a functor to have a default value. Thus when calling the functor, that argument can be left out. The limitation of the STL library is that it only supports functors with up to 2 arguments only. This set of functors is meant to complement the STL library by supporting functors with up to 10 arguments. By supporting up to 10 arguments, it also meant that the programmer can choose to bind from the first argument up to the tenth argument and not only the first two arguments.

Usage

The general format of the set of functors is BindFunctor??? where ??? is 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th or 10th. All the ten set of functors have the same syntax. For example, to use BindFunctor3rd on a functor with 4 arguments, you will use BindFunctor3rd<FunctorT,Arg1T,Arg2T,Arg3T,Arg4T,RetT> and the constructor requires an instance of the functor and the default value for the 3rd argument.

Example

struct BoxVolume
{
  int operator()(int length, int breadth, int height) 
     {return length * breadth * height;}
};
//Set breadth to be a constant 3
BindFunctor2nd<BoxVolume,int,int,int,int> boxvol(BoxVolume(), 3); 

cout << boxvol(2,5) << endl; // Output is 30
cout << boxvol(1,3) << endl; // Output is 9

Summary

The usefulness of these sets of functors depends on the imagination of the programmer. I believe it will be useful for generic programming as it will enable legacy C functions to be converted to functors which can then be manipulated in several ways before feeding into another function. This allows new functions to be created without recoding the existing functions.

Do let me know of any bugs in the code so that I may correct them. It is no joke trying to examine the code for minor coding errors as the codes are rather repetitive and boring to examine properly. I am also keen to learn of any desired improvement to the code.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Yap Chun Wei
Researcher
Singapore Singapore
No Biography provided

Comments and Discussions

 
GeneralWow Awesome Pinmemberpyrokins6-Nov-05 14:20 
GeneralThere are a set of such stuff in the existing STL PinmemberLai Shiaw San Kent9-Nov-03 20:46 
GeneralRe: There are a set of such stuff in the existing STL PinmemberYap Chun Wei10-Nov-03 15:17 
I know the existent of all these supporting functions. It just that they don't do some of the jobs that I need. For instance, how many arguments can the functors produced by these support functions take? The answer is not more than two. But the functors that I provide can support up to 10 arguments.
 
For the FunctorToFunctor, yes, you can automatically do that if you derive from unary_function and binary_function. But what does the name unary_function and binary_function tells you? They support only up to two arguments of course Poke tongue | ;-P If you have been programming for long, you will realise you sometimes need more arguments than that. Besides, there are a lot of times when you cannot change or don't want to change an existing class just so that it fits in to your new project. So what do you do? FunctorToFunctor can easily convert an existing class, without or without its source code available, to a standardize functor. You can then manipulate the standarize functor with the other classes like BindFunctor3rd to fit your current project without altering the original class. Isn't that neat Cool | :cool:
 
Anyway, if you can't think of how you can use these classes, then most probably you don't need them. As for me, I use them in my projects and it ease the creation of new functors without rewriting any code. I actually have an updated version of this class available but seeing that so few people actually know how to make use of this class, I am too lazy to put it up. Poke tongue | ;-P
Generalboost PinsussGoran Mitrovic5-Mar-03 12:21 
GeneralRe: boost PinmemberYap Chun Wei5-Mar-03 15:01 
GeneralRe: boost PinsussKlaus Nowikow5-Mar-03 23:39 
GeneralRe: boost PinmemberYap Chun Wei6-Mar-03 14:10 
GeneralRe: boost PinmemberCraig Henderson11-Mar-03 21:43 
GeneralRe: boost PinmemberYap Chun Wei12-Mar-03 16:13 
GeneralRe: boost PinmemberFocusedWolf5-Jun-06 12:38 
GeneralRe: boost PinmemberFocusedWolf1-Dec-06 5:54 
GeneralRe: boost PinsussKlaus Nowikow5-Mar-03 23:42 
GeneralRe: boost PinmemberWREY6-Mar-03 3:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 4 Mar 2003
Article Copyright 2003 by Yap Chun Wei
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid