diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-11-22 19:04:58 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-12-20 15:35:58 +0100 |
commit | 53b9ec80a3b7bb7e403a8c7330c0741484b9ba4d (patch) | |
tree | ca5e967c315e08071c432a1f7b589618ed3801e5 /asl | |
parent | defa7e1b9e16c6276e17a39b61d019c1a116472b (diff) |
Don't force niched values to be move assignable in reset, and fix warnings
Diffstat (limited to 'asl')
-rw-r--r-- | asl/option.hpp | 17 | ||||
-rw-r--r-- | asl/tests/option_tests.cpp | 3 |
2 files changed, 17 insertions, 3 deletions
diff --git a/asl/option.hpp b/asl/option.hpp index 6b528b9..f2af9ee 100644 --- a/asl/option.hpp +++ b/asl/option.hpp @@ -123,8 +123,8 @@ public: // NOLINTNEXTLINE(*-explicit-conversions)
constexpr option(nullopt_t) requires (!kHasNiche)
- : m_has_value{false}
- , m_payload{}
+ : m_payload{}
+ , m_has_value{false}
{}
// NOLINTNEXTLINE(*-explicit-conversions)
@@ -383,7 +383,18 @@ public: if constexpr (kHasNiche)
{
- m_payload = T(niche{});
+ if constexpr (move_assignable<T>)
+ {
+ m_payload = T(niche{});
+ }
+ else
+ {
+ if constexpr (!trivially_destructible<T>)
+ {
+ (&m_payload)->~T();
+ }
+ new (&m_payload) T(niche{});
+ }
}
else
{
diff --git a/asl/tests/option_tests.cpp b/asl/tests/option_tests.cpp index 8b98c99..15367f1 100644 --- a/asl/tests/option_tests.cpp +++ b/asl/tests/option_tests.cpp @@ -308,4 +308,7 @@ ASL_TEST(niche) opt = opt2;
ASL_TEST_EXPECT(opt2.has_value());
ASL_TEST_EXPECT(opt2.value().value == 2);
+
+ opt.reset();
+ ASL_TEST_EXPECT(!opt.has_value());
}
|