<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - Cannot assign a lambda expression to an optional<function<...>>"
   href="https://llvm.org/bugs/show_bug.cgi?id=31071">31071</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Cannot assign a lambda expression to an optional<function<...>>
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>matt.rajca@me.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=17615" name="attach_17615" title="Sample project">attachment 17615</a> <a href="attachment.cgi?id=17615&action=edit" title="Sample project">[details]</a></span>
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.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>