CHAPTER 8

image

Functors

This chapter focuses on several techniques that help when writing (or when not writing) functors.

Most STL algorithms require compile-time function objects and this usually requires some manual coding:

struct Person{   unsigned int age;   std::string home_address;   double salary() const;};std::vector<Person> data;std::sort(data.begin(), data.end(), /* by age */ );std::partition(data.begin(), data.end(), /* by salary */ );

If you can modify Person, sometimes an elegant and quick solution is to write a public static member function and a member functor. This simultaneously attains the maximum efficiency and control, as your ...

Get Advanced Metaprogramming in Classic C++ now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.