[PATCH] Add a forwarding variadic constructor to llvm::Optional<T>.

Jordan Rose jordan_rose at apple.com
Fri Sep 26 15:21:05 PDT 2014


Hi dblaikie,

This makes it easier to return values convertible to T when the return type is Optional<T>. This is //not// a feature in the C++ Library Fundamentals TS for std::experimental::optional, but we've found it useful internally.

This is more controversial than the last three diffs I've posted, because it does make things potentially more ambiguous. In particular, David remarked:

> A direct variadic ctor for Optional<T> that builds a T seems a bit subtle/surprising/easily ambiguous - a utility make function is probably more appropriate (make_optional to go with make_unique, etc?).

We've been using this internally already, and the most motivating case was trying to return a type constructed from multiple arguments, i.e.

```
Optional<std::pair<int, int>> test() {
  return { 1, 2 };
}
```

There's also an annoyance with implicit conversions needing to be made explicit:

```
Optional<StringRef> x = ""; // rejected

Optional<StringRef> test() {
  return ""; // rejected
}
```

But I do know that Implicit Conversions Are Dangerous, and I'd be willing to withdraw this (and go change our internal code), particularly if someone comes up with an example of this going awry.

(I've marked David as a reviewer because he took an interest in my original post, but I'd welcome comments from anyone here.)

http://reviews.llvm.org/D5511

Files:
  include/llvm/ADT/Optional.h
  unittests/ADT/OptionalTest.cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D5511.14133.patch
Type: text/x-patch
Size: 7251 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140926/33093f9a/attachment.bin>


More information about the llvm-commits mailing list