}
Value* get_pointer() && = delete;
- bool hasValue() const {
+ bool hasValue() const noexcept {
return storage_.hasValue();
}
- explicit operator bool() const {
+ explicit operator bool() const noexcept {
return hasValue();
}
std::is_trivially_destructible<Value>::value,
StorageTriviallyDestructible,
StorageNonTriviallyDestructible>::type {
- bool hasValue() const {
+ bool hasValue() const noexcept {
return this->hasValue_;
}
template <class V>
bool operator>(const V& other, const Optional<V>&) = delete;
+// Comparisons with none
+template <class V>
+bool operator==(const Optional<V>& a, None) noexcept {
+ return !a.hasValue();
+}
+template <class V>
+bool operator==(None, const Optional<V>& a) noexcept {
+ return !a.hasValue();
+}
+template <class V>
+bool operator<(const Optional<V>&, None) noexcept {
+ return false;
+}
+template <class V>
+bool operator<(None, const Optional<V>& a) noexcept {
+ return a.hasValue();
+}
+template <class V>
+bool operator>(const Optional<V>& a, None) noexcept {
+ return a.hasValue();
+}
+template <class V>
+bool operator>(None, const Optional<V>&) noexcept {
+ return false;
+}
+template <class V>
+bool operator<=(None, const Optional<V>&) noexcept {
+ return true;
+}
+template <class V>
+bool operator<=(const Optional<V>& a, None) noexcept {
+ return !a.hasValue();
+}
+template <class V>
+bool operator>=(const Optional<V>&, None) noexcept {
+ return true;
+}
+template <class V>
+bool operator>=(None, const Optional<V>& a) noexcept {
+ return !a.hasValue();
+}
+
///////////////////////////////////////////////////////////////////////////////
} // namespace folly
EXPECT_TRUE(opt8(4) >= opt64());
}
+TEST(Optional, NoneComparisons) {
+ using opt = Optional<int>;
+ EXPECT_TRUE(opt() == none);
+ EXPECT_TRUE(none == opt());
+ EXPECT_FALSE(opt(1) == none);
+ EXPECT_FALSE(none == opt(1));
+
+ EXPECT_FALSE(opt() != none);
+ EXPECT_FALSE(none != opt());
+ EXPECT_TRUE(opt(1) != none);
+ EXPECT_TRUE(none != opt(1));
+
+ EXPECT_FALSE(opt() < none);
+ EXPECT_FALSE(none < opt());
+ EXPECT_FALSE(opt(1) < none);
+ EXPECT_TRUE(none < opt(1));
+
+ EXPECT_FALSE(opt() > none);
+ EXPECT_FALSE(none > opt());
+ EXPECT_FALSE(none > opt(1));
+ EXPECT_TRUE(opt(1) > none);
+
+ EXPECT_TRUE(opt() <= none);
+ EXPECT_TRUE(none <= opt());
+ EXPECT_FALSE(opt(1) <= none);
+ EXPECT_TRUE(none <= opt(1));
+
+ EXPECT_TRUE(opt() >= none);
+ EXPECT_TRUE(none >= opt());
+ EXPECT_TRUE(opt(1) >= none);
+ EXPECT_FALSE(none >= opt(1));
+}
+
TEST(Optional, Conversions) {
Optional<bool> mbool;
Optional<short> mshort;