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.

llvm-svn: 245518
This commit is contained in:
David Blaikie 2015-08-19 23:07:27 +00:00
parent b73f8e83f8
commit bdabca9432
2 changed files with 32 additions and 0 deletions

View File

@ -159,6 +159,25 @@ template <typename T> struct isPodLike<Optional<T> > {
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.

View File

@ -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