diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-11-22 17:13:03 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-12-20 15:35:58 +0100 |
commit | 58bb86fd2f0ecd740fc5bc8078de44221c12c70a (patch) | |
tree | 79e83f044d643211d3f9ebfd933e9dea92b2d04e /asl | |
parent | f5ef1937eafb3d96b3683d92639a193694210c70 (diff) |
Add a niche to box
Diffstat (limited to 'asl')
-rw-r--r-- | asl/annotations.hpp | 1 | ||||
-rw-r--r-- | asl/box.hpp | 16 | ||||
-rw-r--r-- | asl/meta.hpp | 9 | ||||
-rw-r--r-- | asl/tests/box_tests.cpp | 22 |
4 files changed, 27 insertions, 21 deletions
diff --git a/asl/annotations.hpp b/asl/annotations.hpp index a398e13..fc65378 100644 --- a/asl/annotations.hpp +++ b/asl/annotations.hpp @@ -2,7 +2,6 @@ #include "asl/config.hpp"
-
#if ASL_COMPILER_CLANG_CL
#define ASL_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
#elif ASL_COMPILER_CLANG
diff --git a/asl/box.hpp b/asl/box.hpp index d8ccd36..7556d76 100644 --- a/asl/box.hpp +++ b/asl/box.hpp @@ -16,15 +16,18 @@ class box ASL_NO_UNIQUE_ADDRESS Allocator m_alloc;
public:
- explicit constexpr box(T* ptr = nullptr)
+ explicit constexpr box(niche)
requires default_constructible<Allocator>
- : m_ptr{ptr}
+ : m_ptr{nullptr}
+ , m_alloc{}
{}
-
+
constexpr box(T* ptr, Allocator alloc)
: m_ptr{ptr}
, m_alloc{ASL_MOVE(alloc)}
- {}
+ {
+ ASL_ASSERT(m_ptr != nullptr);
+ }
constexpr box(box&& other)
: m_ptr{exchange(other.m_ptr, nullptr)}
@@ -77,6 +80,11 @@ public: ASL_ASSERT(m_ptr != nullptr);
return m_ptr;
}
+
+ constexpr bool operator==(niche) const
+ {
+ return m_ptr == nullptr;
+ }
};
template<is_object T, allocator Allocator = DefaultAllocator, typename... Args>
diff --git a/asl/meta.hpp b/asl/meta.hpp index f0397a1..72b36fd 100644 --- a/asl/meta.hpp +++ b/asl/meta.hpp @@ -160,4 +160,13 @@ template<typename T, int N> struct _is_array_helper<T[N]> : true_type {}; template<typename T> concept is_array = _is_array_helper<T>::value;
+struct niche {};
+
+template<typename T>
+concept has_niche = constructible_from<T, niche> &&
+ requires (const T& value, niche n)
+ {
+ { value == n } -> same_as<bool>;
+ };
+
} // namespace asl
diff --git a/asl/tests/box_tests.cpp b/asl/tests/box_tests.cpp index f1a35f3..3813639 100644 --- a/asl/tests/box_tests.cpp +++ b/asl/tests/box_tests.cpp @@ -4,30 +4,20 @@ #include "asl/tests/test_types.hpp"
static_assert(sizeof(asl::box<int>) == sizeof(int*));
-static_assert(asl::default_constructible<asl::box<int>>);
static_assert(!asl::copyable<asl::box<int>>);
static_assert(asl::moveable<asl::box<int>>);
-static_assert(asl::default_constructible<asl::box<NonMoveConstructible>>);
+static_assert(asl::has_niche<asl::box<int>>);
ASL_TEST(destructor)
{
bool d = false;
{
- asl::box<DestructorObserver> box2;
-
- {
- auto box = asl::make_box<DestructorObserver>(&d);
- ASL_TEST_ASSERT(!d);
-
-
- auto box3 = ASL_MOVE(box);
- ASL_TEST_ASSERT(!d);
-
- box2 = ASL_MOVE(box3);
- ASL_TEST_ASSERT(!d);
- }
-
+ auto box = asl::make_box<DestructorObserver>(&d);
+ ASL_TEST_ASSERT(!d);
+
+
+ auto box3 = ASL_MOVE(box);
ASL_TEST_ASSERT(!d);
}
|