[llvm] 637a594 - [ADT] Add deduction guide for llvm::Optional
David Blaikie via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 26 14:46:33 PDT 2022
Author: David Blaikie
Date: 2022-10-26T21:45:57Z
New Revision: 637a594f5a17bbebb9bce72845749a6803561704
URL: https://github.com/llvm/llvm-project/commit/637a594f5a17bbebb9bce72845749a6803561704
DIFF: https://github.com/llvm/llvm-project/commit/637a594f5a17bbebb9bce72845749a6803561704.diff
LOG: [ADT] Add deduction guide for llvm::Optional
Added to address some uses of implicit CTAD added in clang recently.
Added:
Modified:
llvm/include/llvm/ADT/Optional.h
llvm/unittests/ADT/OptionalTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/Optional.h b/llvm/include/llvm/ADT/Optional.h
index 0f4e500834855..36ad6210a849e 100644
--- a/llvm/include/llvm/ADT/Optional.h
+++ b/llvm/include/llvm/ADT/Optional.h
@@ -388,6 +388,9 @@ template <typename T> class Optional {
}
};
+template<typename T>
+Optional(const T&) -> Optional<T>;
+
template <class T> llvm::hash_code hash_value(const Optional<T> &O) {
return O ? hash_combine(true, *O) : hash_value(false);
}
diff --git a/llvm/unittests/ADT/OptionalTest.cpp b/llvm/unittests/ADT/OptionalTest.cpp
index c583ff0df9af1..e615d56e2b625 100644
--- a/llvm/unittests/ADT/OptionalTest.cpp
+++ b/llvm/unittests/ADT/OptionalTest.cpp
@@ -854,4 +854,13 @@ TEST(OptionalTest, GCCIsTriviallyMoveConstructibleCompat) {
EXPECT_FALSE(V);
}
+TEST(OptionalTest, DeductionGuide) {
+ Optional V = MoveOnly(1);
+ EXPECT_TRUE(V);
+ EXPECT_EQ(V->val, 1);
+}
+
+static_assert(
+ std::is_same_v<Optional<MoveOnly>, decltype(Optional(MoveOnly(1)))>);
+
} // end anonymous namespace
More information about the llvm-commits
mailing list