From 6f45f8555be7bef45b7c50a95b898ebba7fb9a92 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sun, 14 Jul 2024 13:20:49 +0200 Subject: w --- compat/borrowed-ptr.hpp | 10 ++++------ compat/borrowed-ptr.inl | 17 ++++++++--------- test/bptr.cpp | 19 ++++++++++++++++++- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/compat/borrowed-ptr.hpp b/compat/borrowed-ptr.hpp index affad912..3982b547 100644 --- a/compat/borrowed-ptr.hpp +++ b/compat/borrowed-ptr.hpp @@ -31,8 +31,6 @@ class bptr final // NOLINT(*-special-member-functions) { detail_borrowed_ptr::control_block* blk; - bptr(detail_borrowed_ptr::control_block* blk) noexcept; - template bptr(const bptr& other, std::nullptr_t) noexcept; template bptr& _copy_assign(const bptr& other) noexcept; template bptr(bptr&& other, std::nullptr_t) noexcept; @@ -52,13 +50,13 @@ public: bptr(const bptr&) noexcept; bptr& operator=(const bptr&) noexcept; - template> Y> bptr(const bptr&) noexcept; - template> Y> bptr& operator=(const bptr&) noexcept; + template Y> bptr(const bptr&) noexcept; + template Y> bptr& operator=(const bptr&) noexcept; bptr(bptr&&) noexcept; bptr& operator=(bptr&&) noexcept; - template> Y> bptr(bptr&&) noexcept; - template> Y> bptr& operator=(bptr&&) noexcept; + template Y> bptr(bptr&&) noexcept; + template Y> bptr& operator=(bptr&&) noexcept; operator bptr() const noexcept requires (!std::is_const_v); diff --git a/compat/borrowed-ptr.inl b/compat/borrowed-ptr.inl index acdcb0df..c2a8b730 100644 --- a/compat/borrowed-ptr.inl +++ b/compat/borrowed-ptr.inl @@ -43,14 +43,16 @@ template template requires std::is_constructible_v, Ts&&...> bptr::bptr(InPlaceInitT, Ts&&... args) noexcept: -bptr{ new T{ forward(args...) } } +bptr{ new std::remove_const_t{ forward(args)... } } { } template bptr::bptr(std::nullptr_t) noexcept: blk{nullptr} {} template bptr::bptr() noexcept: bptr{nullptr} {} -template bptr::bptr(T* ptr) noexcept: blk{ptr ? new detail_borrowed_ptr::control_block{ptr, 1} : nullptr} {} +template bptr::bptr(T* ptr) noexcept: + blk{ptr ? new detail_borrowed_ptr::control_block{const_cast*>(ptr), 1} : nullptr} +{} template bptr::~bptr() noexcept @@ -63,11 +65,11 @@ template bptr::bptr(const bptr& other) noexcept: bptr{other, null template bptr& bptr::operator=(const bptr& other) noexcept { return _copy_assign(other); } template -template> Y> +template Y> bptr::bptr(const bptr& other) noexcept: bptr{other, nullptr} {} template -template> Y> +template Y> bptr& bptr::operator=(const bptr& other) noexcept { return _copy_assign(other); } @@ -75,11 +77,11 @@ template bptr& bptr::operator=(bptr&& other) noexcept { return template bptr::bptr(bptr&& other) noexcept: bptr{move(other), nullptr} {} template -template> Y> +template Y> bptr::bptr(bptr&& other) noexcept: bptr{move(other), nullptr} {} template -template> Y> +template Y> bptr& bptr::operator=(bptr&& other) noexcept { return _move_assign(move(other)); } @@ -114,9 +116,6 @@ void bptr::destroy() noexcept template bptr& bptr::operator=(std::nullptr_t) noexcept { reset(); return *this; } -template -bptr::bptr(detail_borrowed_ptr::control_block* blk) noexcept: blk{blk} { } - template template bptr::bptr(const bptr& other, std::nullptr_t) noexcept: diff --git a/test/bptr.cpp b/test/bptr.cpp index 3a62de53..12bd9442 100644 --- a/test/bptr.cpp +++ b/test/bptr.cpp @@ -8,7 +8,13 @@ namespace floormat { namespace { -struct Foo : bptr_base {}; +struct Foo : bptr_base +{ + int x; + + fm_DECLARE_DELETED_COPY_MOVE_ASSIGNMENTS(Foo); + Foo(int x) : x{x} {} +}; struct Bar : Foo {}; struct Baz : bptr_base {}; } // namespace @@ -334,6 +340,17 @@ void test10() fm_assert(bptr{} == bptr{}); fm_assert(bptr{} == bptr{}); fm_assert(bptr{} == bptr{}); + + auto p1 = bptr{InPlace, 1}; (void)p1; + //auto p2 = bptr{p1}; + auto p3 = bptr{p1}; (void)p3; + fm_assert(p1->x == 1); fm_assert(p3->x == 1); + fm_assert(p1 == p3); + + auto p4 = bptr{InPlace, 4}; (void)p4; + auto p5 = bptr{p4}; (void)p5; + fm_assert(p4->x == 4); fm_assert(p5->x == 4); + fm_assert(p4 == p5); } } // namespace -- cgit v1.2.3