Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use class lambda - no matching function to call #289

Open
vbartusevicius opened this issue Jan 20, 2024 · 4 comments
Open

Unable to use class lambda - no matching function to call #289

vbartusevicius opened this issue Jan 20, 2024 · 4 comments

Comments

@vbartusevicius
Copy link

Describe the bug
Trying to use a lambda callback as in the README:

    ESPUI.addControl(
        ControlType::Number, 
        "Distance to sensor when \"empty\" (cm)", 
        "100", 
        color, 
        Control::noParent,
        [&](Control *sender, int eventname) {
            this->handleCallback(sender, eventname);
        }
    );

Getting an error:

Admin.cpp:31:5: error: no matching function for call to 'ESPUIClass::addControl(ControlType, const char [37], const char [4], ControlColor&, const uint16_t&, WebAdmin::begin()::<lambda(Control*, int)>)'

Using PlatformIO and s00500/ESPUI@^2.2.3

@MartinMueller2003
Copy link
Collaborator

I am using Lambda all the time. The issue is that you cannot capture "this" easily. I pass the this pointer in as a user argument. Here is the syntax I am using:

ControlId = ESPUI.addControl (
    uiControltype,
    Title.c_str (),
    GetDataValueStr (),
    color,
    TabId,
    [] (Control * sender, int type, void * UserInfo)
    {
        if (UserInfo)
        {
            static_cast <cControlCommon *> (UserInfo)->Callback (sender, type);
        }
    },
    this);

@vbartusevicius
Copy link
Author

Yes, that works. I just want to ask if there is an error in the documentation or the code. Because the docs say:
image
I think the type-definition could be adjusted to allow capturing this.

Personally, I use the UserInfo for different purposes. Wrapping additional structure to incorporate this seems not the best idea.

@MartinMueller2003
Copy link
Collaborator

I happen to use this. You can point at any structure you want and interpret it any way you want. Ultimate flexibility.

@thespielplatz
Copy link

I am using Lambda all the time. The issue is that you cannot capture "this" easily. I pass the this pointer in as a user argument. Here is the syntax I am using:

ControlId = ESPUI.addControl (
    uiControltype,
    Title.c_str (),
    GetDataValueStr (),
    color,
    TabId,
    [] (Control * sender, int type, void * UserInfo)
    {
        if (UserInfo)
        {
            static_cast <cControlCommon *> (UserInfo)->Callback (sender, type);
        }
    },
    this);

Thanks a lot! I was missing this!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants