From 7d5316bd79cc720f8c35694cc6443563f304789c Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Wed, 25 Feb 2026 08:25:03 -0500 Subject: [PATCH] Allow get_rcp() to be called on a const reference --- include/rcp.h | 4 ++-- tests.cpp | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/include/rcp.h b/include/rcp.h index 9d33a6a..1d169bc 100644 --- a/include/rcp.h +++ b/include/rcp.h @@ -297,9 +297,9 @@ rcp rcp_dynamic_cast(rcp && other) #define rcp_managed(classname) \ public: \ - rcp get_rcp() \ + rcp get_rcp() const \ { \ - return rcp(this); \ + return rcp(const_cast(this)); \ } \ template \ static rcp create(Args&&... args) \ diff --git a/tests.cpp b/tests.cpp index a1d4df9..0f9d695 100644 --- a/tests.cpp +++ b/tests.cpp @@ -114,6 +114,16 @@ struct Receiver } }; +void test_get_rcp_const() +{ + auto a = MyObj::create(); + const MyObj & ref = *a; + rcp b = ref.get_rcp(); + assert(b); + assert(b->v == 42); + assert(b.use_count() == 2); +} + void test_multi_construct_from_raw_pointers() { Receiver r; @@ -423,6 +433,7 @@ int main(int argc, char * argv[]) test_dereference(); test_booleans(); test_create(); + test_get_rcp_const(); test_multi_construct_from_raw_pointers(); test_listener_self_registration(); test_copy_assignment_decrements_previous_reference();