0

How to convert lambda to member function pointer

Hi Refer code below: I want to convert lambda function to member function pointer so that I can get rid off the error I have. How to do so? https://sololearn.com/compiler-playground/cRM9thuwiZdg/?ref=app

19th Jul 2024, 8:57 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
15 Answers
+ 3
You faced the error because a lambda cannot be directly converted to a member function pointer. Member function pointers must point to actual member functions within a class, while lambdas are instances of anonymous classes with an overloaded operator(). To solve this, you need to: Store the lambda in a member variable of the class. Provide a member function that can call the stored lambda. https://sololearn.com/compiler-playground/ccIr3UMfBR3Z/?ref=app
19th Jul 2024, 9:30 AM
✧GHOST✧
✧GHOST✧ - avatar
+ 1
Thanks 😊
19th Jul 2024, 9:42 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
You're facing an error because `fn1` expects a member function pointer (`void(Test::*)()`), but you're trying to use `std::bind` or a lambda which are not directly compatible. Use a Lambda: Create a lambda that captures the necessary data and performs the required function call. Change `fn1` to Use `std::function`: Modify `fn1` to accept `std::function<void()>` to handle lambdas and `std::bind` directly. If you can't modify `fn1`, use a static member function to call the lambda indirectly. include <iostream> #include <functional> using namespace std; class Test { public: void display() { cout << "display\n"; } void display(int b) { cout << b << " display\n"; } }; void fn1(Test* ptr, std::function<void()> func) { func(); } int main() { Test ob; auto lambdaDisplay = [&ob]() { ob.display(5); }; fn1(&ob, lambdaDisplay); return 0; }
19th Jul 2024, 10:25 AM
✧GHOST✧
✧GHOST✧ - avatar
19th Jul 2024, 12:18 PM
✧GHOST✧
✧GHOST✧ - avatar
+ 1
with multiple ways to alter data applied as monkeypatches, it feels like your codebase will be full of backdoors , tunnels and secret entrances...
19th Jul 2024, 3:18 PM
Bob_Li
Bob_Li - avatar
0
Actually, my issue was something different. Refer code below: It has only a function which takes function pointer as an argument. This function pointer is of member function and taking no argument and returning nothing. This code is in ownership of others and cannot be changed. (fn1 in shared code). So, I was doing a storage of int into class, setting it and using member variable inside function. I got review comment that we should not be storing this as class member to avoid memory usage for each class objects. This forced me to use lambda instead of member function where I can pass argument to function call easily and avoid storing int value in class. Your solution is solving my problem in a way, but it is asking me to store a variable into class for function pointer. This is same overhead . Is there a way to avoid it? Many thanks for your suggestion and appreciate it. https://sololearn.com/compiler-playground/cVb9v4MCi44a/?ref=app
19th Jul 2024, 10:07 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Yes , i can't change the fn1. How to do it indirectly ? My fn1 takes obj pointer and member function pointer.
19th Jul 2024, 10:35 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Since you can't change fn1 and it requires a member function pointer, you can use a static member function or a free function to work around this. Use a static member function or a free function to store the integer and then call the actual member function. Store the additional data in a static context or a singleton-like class.
19th Jul 2024, 10:43 AM
✧GHOST✧
✧GHOST✧ - avatar
0
Use calldisplayWithArg to call display with arguments indirectly.
19th Jul 2024, 10:44 AM
✧GHOST✧
✧GHOST✧ - avatar
0
I am sorry but not getting your point. Doubt as below: static member function to store the variable: where should I store it ? If storage was not an issue, existing code is working with storage as member variable. To get rid off from stored member variable is the real pain area.
19th Jul 2024, 12:04 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Here If you want to store a lambda or a variable without using instance member variables and without storing it in a static member variable, you can use a static function to hold and manage the lambda or data. Static functions can operate independently of any class instances and access static variables
19th Jul 2024, 12:14 PM
✧GHOST✧
✧GHOST✧ - avatar
0
Many thanks ✧GHOST✧ but it also stores static variable. For me, I will opt for member variable like I have as it is more clean.
19th Jul 2024, 12:30 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
I don’t know
21st Jul 2024, 12:58 AM
Niall Sarath
Niall Sarath - avatar
0
How to convert lambda to member function pointer?
21st Jul 2024, 1:02 AM
Niall Sarath
Niall Sarath - avatar
0
I have a code that I could not complete the task. It is under the introductory part of python input. I have a screenshot, how do I upload it here?
21st Jul 2024, 1:03 AM
Niall Sarath
Niall Sarath - avatar