Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the benefit of inheriting from std::binary_function (or std::unary function)?

Tags:

c++

stl

What is the benefit of inheriting from std::binary_function (or std::unary_function)?

For example I have such code:

class Person {  public:     Person();     Person(int a, std::string n);     Person(const Person& src);      int age;     std::string name;  };   Person::Person()            : age(0)              , name("")                {};   Person::Person(int a, std::string n)  : age(a)  , name(n)  {};   Person::Person(const Person& src)  {    age = src.age;    name = src.name;  };   struct PersonPrint : public std::unary_function<Person, void>{    void operator() (Person p){      std::cout << " Person age: " << p.age                 << " name: " << p.name << std::endl;    }  };   struct PersonGreater : public std::binary_function<Person, Person, bool>{    bool operator()(const Person& p1, const Person p2){      if (p1.age > p2.age) return true;      if (p1.name.compare(p2.name) > 0) return true;      return false;    }  };   int main(int count, char** args)  {    std::vector<Person> personVec;    Person p1(10, "Person1");    Person p2(12, "Person2");    Person p3(12, "Person3");     personVec.push_back(p1);    personVec.push_back(p2);    personVec.push_back(p3);     std::cout << "before sort: " << std::endl;    std::for_each(personVec.begin(), personVec.end(), PersonPrint());    std::sort(personVec.begin(), personVec.end(), PersonGreater());    std::cout << "after: " << std::endl;    std::for_each(personVec.begin(), personVec.end(), PersonPrint());  } 

But I also could write this code without inheritance form std::unary_function/std::binary_function?

 struct PersonPrint {      void operator() (Person p) {          std::cout << " Person age: " << p.age << " name: " << p.name << std::endl;       }   };    struct PersonGreater {      bool operator()(const Person& p1, const Person p2) {          if (p1.age > p2.age) return true;           if (p1.name.compare(p2.name) > 0) return true;           return false;       }   }; 

UPDATED

std::binary_function and std::unary_function are deprecated as of C++11 see comment by @AlexandreC.

like image 703
Darius Kucinskas Avatar asked Mar 04 '09 10:03

Darius Kucinskas


People also ask

What is std :: Binary_function?

binary_function is a base class for creating function objects with two arguments. binary_function does not define operator(); it is expected that derived classes will define this. binary_function provides only three types - first_argument_type , second_argument_type and result_type - defined by the template parameters.

What is unary function in C++?

A Unary Function is a kind of function object: an object that is called as if it were an ordinary C++ function. A Unary Function is called with a single argument.


1 Answers

Inheritance from [unary|binary]_function just gives you an additional typedefs in your class:

For unary_function

argument_type result_type 

For binary_function

first_argument_type second_argument_type result_type  

Which are those types you pass to [unary|binary]_function. In your case there is no benefits.

If you ever going to use your Functors with other std Functors modificators like not1, bind1st you have to inherit from [unart|binart]_function.

And if you are going to store this template information for your purpose it is better to use ready solution.

like image 145
Mykola Golubyev Avatar answered Sep 24 '22 03:09

Mykola Golubyev