summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--compat/borrowed-ptr.hpp10
-rw-r--r--compat/borrowed-ptr.inl17
-rw-r--r--test/bptr.cpp19
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<typename Y> bptr(const bptr<Y>& other, std::nullptr_t) noexcept;
template<typename Y> bptr& _copy_assign(const bptr<Y>& other) noexcept;
template<typename Y> bptr(bptr<Y>&& other, std::nullptr_t) noexcept;
@@ -52,13 +50,13 @@ public:
bptr(const bptr&) noexcept;
bptr& operator=(const bptr&) noexcept;
- template<detail_borrowed_ptr::DerivedFrom<std::remove_const_t<T>> Y> bptr(const bptr<Y>&) noexcept;
- template<detail_borrowed_ptr::DerivedFrom<std::remove_const_t<T>> Y> bptr& operator=(const bptr<Y>&) noexcept;
+ template<detail_borrowed_ptr::DerivedFrom<T> Y> bptr(const bptr<Y>&) noexcept;
+ template<detail_borrowed_ptr::DerivedFrom<T> Y> bptr& operator=(const bptr<Y>&) noexcept;
bptr(bptr&&) noexcept;
bptr& operator=(bptr&&) noexcept;
- template<detail_borrowed_ptr::DerivedFrom<std::remove_const_t<T>> Y> bptr(bptr<Y>&&) noexcept;
- template<detail_borrowed_ptr::DerivedFrom<std::remove_const_t<T>> Y> bptr& operator=(bptr<Y>&&) noexcept;
+ template<detail_borrowed_ptr::DerivedFrom<T> Y> bptr(bptr<Y>&&) noexcept;
+ template<detail_borrowed_ptr::DerivedFrom<T> Y> bptr& operator=(bptr<Y>&&) noexcept;
operator bptr<const T>() const noexcept requires (!std::is_const_v<T>);
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<typename T>
template<typename... Ts>
requires std::is_constructible_v<std::remove_const_t<T>, Ts&&...>
bptr<T>::bptr(InPlaceInitT, Ts&&... args) noexcept:
-bptr{ new T{ forward<Ts...>(args...) } }
+bptr{ new std::remove_const_t<T>{ forward<Ts...>(args)... } }
{
}
template<typename T> bptr<T>::bptr(std::nullptr_t) noexcept: blk{nullptr} {}
template<typename T> bptr<T>::bptr() noexcept: bptr{nullptr} {}
-template<typename T> bptr<T>::bptr(T* ptr) noexcept: blk{ptr ? new detail_borrowed_ptr::control_block{ptr, 1} : nullptr} {}
+template<typename T> bptr<T>::bptr(T* ptr) noexcept:
+ blk{ptr ? new detail_borrowed_ptr::control_block{const_cast<std::remove_const_t<T>*>(ptr), 1} : nullptr}
+{}
template<typename T>
bptr<T>::~bptr() noexcept
@@ -63,11 +65,11 @@ template<typename T> bptr<T>::bptr(const bptr& other) noexcept: bptr{other, null
template<typename T> bptr<T>& bptr<T>::operator=(const bptr& other) noexcept { return _copy_assign(other); }
template<typename T>
-template<detail_borrowed_ptr::DerivedFrom<std::remove_const_t<T>> Y>
+template<detail_borrowed_ptr::DerivedFrom<T> Y>
bptr<T>::bptr(const bptr<Y>& other) noexcept: bptr{other, nullptr} {}
template<typename T>
-template<detail_borrowed_ptr::DerivedFrom<std::remove_const_t<T>> Y>
+template<detail_borrowed_ptr::DerivedFrom<T> Y>
bptr<T>& bptr<T>::operator=(const bptr<Y>& other) noexcept
{ return _copy_assign(other); }
@@ -75,11 +77,11 @@ template<typename T> bptr<T>& bptr<T>::operator=(bptr&& other) noexcept { return
template<typename T> bptr<T>::bptr(bptr&& other) noexcept: bptr{move(other), nullptr} {}
template<typename T>
-template<detail_borrowed_ptr::DerivedFrom<std::remove_const_t<T>> Y>
+template<detail_borrowed_ptr::DerivedFrom<T> Y>
bptr<T>::bptr(bptr<Y>&& other) noexcept: bptr{move(other), nullptr} {}
template<typename T>
-template<detail_borrowed_ptr::DerivedFrom<std::remove_const_t<T>> Y>
+template<detail_borrowed_ptr::DerivedFrom<T> Y>
bptr<T>& bptr<T>::operator=(bptr<Y>&& other) noexcept
{ return _move_assign(move(other)); }
@@ -115,9 +117,6 @@ void bptr<T>::destroy() noexcept
template<typename T> bptr<T>& bptr<T>::operator=(std::nullptr_t) noexcept { reset(); return *this; }
template<typename T>
-bptr<T>::bptr(detail_borrowed_ptr::control_block* blk) noexcept: blk{blk} { }
-
-template<typename T>
template<typename Y>
bptr<T>::bptr(const bptr<Y>& other, std::nullptr_t) noexcept:
blk{other.blk}
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<Foo>{} == bptr<Foo>{});
fm_assert(bptr<const Foo>{} == bptr<const Foo>{});
fm_assert(bptr<Foo>{} == bptr<const Foo>{});
+
+ auto p1 = bptr<const Foo>{InPlace, 1}; (void)p1;
+ //auto p2 = bptr<Foo>{p1};
+ auto p3 = bptr<const Foo>{p1}; (void)p3;
+ fm_assert(p1->x == 1); fm_assert(p3->x == 1);
+ fm_assert(p1 == p3);
+
+ auto p4 = bptr<Foo>{InPlace, 4}; (void)p4;
+ auto p5 = bptr<const Foo>{p4}; (void)p5;
+ fm_assert(p4->x == 4); fm_assert(p5->x == 4);
+ fm_assert(p4 == p5);
}
} // namespace