* cout << *v << endl;
* }
*/
-#include <utility>
#include <cassert>
#include <cstddef>
#include <type_traits>
+#include <utility>
#include <boost/operators.hpp>
return Opt(std::forward<T>(v));
}
+///////////////////////////////////////////////////////////////////////////////
+// Comparisons.
+
template<class V>
-bool operator< (const Optional<V>& a, const Optional<V>& b) {
- if (a.hasValue() != b.hasValue()) { return a.hasValue() < b.hasValue(); }
- if (a.hasValue()) { return a.value() < b.value(); }
- return false;
+bool operator==(const Optional<V>& a, const V& b) {
+ return a.hasValue() && a.value() == b;
+}
+
+template<class V>
+bool operator!=(const Optional<V>& a, const V& b) {
+ return !(a == b);
+}
+
+template<class V>
+bool operator==(const V& a, const Optional<V&> b) {
+ return b.hasValue() && b.value() == a;
+}
+
+template<class V>
+bool operator!=(const V& a, const Optional<V>& b) {
+ return !(a == b);
}
template<class V>
return true;
}
-template<class V>
-bool operator<=(const Optional<V>& a, const Optional<V>& b) {
- return !(b < a);
-}
-
template<class V>
bool operator!=(const Optional<V>& a, const Optional<V>& b) {
- return !(b == a);
+ return !(a == b);
}
template<class V>
-bool operator>=(const Optional<V>& a, const Optional<V>& b) {
- return !(a < b);
+bool operator< (const Optional<V>& a, const Optional<V>& b) {
+ if (a.hasValue() != b.hasValue()) { return a.hasValue() < b.hasValue(); }
+ if (a.hasValue()) { return a.value() < b.value(); }
+ return false;
}
template<class V>
return b < a;
}
+template<class V>
+bool operator<=(const Optional<V>& a, const Optional<V>& b) {
+ return !(b < a);
+}
+
+template<class V>
+bool operator>=(const Optional<V>& a, const Optional<V>& b) {
+ return !(a < b);
+}
+
// To supress comparability of Optional<T> with T, despite implicit conversion.
template<class V> bool operator< (const Optional<V>&, const V& other) = delete;
template<class V> bool operator<=(const Optional<V>&, const V& other) = delete;
-template<class V> bool operator==(const Optional<V>&, const V& other) = delete;
-template<class V> bool operator!=(const Optional<V>&, const V& other) = delete;
template<class V> bool operator>=(const Optional<V>&, const V& other) = delete;
template<class V> bool operator> (const Optional<V>&, const V& other) = delete;
template<class V> bool operator< (const V& other, const Optional<V>&) = delete;
template<class V> bool operator<=(const V& other, const Optional<V>&) = delete;
-template<class V> bool operator==(const V& other, const Optional<V>&) = delete;
-template<class V> bool operator!=(const V& other, const Optional<V>&) = delete;
template<class V> bool operator>=(const V& other, const Optional<V>&) = delete;
template<class V> bool operator> (const V& other, const Optional<V>&) = delete;
+///////////////////////////////////////////////////////////////////////////////
+
} // namespace folly
-#endif//FOLLY_OPTIONAL_H_
+#endif // FOLLY_OPTIONAL_H_