2

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?

As a final comment, are there any modern books on STL which serve as a good replacement for Meyers Effective STL? I have learned a lot from reading his book but there are probably more sections than I currently realize which contain obsolete information.

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225

1 Answers1

0

You can use one of these:

  • Lambda expression
  • std::bind
  • std::mem_fn

Does a lambda completely replace these "adapter functions".

Since C++14, lambdas can do everything that the deprecated and later removed functor templates can do.

If so, are function objects still useful post C++ 11?

Lambdas are function objects.

If you mean to ask whether it's still useful to write named function class templates: Yes, it's still occasionally useful.


Lambdas can be just as reusable by writing a named function that returns the lambda.

Can you add ... an example?

Here's an example:

constexpr auto
reusable_lambda() noexcept
{
    return []{
        return "Just an example";
    };
}
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Yes, my question was whether writing a named function object is useful. I guess "reusability" is the only use case? In other words, not repeating the same block of lambda in several places? – FreelanceConsultant Dec 29 '21 at 19:13
  • @FreelanceConsultant Lambdas can be just as reusable by writing a named function that returns the lambda. The difference is mostly in syntax. Sometimes you need to name the type of the functor, and that's where lambdas are a bit cumbersome. – eerorika Dec 29 '21 at 19:25
  • I've not seen that before. Can you add a link to a reference or alternatively an example? Again, it isn't something I have come across thus far. – FreelanceConsultant Dec 29 '21 at 19:28
  • @FreelanceConsultant See the example. – eerorika Dec 29 '21 at 19:34
  • That's quite interesting. So what is `reusable_lambda`? Is it an object like a class or struct? I guess not because it doesn't have any functions, just some kind of return statement which returns something which looks like a lamba function body. Is this essentially a C++ syntax sugar? – FreelanceConsultant Dec 29 '21 at 22:34
  • 1
    @FreelanceConsultant `reusable_lambda` is a function. It returns a functor. Lambda expression is essentially syntax sugar for a functor. – eerorika Dec 29 '21 at 22:42