Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #79203

Re: std::bind with member functions and C++20

From Cholo Lennon <chololennon@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: std::bind with member functions and C++20
Date 2021-04-23 15:05 -0300
Organization Aioe.org NNTP Server
Message-ID <s5v29s$t5k$1@gioia.aioe.org> (permalink)
References (3 earlier) <s5s08n$h9s$1@dont-email.me> <s5s612$u5b$1@dont-email.me> <s5s81b$53p$1@gioia.aioe.org> <s5sb20$1onq$1@gioia.aioe.org> <s5u2ri$1eti$1@gioia.aioe.org>

Show all headers | View raw


On 4/23/21 6:09 AM, MrSpook_wqqeppl@_nb0l03nl53p.tv wrote:
> On Thu, 22 Apr 2021 14:16:49 -0300
> Cholo Lennon <chololennon@hotmail.com> wrote:
>> On 4/22/21 1:25 PM, MrSpook_tm0@i9d.com wrote:
>>> On Thu, 22 Apr 2021 17:50:56 +0200
>>> Bonita Montero <Bonita.Montero@gmail.com> wrote:
>>>>> Huh?
>>>>>
>>>>>     int main()
>>>>>     {
>>>>>         struct S
>>>>>         {
>>>>>             int a, b, c;
>>>>>             int sum( int d )
>>>>>             {
>>>>>                 return a + b + c + d;
>>>>>             }
>>>>>         };
>>>>>         S s;
>>>>>         s.a = s.b = s.c = 1;
>>>>>
>>>>>         auto bound = std::bind(&S::sum, &s, 4);
>>>>>         return bound();
>>>>>     }
>>>>
>>>> Coooooool, I didn't know that this works.
>>>> YOU'RE MY HERO !
>>>
>>> I must be missing the point. When would this ever be used?
>>>
>>
>> Perhaps it's old fashioned, but a common use case for this it's a
>> callback event style similar to C# delegates. Instead of using virtual
>> functions, your class has several "delegates" modeled by std::function.
>> A user of your class can connect his/her callbacks to your delegates
>> using for that std::bind, which can accept any kind of callable object
>> (free functions, functors, member functions, lambdas, etc) with the
>> optional plus of binding/adding extra predefined values to your callback.
> 
> Ugh. Obfuscated code for the sake of being clever.
> 

Why obfuscated? why "clever"? on the contrary, this way of using events 
and callback is very common. Also, it's very simple and more flexible 
than callbacks based on virtual functions, here is a small contrived 
example:


#include <functional>
#include <iostream>
#include <string>


class Foo {
public:
     using event_type = std::function<
	void(Foo&, const std::string& some_event_data)>;

     event_type onStart;
     event_type onStop;


     void start() {
         if (onStart) onStart(*this, "start event data");

         // ...
     }

     void stop() {
         if (onStop) onStop(*this, "stop event data");

         // ...
     }
};


////////////////////////////////////////////////////
class EventHandler {
     Foo foo_;

public:
     EventHandler() {
         using namespace std::placeholders;

         foo_.onStart = std::bind(&EventHandler::onStart, this, _1, _2);
         foo_.onStop = std::bind(&EventHandler::onStop, this, _1, _2);

         foo_.start();
     }

     ~EventHandler() {
         foo_.stop();
     }

     void onStart(Foo&, const std::string& data) {
         std::cout << "on start: " << data << '\n';
     }

     void onStop(Foo&, const std::string& data) {
         std::cout << "on start: " << data << '\n';
     }
};


////////////////////////////////////////////////////
void onMyStart(Foo&, const std::string& data) {
     std::cout << "on start: " << data << '\n';
}

void onMyStop(Foo&, const std::string& data) {
     std::cout << "on stot: " << data << '\n';
}


////////////////////////////////////////////////////
int main() {
     // Example 1 with free function callbacks
     Foo foo;

     foo.onStart = onMyStart;
     foo.onStop = onMyStop;

     foo.start();
     foo.stop();

     // Example 2 with member function callbacks
     EventHandler handler;
}


--
Cholo Lennon
Bs.As.
ARG


Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

std::bind with member functions and C++20 Bonita Montero <Bonita.Montero@gmail.com> - 2021-04-19 11:23 +0200
  Re: std::bind with member functions and C++20 MrSpook_78@xvdv0t9masednkplrcjyxfsl.tv - 2021-04-19 09:32 +0000
    Re: std::bind with member functions and C++20 Bonita Montero <Bonita.Montero@gmail.com> - 2021-04-19 13:04 +0200
      Re: std::bind with member functions and C++20 Öö Tiib <ootiib@hot.ee> - 2021-04-21 23:59 -0700
        Re: std::bind with member functions and C++20 Bonita Montero <Bonita.Montero@gmail.com> - 2021-04-22 10:06 +0200
          Re: std::bind with member functions and C++20 Melzzzzz <Melzzzzz@zzzzz.com> - 2021-04-22 08:14 +0000
            Re: std::bind with member functions and C++20 "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-04-22 01:20 -0700
            Re: std::bind with member functions and C++20 Bonita Montero <Bonita.Montero@gmail.com> - 2021-04-22 11:32 +0200
      Re: std::bind with member functions and C++20 Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-22 07:12 -0700
        Re: std::bind with member functions and C++20 Bonita Montero <Bonita.Montero@gmail.com> - 2021-04-22 17:50 +0200
          Re: std::bind with member functions and C++20 MrSpook_tm0@i9d.com - 2021-04-22 16:25 +0000
            Re: std::bind with member functions and C++20 Bonita Montero <Bonita.Montero@gmail.com> - 2021-04-22 19:02 +0200
              Re: std::bind with member functions and C++20 Bonita Montero <Bonita.Montero@gmail.com> - 2021-04-22 19:25 +0200
            Re: std::bind with member functions and C++20 Cholo Lennon <chololennon@hotmail.com> - 2021-04-22 14:16 -0300
              Re: std::bind with member functions and C++20 MrSpook_wqqeppl@_nb0l03nl53p.tv - 2021-04-23 09:09 +0000
                Re: std::bind with member functions and C++20 Cholo Lennon <chololennon@hotmail.com> - 2021-04-23 15:05 -0300
                Re: std::bind with member functions and C++20 Cholo Lennon <chololennon@hotmail.com> - 2021-04-23 15:47 -0300
                Re: std::bind with member functions and C++20 MrSpook_Xh_gU9dm@pdj82ouz7mi_c.gov.uk - 2021-04-25 15:18 +0000
                Re: std::bind with member functions and C++20 Öö Tiib <ootiib@hot.ee> - 2021-04-25 10:07 -0700
                Re: std::bind with member functions and C++20 MrSpook_2p4eH@0kd0c4bkwzno.biz - 2021-04-26 07:40 +0000
                Re: std::bind with member functions and C++20 Cholo Lennon <chololennon@hotmail.com> - 2021-04-26 21:35 -0300
                Re: std::bind with member functions and C++20 Bonita Montero <Bonita.Montero@gmail.com> - 2021-04-27 05:32 +0200
                Re: std::bind with member functions and C++20 Cholo Lennon <chololennon@hotmail.com> - 2021-04-27 00:46 -0300
                Re: std::bind with member functions and C++20 MrSpook_1xajf_a1kl@f5mda.co.uk - 2021-04-27 07:37 +0000
            Re: std::bind with member functions and C++20 Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-22 10:35 -0700
  Re: std::bind with member functions and C++20 Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-22 07:11 -0700

csiph-web