From cb86cd2bcd8ea96f842142b1b98906fe21cf56dc Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Tue, 15 Sep 2020 08:55:35 +0200 Subject: [PATCH] fix: regression in quantity::op<<() for the case of std::setw() fixed --- src/include/units/bits/unit_text.h | 10 ++++++++++ src/include/units/quantity.h | 11 +++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/include/units/bits/unit_text.h b/src/include/units/bits/unit_text.h index f10ab0ff..95421d51 100644 --- a/src/include/units/bits/unit_text.h +++ b/src/include/units/bits/unit_text.h @@ -178,4 +178,14 @@ constexpr auto unit_text() } } +template +void to_stream(std::basic_ostream& os, const quantity& q) +{ + os << q.count(); + constexpr auto symbol = detail::unit_text(); + if constexpr (symbol.standard().size()) { + os << " " << symbol.standard(); + } +} + } // namespace units::detail diff --git a/src/include/units/quantity.h b/src/include/units/quantity.h index 57b84410..72e53405 100644 --- a/src/include/units/quantity.h +++ b/src/include/units/quantity.h @@ -334,11 +334,14 @@ public: template friend std::basic_ostream& operator<<(std::basic_ostream& os, const quantity& q) { - os << q.count(); - constexpr auto symbol = detail::unit_text(); - if constexpr (symbol.standard().size()) { - os << " " << symbol.standard(); + if(os.width()) { + // std::setw() applies to the whole quantity output so it has to be first put into std::string + std::basic_ostringstream s; + detail::to_stream(s, q); + return os << s.str(); } + + detail::to_stream(os, q); return os; } };