[llvm] r245518 - Allow Optionals to be compared to None
David Blaikie via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 19 16:07:27 PDT 2015
Author: dblaikie
Date: Wed Aug 19 18:07:27 2015
New Revision: 245518
URL: http://llvm.org/viewvc/llvm-project?rev=245518&view=rev
Log:
Allow Optionals to be compared to None
This is something like nullopt in std::experimental::optional. Optional
could already be constructed from None, so this seems like an obvious
extension from there.
I have a use in a future patch for Clang, though it may not go that
way/end up used - so this seemed worth committing now regardless.
Modified:
llvm/trunk/include/llvm/ADT/Optional.h
llvm/trunk/unittests/ADT/OptionalTest.cpp
Modified: llvm/trunk/include/llvm/ADT/Optional.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Optional.h?rev=245518&r1=245517&r2=245518&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/Optional.h (original)
+++ llvm/trunk/include/llvm/ADT/Optional.h Wed Aug 19 18:07:27 2015
@@ -159,6 +159,25 @@ template <typename T> struct isPodLike<O
template<typename T, typename U>
void operator==(const Optional<T> &X, const Optional<U> &Y);
+template<typename T>
+bool operator==(const Optional<T> &X, NoneType) {
+ return !X.hasValue();
+}
+
+template<typename T>
+bool operator==(NoneType, const Optional<T> &X) {
+ return X == None;
+}
+
+template<typename T>
+bool operator!=(const Optional<T> &X, NoneType) {
+ return !(X == None);
+}
+
+template<typename T>
+bool operator!=(NoneType, const Optional<T> &X) {
+ return X != None;
+}
/// \brief Poison comparison between two \c Optional objects. Clients needs to
/// explicitly compare the underlying values and account for empty \c Optional
/// objects.
Modified: llvm/trunk/unittests/ADT/OptionalTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/OptionalTest.cpp?rev=245518&r1=245517&r2=245518&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/OptionalTest.cpp (original)
+++ llvm/trunk/unittests/ADT/OptionalTest.cpp Wed Aug 19 18:07:27 2015
@@ -377,5 +377,18 @@ TEST_F(OptionalTest, MoveGetValueOr) {
#endif // LLVM_HAS_RVALUE_REFERENCE_THIS
+TEST_F(OptionalTest, NoneComparison) {
+ Optional<int> o;
+ EXPECT_EQ(o, None);
+ EXPECT_EQ(None, o);
+ EXPECT_FALSE(o != None);
+ EXPECT_FALSE(None != o);
+ o = 3;
+ EXPECT_FALSE(o == None);
+ EXPECT_FALSE(None == o);
+ EXPECT_TRUE(o != None);
+ EXPECT_TRUE(None != o);
+}
+
} // end anonymous namespace
More information about the llvm-commits
mailing list