0

I'm working in C++ 14 with Wt, and I'm trying to create a generic "UNDO" widget to go alongside my form fields.

Being new to Lambda's I'm getting stuck - the compiler is complaining about no matching function call for the UndoIcon creation below:

auto editField = templatePtr->bindWidget("lineedit", Wt::cpp14::make_unique<Wt::WLineEdit>());
editField->setText(Wt::toWString(m_initialValue));
const QString initialValue = m_initialValue; // Copy from member variable to local for lambda
auto undoIcon = Wt::cpp14::make_unique<UndoIcon>(1,[editField,initialValue] { editField->setText(Wt::toWString(initialValue)); });

and I define my UndoIcon class as follows:

UndoIcon::UndoIcon(unsigned char iconSize, void (*callback)()) {
this->clicked().connect([=] { (*callback)(); });
}

Is it possible to pass a Lambda like this? The various (similar) answers on SO all use templates which I don't think I need.

TSG
  • 4,242
  • 9
  • 61
  • 121
  • 3
    Only stateless lambdas are convertible to function pointers. You can use `std::function` that can store all callables. BTW `[editField,initialValue]` copies the values into lambda, `[&editField,&initialValue]` captures by reference. – Quimby Jul 10 '19 at 15:04
  • "The various (similar) answers on SO all use templates which I don't think I need" You think wrong. Look at the definition of [`clicked().connect`](https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1EventSignal.html#a7050f8ff1075fb01fc0ea6a49a28b703) – Caleth Jul 10 '19 at 15:05
  • @NathanOliver they say that `UndoIcon` class is theirs, so they can change it. Though at that point, why not just accept any callable via templates? That's likely what `connect` already does. – Dan M. Jul 10 '19 at 15:06
  • @NathanOliver I was referring to `UndoIcon::callback` argument, but `::connect` is probably the same. – Quimby Jul 10 '19 at 15:06
  • Yep. I though it was a library class for some reason, comment retracted. – NathanOliver Jul 10 '19 at 15:07

0 Answers0