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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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
Thanks for your registration, follow us on our social networks to keep up-to-date