Open Quuxplusone opened 7 years ago
Attached OptionalFunctions.zip
(16964 bytes, application/zip): Sample project
The following code compiles w/o error with ToT:
using namespace std;
using Handler = function<void(void)>;
void doSomething(int flags, optional<Handler> callback = optional<Handler>()) {
if (callback)
(*callback)();
}
void func() {}
int main () { doSomething(0); doSomething(0, func); doSomething(0, [](){}); doSomething(0, [](){cout << "Hello World\n"; }); }
Note that with Apple's shipping Xcode, the last three calls to doSomething
fail to compile.
Apparently we've got some divergence.
This fails with std::experimental::optional, but works with std::optional.
Re-title this bug. The reproducer fails to compile because we only implement LFTS v1, and the constructor needed to accept the example was added in LFTS v2. See https://rawgit.com/cplusplus/fundamentals-ts/v2/main.html#optional.object.ctor
I looked into using
However there are a couple of problems:
1.experimental::optional uses it's own in_place_t, so it's incompatible with std::in_place_t.
std::optional requires C++14 constexpr, so we can't possibly implement it in C++11 fully.
I tried pretty hard to make this work, and it almost does, but in the end it's going to be too much trouble.
Unfortunately <experimental/optional> isn't going to get much love now that
_Bug 32133 has been marked as a duplicate of this bug._
OptionalFunctions.zip
(16964 bytes, application/zip)Created attachment 17615 Sample project
Suppose we a function that takes a completion handler in the form of an optional:
Since a default value of an empty optional is provided, I can invoke this without passing a value for the final parameter just fine:
Moreover, I can invoke
doSomething
if I explicitly cast a lambda expression to aHandler
and pass it for the final parameter:However, passing a lambda expression directly does not work (the following does not compile with "No matching function for call to 'doSomething'"):
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 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.
A sample project that demonstrates this is attached. I am testing with Xcode 8.1 GM.