diff --git a/README.md b/README.md index 47a2d2f4a..0781f9f4f 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ analysis and unit/quantity manipulation. Here is a small example of possible operations: ```cpp -#include +import mp_units; using namespace mp_units; using namespace mp_units::si::unit_symbols; @@ -66,7 +66,7 @@ using namespace mp_units::si::unit_symbols; // simple numeric operations static_assert(10 * km / 2 == 5 * km); -// unit conversions +// conversions to common units static_assert(1 * h == 3600 * s); static_assert(1 * km + 1 * m == 1001 * m); @@ -90,12 +90,9 @@ and dimensional analysis can be performed without sacrificing on runtime perform accuracy. Please see the below example for a quick preview of basic library features: ```cpp -#include -#include -#include -#include -#include +#include #include +import mp_units; using namespace mp_units; @@ -118,13 +115,13 @@ int main() constexpr quantity v6 = value_cast(v4); constexpr quantity v7 = value_cast(v6); - std::cout << v1 << '\n'; // 110 km/h - std::cout << v2 << '\n'; // 70 mi/h - std::cout << std::format("{}", v3) << '\n'; // 110 km/h - std::cout << std::format("{:*^14}", v4) << '\n'; // ***70 mi/h**** - std::cout << std::format("{:%Q in %q}", v5) << '\n'; // 30.5556 in m/s - std::cout << std::format("{0:%Q} in {0:%q}", v6) << '\n'; // 31.2928 in m/s - std::cout << std::format("{:%Q}", v7) << '\n'; // 31 + std::cout << v1 << '\n'; // 110 km/h + std::cout << std::setw(10) << std::setfill('*') << v2 << '\n'; // ***70 mi/h + std::cout << std::format("{:*^10}\n", v3); // *110 km/h* + std::cout << std::format("{:%N in %U}\n", v4); // 70 in mi/h + std::cout << std::format("{:{%N:.2f}%?%U}\n", v5); // 30.56 in m/s + std::cout << std::format("{:{%N:.2f}%?{%U:n}}\n", v6); // 31.29 in m s⁻¹ + std::cout << std::format("{:%N}\n", v7); // 31 } ``` diff --git a/docs/getting_started/look_and_feel.md b/docs/getting_started/look_and_feel.md index 581fd1ba0..f0a7307bc 100644 --- a/docs/getting_started/look_and_feel.md +++ b/docs/getting_started/look_and_feel.md @@ -13,7 +13,7 @@ Here is a small example of operations possible on scalar quantities: // simple numeric operations static_assert(10 * km / 2 == 5 * km); - // unit conversions + // conversions to common units static_assert(1 * h == 3600 * s); static_assert(1 * km + 1 * m == 1001 * m); @@ -40,7 +40,7 @@ Here is a small example of operations possible on scalar quantities: // simple numeric operations static_assert(10 * km / 2 == 5 * km); - // unit conversions + // conversions to common units static_assert(1 * h == 3600 * s); static_assert(1 * km + 1 * m == 1001 * m); @@ -56,7 +56,7 @@ Here is a small example of operations possible on scalar quantities: static_assert(1000 / (1 * s) == 1 * kHz); ``` -!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/81Ev7qhTd)" +!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/ox8a8dGTz)" This library requires some C++20 features ([concepts and constraints](https://en.cppreference.com/w/cpp/language/constraints), @@ -69,6 +69,7 @@ performed without sacrificing accuracy. Please see the below example for a quick === "C++ modules" ```cpp + #include #include import mp_units; @@ -93,13 +94,13 @@ performed without sacrificing accuracy. Please see the below example for a quick constexpr quantity v6 = value_cast(v4); constexpr quantity v7 = value_cast(v6); - std::cout << v1 << '\n'; // 110 km/h - std::cout << v2 << '\n'; // 70 mi/h - std::cout << std::format("{}", v3) << '\n'; // 110 km/h - std::cout << std::format("{:*^14}", v4) << '\n'; // ***70 mi/h**** - std::cout << std::format("{:%Q in %q}", v5) << '\n'; // 30.5556 in m/s - std::cout << std::format("{0:%Q} in {0:%q}", v6) << '\n'; // 31.2928 in m/s - std::cout << std::format("{:%Q}", v7) << '\n'; // 31 + std::cout << v1 << '\n'; // 110 km/h + std::cout << std::setw(10) << std::setfill('*') << v2 << '\n'; // ***70 mi/h + std::cout << std::format("{:*^10}\n", v3); // *110 km/h* + std::cout << std::format("{:%N in %U}\n", v4); // 70 in mi/h + std::cout << std::format("{:{%N:.2f}%?%U}\n", v5); // 30.56 in m/s + std::cout << std::format("{:{%N:.2f}%?{%U:n}}\n", v6); // 31.29 in m s⁻¹ + std::cout << std::format("{:%N}\n", v7); // 31 } ``` @@ -111,6 +112,7 @@ performed without sacrificing accuracy. Please see the below example for a quick #include #include #include + #include #include using namespace mp_units; @@ -134,13 +136,13 @@ performed without sacrificing accuracy. Please see the below example for a quick constexpr quantity v6 = value_cast(v4); constexpr quantity v7 = value_cast(v6); - std::cout << v1 << '\n'; // 110 km/h - std::cout << v2 << '\n'; // 70 mi/h - std::cout << std::format("{}", v3) << '\n'; // 110 km/h - std::cout << std::format("{:*^14}", v4) << '\n'; // ***70 mi/h**** - std::cout << std::format("{:%Q in %q}", v5) << '\n'; // 30.5556 in m/s - std::cout << std::format("{0:%Q} in {0:%q}", v6) << '\n'; // 31.2928 in m/s - std::cout << std::format("{:%Q}", v7) << '\n'; // 31 + std::cout << v1 << '\n'; // 110 km/h + std::cout << std::setw(10) << std::setfill('*') << v2 << '\n'; // ***70 mi/h + std::cout << std::format("{:*^10}\n", v3); // *110 km/h* + std::cout << std::format("{:%N in %U}\n", v4); // 70 in mi/h + std::cout << std::format("{:{%N:.2f}%?%U}\n", v5); // 30.56 in m/s + std::cout << std::format("{:{%N:.2f}%?{%U:n}}\n", v6); // 31.29 in m s⁻¹ + std::cout << std::format("{:%N}\n", v7); // 31 } ``` diff --git a/docs/users_guide/examples/hello_units.md b/docs/users_guide/examples/hello_units.md index 8e2935a97..61656ef24 100644 --- a/docs/users_guide/examples/hello_units.md +++ b/docs/users_guide/examples/hello_units.md @@ -18,20 +18,20 @@ First, we either import the `mp_units` module or include the headers for: - text formatting and stream output support ```cpp title="hello_units.cpp" linenums="1" ---8<-- "example/hello_units.cpp:28:39" +--8<-- "example/hello_units.cpp:28:40" ``` Also, to shorten the definitions, we "import" all the symbols from the `mp_units` namespace. -```cpp title="hello_units.cpp" linenums="12" ---8<-- "example/hello_units.cpp:40:41" +```cpp title="hello_units.cpp" linenums="13" +--8<-- "example/hello_units.cpp:41:42" ``` Next, we define a simple function that calculates the average speed based on the provided arguments of length and time: -```cpp title="hello_units.cpp" linenums="13" ---8<-- "example/hello_units.cpp:42:45" +```cpp title="hello_units.cpp" linenums="14" +--8<-- "example/hello_units.cpp:43:46" ``` The above function template takes any quantities implicitly convertible to `isq::length` @@ -45,16 +45,16 @@ that its quantity type is implicitly convertible to `isq::speed`. type is beneficial for users of such a function as it provides more information of what to expect from a function than just using `auto`. -```cpp title="hello_units.cpp" linenums="17" ---8<-- "example/hello_units.cpp:47:50" +```cpp title="hello_units.cpp" linenums="18" +--8<-- "example/hello_units.cpp:48:51" ``` The above lines explicitly opt into using unit symbols from two systems of units. As this introduces a lot of short identifiers into the current scope, it is not done implicitly while including a header file. -```cpp title="hello_units.cpp" linenums="21" ---8<-- "example/hello_units.cpp:52:58" +```cpp title="hello_units.cpp" linenums="22" +--8<-- "example/hello_units.cpp:53:59" ``` - Lines `21` & `22` create a quantity of kind `isq::length / isq::time` with the numbers @@ -74,8 +74,8 @@ implicitly while including a header file. - Line `27` does a [value-truncating conversion](../framework_basics/value_conversions.md#value-truncating-conversions) of changing the underlying representation type from `double` to `int`. -```cpp title="hello_units.cpp" linenums="28" ---8<-- "example/hello_units.cpp:60" +```cpp title="hello_units.cpp" linenums="29" +--8<-- "example/hello_units.cpp:61" ``` The above presents [various ways to print a quantity](../framework_basics/text_output.md). diff --git a/example/clcpp_response.cpp b/example/clcpp_response.cpp index 98664b85c..db2be2cae 100644 --- a/example/clcpp_response.cpp +++ b/example/clcpp_response.cpp @@ -118,7 +118,7 @@ void calcs_comparison() const auto L1A = 2.f * fm; const auto L2A = 3.f * fm; const auto LrA = L1A + L2A; - std::cout << MP_UNITS_STD_FMT::format("{:%.30Q %q}\n + {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, LrA); + std::cout << MP_UNITS_STD_FMT::format("{:{%N:.30} %U}\n + {:{%N:.30} %U}\n = {:{%N:.30} %U}\n\n", L1A, L2A, LrA); std::cout << "The single unit method must convert large\n" "or small values in other units to the base unit.\n" @@ -127,17 +127,17 @@ void calcs_comparison() const auto L1B = L1A.in(m); const auto L2B = L2A.in(m); const auto LrB = L1B + L2B; - std::cout << MP_UNITS_STD_FMT::format("{:%.30eQ %q}\n + {:%.30eQ %q}\n = {:%.30eQ %q}\n\n", L1B, L2B, LrB); + std::cout << MP_UNITS_STD_FMT::format("{:{%N:.30e} %U}\n + {:{%N:.30e} %U}\n = {:{%N:.30e} %U}\n\n", L1B, L2B, LrB); std::cout << "In multiplication and division:\n\n"; const quantity ArA = L1A * L2A; - std::cout << MP_UNITS_STD_FMT::format("{:%.30Q %q}\n * {:%.30Q %q}\n = {:%.30Q %q}\n\n", L1A, L2A, ArA); + std::cout << MP_UNITS_STD_FMT::format("{:{%N:.30} %U}\n * {:{%N:.30} %U}\n = {:{%N:.30} %U}\n\n", L1A, L2A, ArA); std::cout << "similar problems arise\n\n"; const quantity ArB = L1B * L2B; - std::cout << MP_UNITS_STD_FMT::format("{:%.30eQ %q}\n * {:%.30eQ %q}\n = {:%.30eQ %q}\n\n", L1B, L2B, ArB); + std::cout << MP_UNITS_STD_FMT::format("{:{%N:.30e} %U}\n * {:{%N:.30e} %U}\n = {:{%N:.30e} %U}\n\n", L1B, L2B, ArB); } } // namespace diff --git a/example/conversion_factor.cpp b/example/conversion_factor.cpp index e98dd694d..308fb2a00 100644 --- a/example/conversion_factor.cpp +++ b/example/conversion_factor.cpp @@ -58,7 +58,7 @@ int main() std::cout << MP_UNITS_STD_FMT::format("therefore ratio lengthA / lengthB == {}\n\n", lengthA / lengthB); - std::cout << MP_UNITS_STD_FMT::format("conversion factor from lengthA::unit of {:%q} to lengthB::unit of {:%q}:\n\n", + std::cout << MP_UNITS_STD_FMT::format("conversion factor from lengthA::unit of {:%U} to lengthB::unit of {:%U}:\n\n", lengthA, lengthB) << MP_UNITS_STD_FMT::format("lengthB.value( {} ) == lengthA.value( {} ) * conversion_factor( {} )\n", lengthB.numerical_value_ref_in(lengthB.unit), diff --git a/example/glide_computer.cpp b/example/glide_computer.cpp index b834e917f..5408d39fd 100644 --- a/example/glide_computer.cpp +++ b/example/glide_computer.cpp @@ -87,8 +87,8 @@ void print(const R& gliders) std::cout << "- Polar:\n"; for (const auto& p : g.polar) { const auto ratio = glide_ratio(g.polar[0]).force_in(one); - std::cout << MP_UNITS_STD_FMT::format(" * {:%.4Q %q} @ {:%.1Q %q} -> {:%.1Q %q} ({:%.1Q %q})\n", p.climb, p.v, - ratio, + std::cout << MP_UNITS_STD_FMT::format(" * {:{%N:.4} %U} @ {:{%N:.1} %U} -> {:{%N:.1} %U} ({:{%N:.1} %U})\n", + p.climb, p.v, ratio, // TODO is it possible to make ADL work below (we need another set of trig // functions for strong angle in a different namespace) si::asin(1 / ratio).force_in(si::degree)); @@ -106,8 +106,8 @@ void print(const R& conditions) for (const auto& c : conditions) { std::cout << "- " << c.first << "\n"; const auto& w = c.second; - std::cout << " * Cloud base: " << MP_UNITS_STD_FMT::format("{:%.0Q %q}", w.cloud_base) << " AGL\n"; - std::cout << " * Thermals strength: " << MP_UNITS_STD_FMT::format("{:%.1Q %q}", w.thermal_strength) << "\n"; + std::cout << " * Cloud base: " << MP_UNITS_STD_FMT::format("{:{%N:.0} %U}", w.cloud_base) << " AGL\n"; + std::cout << " * Thermals strength: " << MP_UNITS_STD_FMT::format("{:{%N:.1} %U}", w.thermal_strength) << "\n"; std::cout << "\n"; } } @@ -119,7 +119,7 @@ void print(const R& waypoints) std::cout << "Waypoints:\n"; std::cout << "==========\n"; for (const auto& w : waypoints) - std::cout << MP_UNITS_STD_FMT::format("- {}: {} {}, {:%.1Q %q}\n", w.name, w.pos.lat, w.pos.lon, w.alt); + std::cout << MP_UNITS_STD_FMT::format("- {}: {} {}, {:{%N:.1} %U}\n", w.name, w.pos.lat, w.pos.lon, w.alt); std::cout << "\n"; } @@ -130,12 +130,12 @@ void print(const task& t) std::cout << "- Start: " << t.get_start().name << "\n"; std::cout << "- Finish: " << t.get_finish().name << "\n"; - std::cout << "- Length: " << MP_UNITS_STD_FMT::format("{:%.1Q %q}", t.get_distance()) << "\n"; + std::cout << "- Length: " << MP_UNITS_STD_FMT::format("{:{%N:.1} %U}", t.get_distance()) << "\n"; std::cout << "- Legs: " << "\n"; for (const auto& l : t.get_legs()) - std::cout << MP_UNITS_STD_FMT::format(" * {} -> {} ({:%.1Q %q})\n", l.begin().name, l.end().name, + std::cout << MP_UNITS_STD_FMT::format(" * {} -> {} ({:{%N:.1} %U})\n", l.begin().name, l.end().name, l.get_distance()); std::cout << "\n"; } @@ -144,7 +144,7 @@ void print(const safety& s) { std::cout << "Safety:\n"; std::cout << "=======\n"; - std::cout << "- Min AGL separation: " << MP_UNITS_STD_FMT::format("{:%.0Q %q}", s.min_agl_height) << "\n"; + std::cout << "- Min AGL separation: " << MP_UNITS_STD_FMT::format("{:{%N:.0} %U}", s.min_agl_height) << "\n"; std::cout << "\n"; } @@ -153,8 +153,8 @@ void print(const aircraft_tow& tow) std::cout << "Tow:\n"; std::cout << "====\n"; std::cout << "- Type: aircraft\n"; - std::cout << "- Height: " << MP_UNITS_STD_FMT::format("{:%.0Q %q}", tow.height_agl) << "\n"; - std::cout << "- Performance: " << MP_UNITS_STD_FMT::format("{:%.1Q %q}", tow.performance) << "\n"; + std::cout << "- Height: " << MP_UNITS_STD_FMT::format("{:{%N:.0} %U}", tow.height_agl) << "\n"; + std::cout << "- Performance: " << MP_UNITS_STD_FMT::format("{:{%N:.1} %U}", tow.performance) << "\n"; std::cout << "\n"; } diff --git a/example/glide_computer_lib/glide_computer_lib.cpp b/example/glide_computer_lib/glide_computer_lib.cpp index 8c3af99f9..85cf2a0e6 100644 --- a/example/glide_computer_lib/glide_computer_lib.cpp +++ b/example/glide_computer_lib/glide_computer_lib.cpp @@ -82,7 +82,8 @@ void print(std::string_view phase_name, timestamp start_ts, const glide_computer const glide_computer::flight_point& new_point) { std::cout << MP_UNITS_STD_FMT::format( - "| {:<12} | {:>9%.1Q %q} (Total: {:>9%.1Q %q}) | {:>8%.1Q %q} (Total: {:>8%.1Q %q}) | {:>7%.0Q %q} ({:>6%.0Q %q}) " + "| {:<12} | {:>9{%N:.1} %U} (Total: {:>9{%N:.1} %U}) | {:>8{%N:.1} %U} (Total: {:>8{%N:.1} %U}) | {:>7{%N:.0} %U} " + "({:>6{%N:.0} %U}) " "|\n", phase_name, value_cast(new_point.ts - point.ts), value_cast(new_point.ts - start_ts), new_point.dist - point.dist, new_point.dist, new_point.alt - point.alt, new_point.alt); diff --git a/example/hello_units.cpp b/example/hello_units.cpp index 9725926a4..c54b9b04f 100644 --- a/example/hello_units.cpp +++ b/example/hello_units.cpp @@ -26,6 +26,7 @@ // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #include +#include #include #ifdef MP_UNITS_MODULES import mp_units; @@ -57,11 +58,11 @@ int main() constexpr quantity v6 = value_cast(v4); constexpr quantity v7 = value_cast(v6); - std::cout << v1 << '\n'; // 110 km/h - std::cout << v2 << '\n'; // 70 mi/h - std::cout << MP_UNITS_STD_FMT::format("{}", v3) << '\n'; // 110 km/h - std::cout << MP_UNITS_STD_FMT::format("{:*^14}", v4) << '\n'; // ***70 mi/h**** - std::cout << MP_UNITS_STD_FMT::format("{:%Q in %q}", v5) << '\n'; // 30.5556 in m/s - std::cout << MP_UNITS_STD_FMT::format("{0:%Q} in {0:%q}", v6) << '\n'; // 31.2928 in m/s - std::cout << MP_UNITS_STD_FMT::format("{:%Q}", v7) << '\n'; // 31 + std::cout << v1 << '\n'; // 110 km/h + std::cout << std::setw(10) << std::setfill('*') << v2 << '\n'; // ***70 mi/h + std::cout << MP_UNITS_STD_FMT::format("{:*^10}\n", v3); // *110 km/h* + std::cout << MP_UNITS_STD_FMT::format("{:%N in %U}\n", v4); // 70 in mi/h + std::cout << MP_UNITS_STD_FMT::format("{:{%N:.2f}%?%U}\n", v5); // 30.56 in m/s + std::cout << MP_UNITS_STD_FMT::format("{:{%N:.2f}%?{%U:n}}\n", v6); // 31.29 in m s⁻¹ + std::cout << MP_UNITS_STD_FMT::format("{:%N}\n", v7); // 31 } diff --git a/example/include/geographic.h b/example/include/geographic.h index 9ee949195..e2b921244 100644 --- a/example/include/geographic.h +++ b/example/include/geographic.h @@ -55,12 +55,14 @@ std::basic_ostream& operator<<(std::basic_ostream& } // namespace geographic -template<> -struct MP_UNITS_STD_FMT::formatter : formatter { +template +struct MP_UNITS_STD_FMT::formatter : + formatter { template - auto format(const geographic::msl_altitude& a, FormatContext& ctx) + auto format(const geographic::msl_altitude& a, FormatContext& ctx) const -> decltype(ctx.out()) { - formatter::format(a - geographic::mean_sea_level, ctx); + ctx.advance_to( + formatter::format(a - geographic::mean_sea_level, ctx)); return MP_UNITS_STD_FMT::format_to(ctx.out(), " AMSL"); } }; @@ -137,29 +139,29 @@ class std::numeric_limits> : public numeric_limits { static constexpr auto max() noexcept { return geographic::longitude(180); } }; -template -struct MP_UNITS_STD_FMT::formatter> : - formatter::quantity_type> { +template +struct MP_UNITS_STD_FMT::formatter, Char> : + formatter::quantity_type, Char> { template - auto format(geographic::latitude lat, FormatContext& ctx) + auto format(geographic::latitude lat, FormatContext& ctx) const -> decltype(ctx.out()) { const auto& q = lat.quantity_ref_from(geographic::equator); - formatter::quantity_type>::format(is_gteq_zero(q) ? q : -q, ctx); - MP_UNITS_STD_FMT::format_to(ctx.out(), "{}", is_gteq_zero(q) ? " N" : "S"); - return ctx.out(); + ctx.advance_to( + formatter::quantity_type, Char>::format(is_gteq_zero(q) ? q : -q, ctx)); + return MP_UNITS_STD_FMT::format_to(ctx.out(), "{}", is_gteq_zero(q) ? " N" : "S"); } }; -template -struct MP_UNITS_STD_FMT::formatter> : - formatter::quantity_type> { +template +struct MP_UNITS_STD_FMT::formatter, Char> : + formatter::quantity_type, Char> { template - auto format(geographic::longitude lon, FormatContext& ctx) + auto format(geographic::longitude lon, FormatContext& ctx) const -> decltype(ctx.out()) { const auto& q = lon.quantity_ref_from(geographic::prime_meridian); - formatter::quantity_type>::format(is_gteq_zero(q) ? q : -q, ctx); - MP_UNITS_STD_FMT::format_to(ctx.out(), "{}", is_gteq_zero(q) ? " E" : " W"); - return ctx.out(); + ctx.advance_to( + formatter::quantity_type, Char>::format(is_gteq_zero(q) ? q : -q, ctx)); + return MP_UNITS_STD_FMT::format_to(ctx.out(), "{}", is_gteq_zero(q) ? " E" : " W"); } }; diff --git a/example/include/ranged_representation.h b/example/include/ranged_representation.h index 2b7fff524..678f650a4 100644 --- a/example/include/ranged_representation.h +++ b/example/include/ranged_representation.h @@ -63,11 +63,11 @@ template inline constexpr bool mp_units::treat_as_floating_point> = mp_units::treat_as_floating_point; -template -struct MP_UNITS_STD_FMT::formatter> : formatter { +template +struct MP_UNITS_STD_FMT::formatter, Char> : formatter { template - auto format(const ranged_representation& v, FormatContext& ctx) + auto format(const ranged_representation& v, FormatContext& ctx) const -> decltype(ctx.out()) { - return formatter::format(v.value(), ctx); + return formatter::format(v.value(), ctx); } }; diff --git a/example/include/validated_type.h b/example/include/validated_type.h index 814cefd74..f9a8e5163 100644 --- a/example/include/validated_type.h +++ b/example/include/validated_type.h @@ -123,11 +123,11 @@ std::basic_ostream& operator<<(std::basic_ostream& } -template -struct MP_UNITS_STD_FMT::formatter> : formatter { +template +struct MP_UNITS_STD_FMT::formatter, Char> : formatter { template - auto format(const validated_type& v, FormatContext& ctx) + auto format(const validated_type& v, FormatContext& ctx) const -> decltype(ctx.out()) { - return formatter::format(v.value(), ctx); + return formatter::format(v.value(), ctx); } }; diff --git a/example/si_constants.cpp b/example/si_constants.cpp index 2567210ad..a2f38295d 100644 --- a/example/si_constants.cpp +++ b/example/si_constants.cpp @@ -45,19 +45,19 @@ int main() using namespace mp_units::si::unit_symbols; std::cout << "The seven defining constants of the SI and the seven corresponding units they define:\n"; - std::cout << MP_UNITS_STD_FMT::format("- hyperfine transition frequency of Cs: {} = {:%.0Q %q}\n", + std::cout << MP_UNITS_STD_FMT::format("- hyperfine transition frequency of Cs: {} = {:{%N:.0} %U}\n", 1. * si2019::hyperfine_structure_transition_frequency_of_cs, (1. * si2019::hyperfine_structure_transition_frequency_of_cs).in(Hz)); - std::cout << MP_UNITS_STD_FMT::format("- speed of light in vacuum: {} = {:%.0Q %q}\n", + std::cout << MP_UNITS_STD_FMT::format("- speed of light in vacuum: {} = {:{%N:.0} %U}\n", 1. * si2019::speed_of_light_in_vacuum, (1. * si2019::speed_of_light_in_vacuum).in(m / s)); - std::cout << MP_UNITS_STD_FMT::format("- Planck constant: {} = {:%.8eQ %q}\n", + std::cout << MP_UNITS_STD_FMT::format("- Planck constant: {} = {:{%N:.8e} %U}\n", 1. * si2019::planck_constant, (1. * si2019::planck_constant).in(J * s)); - std::cout << MP_UNITS_STD_FMT::format("- elementary charge: {} = {:%.9eQ %q}\n", + std::cout << MP_UNITS_STD_FMT::format("- elementary charge: {} = {:{%N:.9e} %U}\n", 1. * si2019::elementary_charge, (1. * si2019::elementary_charge).in(C)); - std::cout << MP_UNITS_STD_FMT::format("- Boltzmann constant: {} = {:%.6eQ %q}\n", + std::cout << MP_UNITS_STD_FMT::format("- Boltzmann constant: {} = {:{%N:.6e} %U}\n", 1. * si2019::boltzmann_constant, (1. * si2019::boltzmann_constant).in(J / K)); - std::cout << MP_UNITS_STD_FMT::format("- Avogadro constant: {} = {:%.8eQ %q}\n", + std::cout << MP_UNITS_STD_FMT::format("- Avogadro constant: {} = {:{%N:.8e} %U}\n", 1. * si2019::avogadro_constant, (1. * si2019::avogadro_constant).in(one / mol)); std::cout << MP_UNITS_STD_FMT::format("- luminous efficacy: {} = {}\n", 1. * si2019::luminous_efficacy, (1. * si2019::luminous_efficacy).in(lm / W)); diff --git a/example/spectroscopy_units.cpp b/example/spectroscopy_units.cpp index 0badec0b5..78869409b 100644 --- a/example/spectroscopy_units.cpp +++ b/example/spectroscopy_units.cpp @@ -55,8 +55,8 @@ template T1, QuantityOf T2, QuantityOf< QuantityOf T4, QuantityOf T5> void print_line(const std::tuple& t) { - MP_UNITS_STD_FMT::println("| {:<15} | {:<15} | {:<15} | {:<15} | {:<15} |", std::get<0>(t), std::get<1>(t), - std::get<2>(t), std::get<3>(t), std::get<4>(t)); + std::cout << MP_UNITS_STD_FMT::format("| {:<15} | {:<15} | {:<15} | {:<15} | {:<15} |\n", std::get<0>(t), + std::get<1>(t), std::get<2>(t), std::get<3>(t), std::get<4>(t)); } // prints quantities in semi-SI units @@ -65,9 +65,9 @@ template T1, QuantityOf T2, QuantityOf< QuantityOf T4, QuantityOf T5> void print_line_si(const std::tuple& t) { - MP_UNITS_STD_FMT::println("| {:<15} | {:<15} | {:<15} | {:<15} | {:<15} |", std::get<0>(t).in(eV), - std::get<1>(t).in(one / cm), std::get<2>(t).in(THz), std::get<3>(t).in(K), - std::get<4>(t).in(um)); + std::cout << MP_UNITS_STD_FMT::format("| {:<15} | {:<15} | {:<15} | {:<15} | {:<15} |\n", std::get<0>(t).in(eV), + std::get<1>(t).in(one / cm), std::get<2>(t).in(THz), std::get<3>(t).in(K), + std::get<4>(t).in(um)); } int main() @@ -92,16 +92,16 @@ int main() const auto t5 = std::make_tuple(isq::energy(h * c / q5), isq::wavenumber(1 / q5), isq::frequency(c / q5), isq::thermodynamic_temperature(h * c / (q5 * kb)), q5); - MP_UNITS_STD_FMT::println("| {:<15} | {:<15} | {:<15} | {:<15} | {:<15} |", "Energy", "Wavenumber", "Frequency", - "Temperature", "Wavelength"); - MP_UNITS_STD_FMT::println("| {0:-^15} | {0:-^15} | {0:-^15} | {0:-^15} | {0:-^15} |", ""); + std::cout << MP_UNITS_STD_FMT::format("| {:<15} | {:<15} | {:<15} | {:<15} | {:<15} |\n", "Energy", "Wavenumber", + "Frequency", "Temperature", "Wavelength"); + std::cout << MP_UNITS_STD_FMT::format("| {0:-^15} | {0:-^15} | {0:-^15} | {0:-^15} | {0:-^15} |\n", ""); print_line(t1); print_line(t2); print_line(t3); print_line(t4); print_line(t5); - MP_UNITS_STD_FMT::println("| {0:-^15} | {0:-^15} | {0:-^15} | {0:-^15} | {0:-^15} |", ""); + std::cout << MP_UNITS_STD_FMT::format("| {0:-^15} | {0:-^15} | {0:-^15} | {0:-^15} | {0:-^15} |\n", ""); print_line_si(t1); print_line_si(t2); print_line_si(t3); diff --git a/example/unmanned_aerial_vehicle.cpp b/example/unmanned_aerial_vehicle.cpp index 05bf9309f..13f248401 100644 --- a/example/unmanned_aerial_vehicle.cpp +++ b/example/unmanned_aerial_vehicle.cpp @@ -79,13 +79,13 @@ std::basic_ostream& operator<<(std::basic_ostream& return os << a - a.absolute_point_origin << " HAE(" << to_text(a.absolute_point_origin.egm) << ")"; } -template +template requires(is_hae(QP::absolute_point_origin)) -struct MP_UNITS_STD_FMT::formatter : formatter { +struct MP_UNITS_STD_FMT::formatter : formatter { template - auto format(const QP& a, FormatContext& ctx) + auto format(const QP& a, FormatContext& ctx) const -> decltype(ctx.out()) { - formatter::format(a - a.absolute_point_origin, ctx); + formatter::format(a - a.absolute_point_origin, ctx); return MP_UNITS_STD_FMT::format_to(ctx.out(), " HAE({})", to_text(QP::absolute_point_origin.egm)); } }; @@ -123,12 +123,12 @@ std::basic_ostream& operator<<(std::basic_ostream& return os << a.quantity_from(height_above_launch) << " HAL"; } -template<> -struct MP_UNITS_STD_FMT::formatter : formatter { +template +struct MP_UNITS_STD_FMT::formatter : formatter { template - auto format(const hal_altitude& a, FormatContext& ctx) + auto format(const hal_altitude& a, FormatContext& ctx) const -> decltype(ctx.out()) { - formatter::format(a.quantity_from(height_above_launch), ctx); + formatter::format(a.quantity_from(height_above_launch), ctx); return MP_UNITS_STD_FMT::format_to(ctx.out(), " HAL"); } }; @@ -170,6 +170,6 @@ int main() }; waypoint wpt = {"EPPR", {54.24772_N, 18.6745_E}, mean_sea_level + 16. * ft}; - std::cout << MP_UNITS_STD_FMT::format("{}: {} {}, {:%.2Q %q}, {:%.2Q %q}\n", wpt.name, wpt.pos.lat, wpt.pos.lon, + std::cout << MP_UNITS_STD_FMT::format("{}: {} {}, {:{%N:.2} %U}, {:{%N:.2} %U}\n", wpt.name, wpt.pos.lat, wpt.pos.lon, wpt.msl_alt, to_hae(wpt.msl_alt, wpt.pos)); }