Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #79076 > unrolled thread
| Started by | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| First post | 2021-04-19 11:23 +0200 |
| Last post | 2021-04-22 07:11 -0700 |
| Articles | 6 on this page of 26 — 12 participants |
Back to article view | Back to comp.lang.c++
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
Page 2 of 2 — ← Prev page 1 [2]
| From | Cholo Lennon <chololennon@hotmail.com> |
|---|---|
| Date | 2021-04-26 21:35 -0300 |
| Message-ID | <s67m7v$4ft$1@gioia.aioe.org> |
| In reply to | #79254 |
On 4/25/21 12:18 PM, MrSpook_Xh_gU9dm@pdj82ouz7mi_c.gov.uk wrote:
> On Fri, 23 Apr 2021 15:05:48 -0300
> Cholo Lennon <chololennon@hotmail.com> wrote:
>> 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:
>
> I'm confused why you think the 2nd example is clearer or neater, requiring
> as it does a 2nd class instead of handling everything in 1. But everyone has a
> prefered style I suppose, its just a matter of taste.
>
I am just showing different use cases, I am not saying than the 1st
example is better than the 2nd one (or vice versa), but the latter is
very common, for example, in GUI applications... your Window/Dialog
class handles the events from the contained widgets. You can use free
functions as event handlers, but member functions has the context of the
parent class (of course, std::bind is very powerful so you can add the
context to your free functions if you want), i.e:
class MyWindow: public Window {
Button okCancel;
EditBox name;
EditBox address;
SomeWindowContext context;
void onClick(Button& button) {
if (button.isCancel()) {
...
}
else {
...
}
}
void onFocus(EditBox& editBox) {
// Use window's context here
}
void onLostFocus(EditBox& editBox) {
// Use window's context here
}
void initWidgets() {
...
okCancel.onClick = std::bind(&MyWindows::onClick, this, _1);
// For simplicity, callbacks are reused for similar widgets
name.onFocus = std::bind(&MyWindows::onFocus, this, _1);
name.onLostFocus = std::bind(&MyWindows::onLostFocus, this, _1);
address.onFocus = std::bind(&MyWindows::onFocus, this, _1);
address.onLostFocus = std::bind(&MyWindows::onLostFocus, this, _1);
...
}
public:
MyWindow(...): ... {
initWidgets();
}
void show() {
...
}
};
C# uses the same scheme via delegates (the .Net std::function
counterpart). Java used to have a similar approach in Swing, but
anonymous classes were used to redirect a widget event to the parent
class callback. In modern C++ we can use a lambda to code the event or
to redirect it to the parent class. There are a lot of approaches, all
of them with pros and cons.
Usually, the initWidget member function (initializeComponent/jbInit in
C#/Java) are managed by the GUI editor/IDE, so the event connection (as
well as the widget configuration) is automatic. In the old days of MFC,
a message map built with macros was the initWidget equivalence. The map
was managed by the Class Wizard (*) tool.
I use the past tense to talk about MFC because my last contact with it
was 16 years ago :-O but I think Class Wizard (and the message map)
still exist.
--
Cholo Lennon
Bs.As.
ARG
[toc] | [prev] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2021-04-27 05:32 +0200 |
| Message-ID | <s680l3$2nj$1@dont-email.me> |
| In reply to | #79314 |
> // For simplicity, callbacks are reused for similar widgets
> name.onFocus = std::bind(&MyWindows::onFocus, this, _1);
> name.onLostFocus = std::bind(&MyWindows::onLostFocus, this, _1);
> address.onFocus = std::bind(&MyWindows::onFocus, this, _1);
> address.onLostFocus = std::bind(&MyWindows::onLostFocus, this, _1);
You would never use bind()-objects themselfes as callback-types because
the types are variable depending on the parameters. Usually you would
encapsulate them into a function-object which gives you the advantage,
that you can have any types of paramters with them, f.e.:
name.onFocus = function<void(param_type)>( bind(
&MyWindows::onFocus, this, _1 ) );
[toc] | [prev] | [next] | [standalone]
| From | Cholo Lennon <chololennon@hotmail.com> |
|---|---|
| Date | 2021-04-27 00:46 -0300 |
| Message-ID | <s681du$1u5q$1@gioia.aioe.org> |
| In reply to | #79317 |
On 4/27/21 12:32 AM, Bonita Montero wrote: >> // For simplicity, callbacks are reused for similar widgets >> name.onFocus = std::bind(&MyWindows::onFocus, this, _1); >> name.onLostFocus = std::bind(&MyWindows::onLostFocus, this, _1); >> address.onFocus = std::bind(&MyWindows::onFocus, this, _1); >> address.onLostFocus = std::bind(&MyWindows::onLostFocus, this, >> _1); > > You would never use bind()-objects themselfes as callback-types because > the types are variable depending on the parameters. Usually you would > encapsulate them into a function-object which gives you the advantage, > that you can have any types of paramters with them, f.e.: > name.onFocus = function<void(param_type)>( bind( > &MyWindows::onFocus, this, _1 ) ); But for my previous post it should be clear that onFocus/onLostFocus/etc are declared as std::function. For the sake of simplicity I didn't add the declaration/definition of these properties (because they belong to classes, Button and Editbox, whose code I didn't show) -- Cholo Lennon Bs.As. ARG
[toc] | [prev] | [next] | [standalone]
| From | MrSpook_1xajf_a1kl@f5mda.co.uk |
|---|---|
| Date | 2021-04-27 07:37 +0000 |
| Message-ID | <s68f01$13bo$1@gioia.aioe.org> |
| In reply to | #79314 |
On Mon, 26 Apr 2021 21:35:13 -0300 Cholo Lennon <chololennon@hotmail.com> wrote: >On 4/25/21 12:18 PM, MrSpook_Xh_gU9dm@pdj82ouz7mi_c.gov.uk wrote: >> On Fri, 23 Apr 2021 15:05:48 -0300 >> Cholo Lennon <chololennon@hotmail.com> wrote: >>> 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: >> >> I'm confused why you think the 2nd example is clearer or neater, requiring >> as it does a 2nd class instead of handling everything in 1. But everyone has >a >> prefered style I suppose, its just a matter of taste. >> > >I am just showing different use cases, I am not saying than the 1st >example is better than the 2nd one (or vice versa), but the latter is >very common, for example, in GUI applications... your Window/Dialog >class handles the events from the contained widgets. You can use free >functions as event handlers, but member functions has the context of the >parent class (of course, std::bind is very powerful so you can add the >context to your free functions if you want), i.e: Perhaps I'm just biased, but IMO if the answer is using std::bind() to link an object method to a function pointer then you're asking the wrong question. In simple code I'm sure it wouldn't be an issue, but in a complex project that may be hacked about by dozens of people over the years the chances the object may be deleted leaving a dangling function pointer elsewhere in the code grows much higher. >Usually, the initWidget member function (initializeComponent/jbInit in >C#/Java) are managed by the GUI editor/IDE, so the event connection (as >well as the widget configuration) is automatic. In the old days of MFC, >a message map built with macros was the initWidget equivalence. The map >was managed by the Class Wizard (*) tool. A much safer and simpler method on an event is simply to call a callback with the widget id. Event driven GUI code isn't speed critical so if safety and simplicity costs a few extra cpu cycles then its a trade worth making.
[toc] | [prev] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2021-04-22 10:35 -0700 |
| Message-ID | <s5sc56$f9c$1@dont-email.me> |
| In reply to | #79172 |
On 4/22/2021 9:25 AM, 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?
>
I'm not sure what the focal point of your question is. What is that
"this" you are asking about?
`std::bind` functionality in general? Applying `std::bind` to member
functions specifically? Something else?
--
Best regards,
Andrey Tarasevich
[toc] | [prev] | [next] | [standalone]
| From | Andrey Tarasevich <andreytarasevich@hotmail.com> |
|---|---|
| Date | 2021-04-22 07:11 -0700 |
| Message-ID | <s5s071$ga6$1@dont-email.me> |
| In reply to | #79076 |
On 4/19/2021 2:23 AM, Bonita Montero wrote:
> I just tested std::bind with a member-function trick
> and C++20 under MSVC:
>
> #include <functional>
> #include <utility>
>
> template<typename T, typename RetType, typename ... Args>
> auto obj_binder( T *this_, RetType (T::*fn)( Args ... ), Args &&...args )
> {
> using namespace std;
> auto binderFn = []<typename T, typename RetType, typename ...
> Args>( T * this_, RetType (T::*fn), Args &&...args ) -> RetType
> {
> return this_->*fn( forward<Args>( args ) ... );
> };
> return bind( binderFn, this_, fn, std::forward<Args>( args ) ... );
> }
>
> 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 = obj_binder<S, int>( &s, &S::sum, 4 );
> }
>
> Unfortunately the compiler crashes with this.
> C++20-support is still experimental with MSVC.
None of this has any relation to "C++20 support with MSVC" since the
code is obviously ill-formed. Reusing template parameter names inside a
template definition is prohibited in all versions of C++ language.
On top of that, the code is doing something nonsensical on top of
something even more nonsensical. I wanted to call it a "solution looking
for a problem", but I can't do even that since it makes no sense whatsoever.
> I just tested std::bind with a member-function trick
There's no such thing as "member-function trick" and there's no need for
any. `std::bind` works with member functions directly.
--
Best regards,
Andrey Tarasevich
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.c++
csiph-web