openframeworks / openframeworks/openFrameworks

Issues with lambdas in new ofEvent style

Open
#5,031 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
10.4k
Forks
2.6k
Avg merge
1d 21h
Merged PRs (30d)
9

Description

I was playing with the new style of registering for events, following the info in this commit message
https://github.com/openframeworks/openFrameworks/commit/52434595cec8dbd7a6af2930e35c815d4ba77994

Using ofEvent::newListener() seems to be working fine for binding callbacks to member functions, both with ofEvent<void> and ofEvent<Object>, i.e. these work fine:

ofEvent<void> testEvent;
ofEvent<string> testStringEvent;

myEventListener = ofApp::testEvent.newListener(this, &ofApp::onTestEvent);
myStringEventListener = ofApp::testEvent.newListener(this, &ofApp::onTestStringEvent);

However, trying to pass in a lambda results in a few compile errors. For example, these results in:

myEventListener = ofApp::testEvent.newListener(this, [](){});
myStringEventListener = ofApp::testStringEvent.newListener(this, [](string& s){});
events/ofEvent.h:614:38: No matching member function for call to 'make_function'
events/ofEvent.h:539:14: Candidate template ignored: could not match 'void (TObj::*)(const void *)' against '(lambda at apps/myApps/EventsTest/src/ofApp.cpp:10:58)'
events/ofEvent.h:597:14: Candidate function not viable: requires 2 arguments, but 3 were provided

If I try the moseMoved example from the commit message, I see..

ofEvents().mouseMoved.newListener([](ofMouseEventArgs & mouse){
    cout << mouse << endl;
});
events/ofEvent.h:441:38: Call to member function 'make_function' is ambiguous
events/ofEvent.h:363:14: Candidate function not viable: no known conversion from '(lambda at apps/myApps/EventsTest/src/ofApp.cpp:14:39)' to 'bool (*)(ofMouseEventArgs &)' for 1st argument
events/ofEvent.h:338:14: Candidate function template not viable: requires 3 arguments, but 2 were provided

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the newListener and make_function overloads in events/ofEvent.h, especially the locations cited in the compiler errors, and compare them with commit 52434595cec8dbd7a6af2930e35c815d4ba77994. Verify the bound and unbound lambda examples for void and string or mouse events; done means these examples compile without ambiguous or unmatched make_function errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.