Skip to content

Commit

Permalink
support unnamed units properly
Browse files Browse the repository at this point in the history
  • Loading branch information
SizzinSeal committed Jan 8, 2025
1 parent 05fac66 commit c9b95c0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 14 deletions.
17 changes: 3 additions & 14 deletions include/units/units.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,7 @@ template <isQuantity Q, typename quotient> using Rooted = Named<
std::ratio_divide<typename Q::angle, quotient>, std::ratio_divide<typename Q::temperature, quotient>,
std::ratio_divide<typename Q::luminosity, quotient>, std::ratio_divide<typename Q::moles, quotient>>>;

template <isQuantity Q>
requires(!std::is_same_v<Named<Q>, Q>)
struct std::formatter<Q> : std::formatter<double> {
auto format(const Q& quantity, std::format_context& ctx) const {
// return std::format_to(ctx.out(), "{}", quantity.internal());
return std::formatter<Named<Q>>::format(quantity, ctx);
}
};

template <isQuantity Q>
requires std::is_same_v<Named<Q>, Q>
struct std::formatter<Q> : std::formatter<double> {
template <isQuantity Q> struct std::formatter<Q> : std::formatter<double> {
auto format(const Q& quantity, std::format_context& ctx) const {
constinit static std::array<std::pair<intmax_t, intmax_t>, 8> dims {{
{Q::mass::num, Q::mass::den},
Expand Down Expand Up @@ -406,8 +395,8 @@ constexpr Number operator""_num(unsigned long long value) {
}

template <> struct std::formatter<Number> : std::formatter<double> {
auto format(const Number& quantity, std::format_context& ctx) const {
return std::format_to(ctx.out(), "{}", quantity.internal());
auto format(const Number& number, std::format_context& ctx) const {
return std::formatter<double>::format(number.internal(), ctx);
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ void initialize() {
std::cout << std::format("{:.2f}", 1.2345) << std::endl;
std::cout << units::pow<5>(505_cm) * 15_celsius << std::endl;
std::cout << std::format("{:.2f}", units::pow<5>(505_cm) * 15_celsius) << std::endl;
Number a(2.123);
std::cout << std::format("{:.2f}", a) << std::endl;
}

constexpr void miscTests() {
Expand Down

0 comments on commit c9b95c0

Please sign in to comment.