From 5507dc6bddb3d2399e274a2641ce889c360c25fc Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Tue, 22 Dec 2020 01:07:37 +0800 Subject: [PATCH] include/denc: use pair in range-based for loop MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit map::value_type is pair, so if we use range-based for loop when iterating through a map, we should use pair instead of pair, the latter also compiles, but it might create a temporary object of pair from pair. GCC-11 complains at seeing this: ../src/include/denc.h:1002:21: warning: loop variable ‘e’ of type ‘const T&’ {aka ‘const std::pair&’} binds to a tem\ porary constructed from type ‘const std::pair’ [-Wrange-loop-constru ct] 1002 | for (const T& e : s) { | ^ this change * use the value_type of container in `maplike_details`, so we can avoid the overhead of creating temporay objects when encoding a map * define denc_traits for std::pair as well, so the elements of a map can be encoded using denc facility Signed-off-by: Kefu Chai (cherry picked from commit c828ce29400a1eea4b223229b36b3a092eda6139) --- src/include/denc.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/include/denc.h b/src/include/denc.h index dfd5e923d3881..acbca1464b1a5 100644 --- a/src/include/denc.h +++ b/src/include/denc.h @@ -876,7 +876,7 @@ struct denc_traits { template struct denc_traits< std::pair, - std::enable_if_t && denc_supported>> { + std::enable_if_t> && denc_supported>> { typedef denc_traits a_traits; typedef denc_traits b_traits; @@ -908,14 +908,14 @@ struct denc_traits< } static void decode(std::pair& v, ceph::buffer::ptr::const_iterator& p, uint64_t f=0) { - denc(v.first, p, f); + denc(const_cast&>(v.first), p, f); denc(v.second, p, f); } template static std::enable_if_t decode(std::pair& v, ceph::buffer::list::const_iterator& p, uint64_t f = 0) { - denc(v.first, p); + denc(const_cast&>(v.first), p); denc(v.second, p); } }; @@ -1116,8 +1116,7 @@ struct denc_traits< namespace _denc { template struct maplike_details : public container_details_base { - using T = std::pair; + using T = typename Container::value_type; template static void insert(Container& c, Args&& ...args) { c.emplace_hint(c.cend(), std::forward(args)...); -- 2.39.5