summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Wiederhake <BenWiederhake.GitHub@gmx.de>2021-09-19 22:10:51 +0200
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-09-21 04:22:52 +0430
commit743470c8f299419199c578efc80e405a8ec0e4d0 (patch)
tree6f1d12b49db29df210a2b1b27e94286622ee88a8
parentf261b68408a13ac6a216eb2ec070b1de1cc428c6 (diff)
downloadserenity-743470c8f299419199c578efc80e405a8ec0e4d0.zip
AK: Introduce ability to default-initialize a Variant
I noticed that Variant<Empty, …> is a somewhat common pattern while working on #10080, and this will simplify a few use-cases. :^)
-rw-r--r--AK/Variant.h6
-rw-r--r--Tests/AK/TestVariant.cpp7
2 files changed, 12 insertions, 1 deletions
diff --git a/AK/Variant.h b/AK/Variant.h
index f757e2a3ac..dae5cd7272 100644
--- a/AK/Variant.h
+++ b/AK/Variant.h
@@ -210,7 +210,11 @@ public:
template<typename... NewTs>
friend struct Variant;
- Variant() = delete;
+ Variant() requires(!can_contain<Empty>()) = delete;
+ Variant() requires(can_contain<Empty>())
+ : Variant(Empty())
+ {
+ }
#ifdef AK_HAS_CONDITIONALLY_TRIVIAL
Variant(const Variant&) requires(!(IsCopyConstructible<Ts> && ...)) = delete;
diff --git a/Tests/AK/TestVariant.cpp b/Tests/AK/TestVariant.cpp
index 8175b695da..f05c629022 100644
--- a/Tests/AK/TestVariant.cpp
+++ b/Tests/AK/TestVariant.cpp
@@ -219,3 +219,10 @@ TEST_CASE(copy_assign)
EXPECT_EQ(the_value.get<String>(), "Hello, world!");
}
}
+
+TEST_CASE(default_empty)
+{
+ Variant<Empty, int> my_variant;
+ EXPECT(my_variant.has<Empty>());
+ EXPECT(!my_variant.has<int>());
+}