diff --git a/tests/multipart.cpp b/tests/multipart.cpp index 6901517..1004410 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,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() 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 +121,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]; }