Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Advanced
Dec 18, 2000

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 <functional>. 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;
};

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;
 }
};
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap