Skip to content

Commit c9b95c0

Browse files
committed
support unnamed units properly
1 parent 05fac66 commit c9b95c0

File tree

2 files changed

+5
-14
lines changed

2 files changed

+5
-14
lines changed

include/units/units.hpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,7 @@ template <isQuantity Q, typename quotient> using Rooted = Named<
184184
std::ratio_divide<typename Q::angle, quotient>, std::ratio_divide<typename Q::temperature, quotient>,
185185
std::ratio_divide<typename Q::luminosity, quotient>, std::ratio_divide<typename Q::moles, quotient>>>;
186186

187-
template <isQuantity Q>
188-
requires(!std::is_same_v<Named<Q>, Q>)
189-
struct std::formatter<Q> : std::formatter<double> {
190-
auto format(const Q& quantity, std::format_context& ctx) const {
191-
// return std::format_to(ctx.out(), "{}", quantity.internal());
192-
return std::formatter<Named<Q>>::format(quantity, ctx);
193-
}
194-
};
195-
196-
template <isQuantity Q>
197-
requires std::is_same_v<Named<Q>, Q>
198-
struct std::formatter<Q> : std::formatter<double> {
187+
template <isQuantity Q> struct std::formatter<Q> : std::formatter<double> {
199188
auto format(const Q& quantity, std::format_context& ctx) const {
200189
constinit static std::array<std::pair<intmax_t, intmax_t>, 8> dims {{
201190
{Q::mass::num, Q::mass::den},
@@ -406,8 +395,8 @@ constexpr Number operator""_num(unsigned long long value) {
406395
}
407396

408397
template <> struct std::formatter<Number> : std::formatter<double> {
409-
auto format(const Number& quantity, std::format_context& ctx) const {
410-
return std::format_to(ctx.out(), "{}", quantity.internal());
398+
auto format(const Number& number, std::format_context& ctx) const {
399+
return std::formatter<double>::format(number.internal(), ctx);
411400
}
412401
};
413402

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ void initialize() {
1313
std::cout << std::format("{:.2f}", 1.2345) << std::endl;
1414
std::cout << units::pow<5>(505_cm) * 15_celsius << std::endl;
1515
std::cout << std::format("{:.2f}", units::pow<5>(505_cm) * 15_celsius) << std::endl;
16+
Number a(2.123);
17+
std::cout << std::format("{:.2f}", a) << std::endl;
1618
}
1719

1820
constexpr void miscTests() {

0 commit comments

Comments
 (0)