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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.