openframeworks / openframeworks/openFrameworks

lambda events with empty capture fail to compile

Open
#4,783 1 comment 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

When I try to attach a lambda to an event listener, the compiler throws up a lot of template error messages, the gist being that it refuses to compile because of ambiguity.

Here's a minimal test case, and my compiler output.

https://gist.github.com/tgfrerer/12e7ef5e2ae314c2dd81

I believe it has to do with ofEvent.h#L190, which states:

#if _MSC_VER
        template<typename> struct check_function;
        template<typename R, typename... Args>
        struct check_function<R(Args...)> : public std::function<R(Args...)> {
            template<typename T,
            class = typename std::enable_if<
                std::is_same<R, void>::value
                || std::is_convertible<
                decltype(std::declval<T>()(std::declval<Args>()...)),
                R>::value>::type>
                check_function(T &&t) : std::function<R(Args...)>(std::forward<T>(t)) { }
        };
#endif

I suppose this is to disambiguate the way the function template is constructed. But I can't really tell what's going on in there...

This might be a VS2015 related issue - I haven't had an opportunity to test on other platforms.

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

Start with the minimal reproduction in the linked gist and inspect ofEvent.h around line 190, especially the MSVC check_function specialization. Reproduce the empty-capture lambda failure with VS2015 if available, then verify that attaching the lambda to an event listener compiles without the reported ambiguity.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.