[llvm-bugs] [Bug 31071] New: Cannot assign a lambda expression to an optional<function<...>>

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Nov 19 08:59:56 PST 2016


https://llvm.org/bugs/show_bug.cgi?id=31071

            Bug ID: 31071
           Summary: Cannot assign a lambda expression to an
                    optional<function<...>>
           Product: clang
           Version: unspecified
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: matt.rajca at me.com
                CC: dgregor at apple.com, llvm-bugs at lists.llvm.org
    Classification: Unclassified

Created attachment 17615
  --> https://llvm.org/bugs/attachment.cgi?id=17615&action=edit
Sample project

Suppose we a function that takes a completion handler in the form of an
optional<function>:

    using Handler = function<void(void)>;

    void doSomething(int flags, optional<Handler> callback =
optional<Handler>()) {
        if (callback) {
            (*callback)();
        }
    }

Since a default value of an empty optional is provided, I can invoke this
without passing a value for the final parameter just fine:

    doSomething(0);

Moreover, I can invoke `doSomething` if I explicitly cast a lambda expression
to a `Handler` and pass it for the final parameter:

    doSomething(0, Handler([]{
        cout << "hello world" << endl;
    }));

However, passing a lambda expression directly does not work (the following does
not compile with "No matching function for call to 'doSomething'"):

    doSomething(0, []{
       cout << "hello world" << endl;
    });

I assume this is because the type checker doesn't know we can assign a lambda
expression to an optional (even if it's an optional<function> and lambda
expression can be assigned to functions).

In other words, if a lambda expression L can be assigned to a function F, we
should be able to assign L to an optional of type optional<F>.

A sample project that demonstrates this is attached. I am testing with Xcode
8.1 GM.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20161119/91efbf21/attachment.html>


More information about the llvm-bugs mailing list