From 7737167d1dcf2f9bc1880d880c512729689aeef2 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Wed, 11 May 2022 18:10:51 +0200 Subject: [PATCH] fix: unit symbol text output unit tests fixed --- src/core/include/units/bits/unit_text.h | 28 +++++++++---------- .../include/units/generic/dimensionless.h | 2 +- src/core/include/units/unit.h | 3 ++ 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/core/include/units/bits/unit_text.h b/src/core/include/units/bits/unit_text.h index a8c323e4..d0118397 100644 --- a/src/core/include/units/bits/unit_text.h +++ b/src/core/include/units/bits/unit_text.h @@ -26,6 +26,7 @@ #include #include #include +#include namespace units::detail { @@ -57,7 +58,7 @@ constexpr auto ratio_text() } } -template +template constexpr auto prefix_or_ratio_text() { if constexpr (R.num == 1 && R.den == 1 && R.exp == 0) { @@ -67,7 +68,7 @@ constexpr auto prefix_or_ratio_text() // try to form a prefix using prefix = downcast>; - if constexpr (!is_same_v>) { + if constexpr (can_be_prefixed && !is_same_v>) { // print as a prefixed unit return prefix::symbol; } else { @@ -138,19 +139,16 @@ constexpr auto unit_text() // print as a prefix or ratio of a coherent unit using coherent_unit = dimension_unit; - if constexpr (has_symbol) { - // use predefined coherent unit symbol - constexpr auto symbol_text = coherent_unit::symbol; - constexpr auto prefix_txt = - prefix_or_ratio_text(); - return prefix_txt + symbol_text; - } else { - // use derived dimension ingredients to create a unit symbol - constexpr auto symbol_text = derived_dimension_unit_text(); - constexpr auto prefix_txt = - prefix_or_ratio_text(); - return prefix_txt + symbol_text; - } + constexpr auto symbol_text = []() { + if constexpr (has_symbol) + return coherent_unit::symbol; + else + return derived_dimension_unit_text(); + }(); + + constexpr auto prefix_txt = + prefix_or_ratio_text(); + return prefix_txt + symbol_text; } } diff --git a/src/core/include/units/generic/dimensionless.h b/src/core/include/units/generic/dimensionless.h index 562977fc..acf9e91d 100644 --- a/src/core/include/units/generic/dimensionless.h +++ b/src/core/include/units/generic/dimensionless.h @@ -30,7 +30,7 @@ namespace units { -struct one : named_unit {}; +struct one : derived_unit {}; struct percent : named_scaled_unit {}; /** diff --git a/src/core/include/units/unit.h b/src/core/include/units/unit.h index 6e8ed3ca..eddb7ea2 100644 --- a/src/core/include/units/unit.h +++ b/src/core/include/units/unit.h @@ -225,6 +225,9 @@ void can_be_prefixed_impl(const volatile alias_unit*); template inline constexpr bool can_be_prefixed = requires(U * u) { can_be_prefixed_impl(u); }; +template +inline constexpr bool can_be_prefixed> = can_be_prefixed; + } // namespace detail } // namespace units