Skip to content

Commit

Permalink
AK: Introduce ability to default-initialize a Variant
Browse files Browse the repository at this point in the history
I noticed that Variant<Empty, …> is a somewhat common pattern while
working on SerenityOS#10080, and this will simplify a few use-cases. :^)
  • Loading branch information
BenWiederhake authored and alimpfard committed Sep 20, 2021
1 parent f261b68 commit 743470c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion AK/Variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ struct Variant
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;
Expand Down
7 changes: 7 additions & 0 deletions Tests/AK/TestVariant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>());
}

0 comments on commit 743470c

Please sign in to comment.