From fceab080a2a5ed76873712d86ccca7ba1b64d59b Mon Sep 17 00:00:00 2001 From: Andrea Iob Date: Fri, 24 Jun 2022 16:17:15 +0200 Subject: [PATCH] containers: fix swap for PiercedStorage The [] operator of std::vector returns a temporary object of a proxy type called std::vector::reference, rather than an actual bool&. Although the libstdc++ defines an overload for swapping this type of proxy objects, this is just an extension to the standard. --- src/containers/piercedStorage.tpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/containers/piercedStorage.tpp b/src/containers/piercedStorage.tpp index be8dab715d..fe88640332 100644 --- a/src/containers/piercedStorage.tpp +++ b/src/containers/piercedStorage.tpp @@ -884,8 +884,20 @@ void PiercedStorage::rawErase(std::size_t pos, std::size_t n) template void PiercedStorage::rawSwap(std::size_t pos_first, std::size_t pos_second) { + std::size_t firstOffset = pos_first * m_nFields; + std::size_t secondOffset = pos_second * m_nFields; for (std::size_t k = 0; k < m_nFields; ++k) { - std::swap(m_fields[pos_first * m_nFields + k], m_fields[pos_second * m_nFields + k]); + // We cannot use std::swap because it will not work for bool storages + // + // The [] operator of std::vector returns a temporary object of a + // proxy type called std::vector::reference, rather than an actual + // bool&. + // + // Although the libstdc++ defines an overload for swapping this type of + // proxy objects, this is just an extension to the standard. + auto temp = std::move(m_fields[firstOffset + k]); + m_fields[firstOffset + k] = m_fields[secondOffset + k]; + m_fields[secondOffset + k] = std::move(temp); } }