From 2b6958788276f99a2217d967de947f2e8dff78af Mon Sep 17 00:00:00 2001 From: mberk-yilmaz Date: Wed, 18 Mar 2026 12:48:20 +0300 Subject: [PATCH 1/2] addon: add const overloads for multipart_t front/back Add const-qualified overloads for multipart_t::front() and back() in `zmq_addon.hpp`. Note on API shape: - In a container-style API, non-const overloads would typically return `message_t&`. - This change intentionally preserves existing non-const return types (`const message_t&`) for backward-compatibility and to avoid broadening mutability semantics in this PR. Also extend `tests/multipart.cpp` with compile-time and runtime checks for const/non-const access paths. No behavioral change intended. --- tests/multipart.cpp | 29 +++++++++++++++++++++++++++-- zmq_addon.hpp | 6 ++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/tests/multipart.cpp b/tests/multipart.cpp index 6901517..4af4948 100644 --- a/tests/multipart.cpp +++ b/tests/multipart.cpp @@ -1,6 +1,8 @@ #include #include +#include + #ifdef ZMQ_HAS_RVALUE_REFS #ifdef ZMQ_CPP17 @@ -17,6 +19,22 @@ static_assert(std::is_invocable::value, "Can't construct with socket_ref"); +static_assert( + std::is_same().front()), + const zmq::message_t &>::value, + "multipart_t::front() should keep returning const message_t&"); +static_assert( + std::is_same().front()), + const zmq::message_t &>::value, + "multipart_t::front() const should return const message_t&"); +static_assert( + std::is_same().back()), + const zmq::message_t &>::value, + "multipart_t::back() should keep returning const message_t&"); +static_assert( + std::is_same().back()), + const zmq::message_t &>::value, + "multipart_t::back() const should return const message_t&"); /// \todo split this up into separate test cases /// @@ -106,11 +124,18 @@ TEST_CASE("multipart legacy test", "[multipart]") multipart.pushmem("Frame0", 6); assert(multipart.size() == 10); - const message_t &front_msg = multipart.front(); + const message_t &front_msg_nonconst = multipart.front(); + assert(&front_msg_nonconst == &multipart[0]); + + const multipart_t &const_multipart = multipart; + const message_t &front_msg = const_multipart.front(); assert(multipart.size() == 10); assert(std::string(front_msg.data(), front_msg.size()) == "Frame0"); - const message_t &back_msg = multipart.back(); + const message_t &back_msg_nonconst = multipart.back(); + assert(&back_msg_nonconst == &multipart[multipart.size() - 1]); + + const message_t &back_msg = const_multipart.back(); assert(multipart.size() == 10); assert(std::string(back_msg.data(), back_msg.size()) == "Frame9"); diff --git a/zmq_addon.hpp b/zmq_addon.hpp index 168735d..04c9966 100644 --- a/zmq_addon.hpp +++ b/zmq_addon.hpp @@ -594,9 +594,15 @@ class multipart_t // get message part from front const message_t &front() { return m_parts.front(); } + // get message part from front + const message_t &front() const { return m_parts.front(); } + // get message part from back const message_t &back() { return m_parts.back(); } + // get message part from back + const message_t &back() const { return m_parts.back(); } + // Get pointer to a specific message part const message_t *peek(size_t index) const { return &m_parts[index]; } From 1d691c21a73f516580a6c59039f78b2451aada9b Mon Sep 17 00:00:00 2001 From: mberk-yilmaz Date: Wed, 18 Mar 2026 14:31:29 +0300 Subject: [PATCH 2/2] fix: applied clang-format to fix style. No functional changes. --- tests/multipart.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/tests/multipart.cpp b/tests/multipart.cpp index 4af4948..1004410 100644 --- a/tests/multipart.cpp +++ b/tests/multipart.cpp @@ -19,22 +19,19 @@ static_assert(std::is_invocable::value, "Can't construct with socket_ref"); -static_assert( - std::is_same().front()), - const zmq::message_t &>::value, - "multipart_t::front() should keep returning const message_t&"); +static_assert(std::is_same().front()), + const zmq::message_t &>::value, + "multipart_t::front() should keep returning const message_t&"); static_assert( std::is_same().front()), const zmq::message_t &>::value, "multipart_t::front() const should return const message_t&"); -static_assert( - std::is_same().back()), - const zmq::message_t &>::value, - "multipart_t::back() should keep returning const message_t&"); -static_assert( - std::is_same().back()), - const zmq::message_t &>::value, - "multipart_t::back() const should return const message_t&"); +static_assert(std::is_same().back()), + const zmq::message_t &>::value, + "multipart_t::back() should keep returning const message_t&"); +static_assert(std::is_same().back()), + const zmq::message_t &>::value, + "multipart_t::back() const should return const message_t&"); /// \todo split this up into separate test cases ///