From 5c54707ed13c2996f36b99dddb76f017765cec72 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 4 May 2024 20:13:15 +0200 Subject: a w --- compat/borrowed-ptr-fwd.hpp | 2 +- compat/borrowed-ptr.hpp | 13 ++++++++----- test/bptr.cpp | 31 +++++++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/compat/borrowed-ptr-fwd.hpp b/compat/borrowed-ptr-fwd.hpp index da91502d..465e9bf4 100644 --- a/compat/borrowed-ptr-fwd.hpp +++ b/compat/borrowed-ptr-fwd.hpp @@ -44,7 +44,7 @@ public: T& operator*() const noexcept; template friend class bptr; - template friend bptr static_pointer_cast(const bptr& p) noexcept; + template friend bptr static_pointer_cast(const bptr& p) noexcept; }; } // namespace floormat diff --git a/compat/borrowed-ptr.hpp b/compat/borrowed-ptr.hpp index 5caa2261..d13d8ee2 100644 --- a/compat/borrowed-ptr.hpp +++ b/compat/borrowed-ptr.hpp @@ -28,20 +28,23 @@ concept StaticCastable = requires(From* from) { namespace floormat { -template +template bptr static_pointer_cast(const bptr& p) noexcept { - static_assert(detail_borrowed_ptr::StaticCastable); - - if (p.blk) [[likely]] + // hack to generate better error message + if constexpr (detail_borrowed_ptr::StaticCastable) { - if (auto* ptr = p.blk->_ptr) + if (p.blk && p.blk->_ptr) [[likely]] { fm_bptr_assert(p.casted_ptr); auto* ret = static_cast(p.casted_ptr); return bptr{DirectInit, ret, p.blk}; } } + else + // concepts can't be forward-declared so use static_assert + static_assert(detail_borrowed_ptr::StaticCastable); + return bptr{nullptr}; } diff --git a/test/bptr.cpp b/test/bptr.cpp index b8e4bef1..99d6cd2c 100644 --- a/test/bptr.cpp +++ b/test/bptr.cpp @@ -4,10 +4,38 @@ namespace floormat { -namespace { struct Foo {}; struct Bar : Foo {}; struct Baz {}; } +namespace { +struct Foo {}; +struct Bar : Foo {}; +struct Baz {}; +} // namespace + +// NOLINTBEGIN(*-use-anonymous-namespace) + template struct detail_borrowed_ptr::control_block_impl; +template struct detail_borrowed_ptr::control_block_impl; +template struct detail_borrowed_ptr::control_block_impl; + +template bool operator==(const bptr&, const bptr&) noexcept; +template bool operator==(const bptr&, const bptr&) noexcept; +template bool operator==(const bptr&, const bptr&) noexcept; + template class bptr; +template class bptr; +template class bptr; + +template bptr static_pointer_cast(const bptr&) noexcept; +template bptr static_pointer_cast(const bptr&) noexcept; +template bptr static_pointer_cast(const bptr&) noexcept; + template bptr static_pointer_cast(const bptr&) noexcept; +template bptr static_pointer_cast(const bptr&) noexcept; + +//template bptr static_pointer_cast(const bptr&) noexcept; // must fail +//template bptr static_pointer_cast(const bptr&) noexcept; // must fail +//template bptr static_pointer_cast(const bptr&) noexcept; // must fail + +// NOLINTEND(*-use-anonymous-namespace) namespace { @@ -22,5 +50,4 @@ void test_app::test_bptr() test1(); } - } // namespace floormat -- cgit v1.2.3