fix: regression in quantity::op<<() for the case of std::setw() fixed

This commit is contained in:
Mateusz Pusz
2020-09-15 08:55:35 +02:00
parent 1eee6cbe93
commit cb86cd2bcd
2 changed files with 17 additions and 4 deletions

View File

@@ -178,4 +178,14 @@ constexpr auto unit_text()
}
}
template<typename CharT, class Traits, typename D, typename U, typename Rep>
void to_stream(std::basic_ostream<CharT, Traits>& os, const quantity<D, U, Rep>& q)
{
os << q.count();
constexpr auto symbol = detail::unit_text<D, U>();
if constexpr (symbol.standard().size()) {
os << " " << symbol.standard();
}
}
} // namespace units::detail

View File

@@ -334,11 +334,14 @@ public:
template<class CharT, class Traits>
friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const quantity& q)
{
os << q.count();
constexpr auto symbol = detail::unit_text<D, U>();
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<CharT, Traits> s;
detail::to_stream(s, q);
return os << s.str();
}
detail::to_stream(os, q);
return os;
}
};