s00500 / ESPUI

A simple web user interface library for ESP32 and ESP8266

Home Page:https://valencia.lbsfilm.at/midterm-presentation/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to use class lambda - no matching function to call

vbartusevicius opened this issue · comments

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

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);

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.

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

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!!!