mercredi 29 décembre 2021

unary_function and binary_function are deprecated in C++ 11, and removed in C++ 17. What should we use instead? [duplicate]

I have been reading Effective STL by Meyers.

I came across some sections which mention function adapter objects, such as not1, bind1st, bind2nd. There are apparently a range of such function adapter objects, however I have never encountered these before. Another example is mem_fun and mem_fun_ref.

Many of these inherit from unary_function and binary_function. Some links are provided below.

https://en.cppreference.com/w/cpp/utility/functional/unary_function

https://en.cppreference.com/w/cpp/utility/functional/binary_function

https://en.cppreference.com/w/cpp/utility/functional/mem_fun

These objects are depreciated in C++ 11, which probably explains why I had not encountered them before.

Why are they deprecated, and what replaces them? My instinct tells me that a lambda can replace such function objects, and while reading the book it did occur to me that some of the sorting operations on STL containers can be described using a lambda instead of a function object.

  • Does a lambda completely replace these "adapter functions".
  • If so, are function objects still useful post C++ 11? If they have been made obsolete in the STL, does this mean function objects are obsolete everywhere in C++ 11 or do they still have uses?

Aucun commentaire:

Enregistrer un commentaire