Modified ref-qualifiers return type for Optional::value() and Optional::operator*
authorAnand Mazumdar <mazumdar.anand@gmail.com>
Tue, 30 Aug 2016 06:21:40 +0000 (23:21 -0700)
committerFacebook Github Bot 2 <facebook-github-bot-2-bot@fb.com>
Tue, 30 Aug 2016 06:23:27 +0000 (23:23 -0700)
commit8deb28b372c8a435aa2a9f0d7977332f500f9fee
tree5d3f19ccf5da6d0c6398779ff90230c746bcc115
parentbae33d680bbba07b74d9bf9b2ba99766ebb3795e
Modified ref-qualifiers return type for Optional::value() and Optional::operator*

Summary:
Optional::value() returns a temporary object when the object is an rvalue. This is different in semantics then what boost::optional/std::optional do.

The decision to make the copy or not should be up to the user and not the library. Consider an example:

```
void F(Optional<T> &&opt) {
  T&& t = std::move(opt).get();
  // I know `opt` is alive in this scope, I should be able to keep a rvalue ref to the internals
}
// if we were to return a `T`, that would actually return a new temporary.
```

```
void G(T&& t);
G(std::move(opt).get()); // This could have surprising behavior too !
```

This change modified the return type to be `T&&` and also introduces an extra overload for `const T&&`. Also, deleted two test-cases that assume the lifetime to be extended. This is a breaking change but this brings folly::Optional on parity with other siblings.
Closes https://github.com/facebook/folly/pull/353

Reviewed By: ddrcoder

Differential Revision: D3714962

Pulled By: yfeldblum

fbshipit-source-id: 1794d51590062db4ad02fc8688cb28a06712c076
folly/Optional.h
folly/test/OptionalTest.cpp