jeudi 27 août 2020

Functional composition with std::bind

I am reading the second edition of the beautiful book of Nicolai Josuttis on C++11 STL.

I found the following piece of code:

#include <functional>
#include <iostream>

int main()
{
   auto plus10 = std::bind(std::plus<int>(),
      std::placeholders::_1,
      10);
   std::cout << "+10:    " << plus10(7) << std::endl;

   auto plus10times2 = std::bind(std::multiplies<int>(),
      std::bind(std::plus<int>(),
         std::placeholders::_1,
         10),
      2);
   // ...
}

I am not able to understand how the bind object "plus10times2" works. It should not bind to int parameters?

How can it bind another bind object? How it works when the call operator of plus10times2 is called (like plus10times2(7) for example)?

Aucun commentaire:

Enregistrer un commentaire