Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » Make THISFN simpler and more powerful (by taking advantage of some new c++ feature)
Make THISFN simpler and more powerful [message #59037] |
Tue, 18 October 2022 21:08  |
Lance
Messages: 656 Registered: March 2007
|
Contributor |
|
|
in Core/Function.h, we have THISFN defined like this
template <class Ptr, class Class, class Res, class... ArgTypes>
Function<Res (ArgTypes...)> MemFn(Ptr object, Res (Class::*method)(ArgTypes...))
{
return [=](ArgTypes... args) { return (object->*method)(args...); };
}
#define THISFN(x) MemFn(this, &CLASSNAME::x)
This can be done in C++20 with the following
#define THISFN(memfun) std::bind_front(&std::remove_cvref_t<decltype(*this)>::memfun, this)
Of course this involves only library feature (no core language features), so it should be able to be rewritten to make available in current versons of c++ compilers/libraries that UPP aims to conform to atm.
An additional benefit of above macro definition is that it relieves the library user of the burden of an otherwise useless typedef
class MyClass
{
typedef MyClass CLASSNAME;
//....
}
to be able to use thisfn
BTW, thisfn is provided in the UPP library for a good reason: there are occasions where using thisfn is so much easier than using an lambda.
eg.
menu.Set( THISFN(MenuMain) );
vs
menu.Set ( [=, this] ( Bar & bar )
{
MenuMain ( bar );
}
);
And imagine when the callback accept a few more parameters.
[/code]
U++ is currently aiming at compliance to C++14. What if you want to use it in your project and you know your compiler/c++library are modern enough?
One way is to put the following in a header file and make sure you include it after <Core/Core.h> is included.
#ifdef THISFN
# undef THISFN
# define THISFN(memfun) ....
#endif
Reference:
Why use `std::bind_front` over lambdas in C++20?
[Updated on: Tue, 18 October 2022 21:19] Report message to a moderator
|
|
|
 |
 |
Make THISFN simpler and more powerful
By: Lance on Tue, 18 October 2022 21:08
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Thu, 10 November 2022 00:32
|
 |
|
Re: Make THISFN simpler and more powerful
By: Oblivion on Thu, 10 November 2022 06:29
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Thu, 10 November 2022 12:52
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Thu, 10 November 2022 21:41
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Fri, 11 November 2022 02:50
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Tue, 08 October 2024 18:38
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Tue, 08 October 2024 18:42
|
 |
|
Re: Make THISFN simpler and more powerful
By: Didier on Tue, 08 October 2024 19:25
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Tue, 08 October 2024 20:01
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Wed, 09 October 2024 05:59
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Wed, 09 October 2024 16:09
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Wed, 09 October 2024 16:17
|
 |
|
Re: Make THISFN simpler and more powerful
By: Didier on Wed, 09 October 2024 19:32
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Thu, 10 October 2024 14:01
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Thu, 10 October 2024 14:06
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Tue, 05 November 2024 02:25
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Tue, 05 November 2024 02:33
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Thu, 07 November 2024 00:49
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Fri, 29 November 2024 02:30
|
Goto Forum:
Current Time: Fri May 09 15:40:04 CEST 2025
Total time taken to generate the page: 0.03793 seconds
|