diff --git a/2.2/feed_rss_created.xml b/2.2/feed_rss_created.xml index a721d3d81..004e1351c 100644 --- a/2.2/feed_rss_created.xml +++ b/2.2/feed_rss_created.xml @@ -1 +1 @@ - mp-unitsThe quantities and units library for C++https://mpusz.github.io/mp-units/2.2/mp-units Teamhttps://github.com/mpusz/mp-unitsen Mon, 26 Feb 2024 12:51:21 -0000 Mon, 26 Feb 2024 12:51:21 -0000 1440 MkDocs RSS plugin - v1.12.1 mp-units 2.1.0 released! mpusz Releases <h1>mp-units 2.1.0 released!</h1><p><strong>A new product version can be obtained from<a href="https://github.com/mpusz/mp-units/releases/tag/v2.1.0">GitHub</a> and<a href="https://conan.io/center/recipes/mp-units?version=2.1.0">Conan</a>.</strong></p><p>The list of the most significant changes introduced by the new version can be found in our<a href="../../release_notes.md#2.1.0">Release Notes</a>. We will also describe the most important of themin this post.</p>https://mpusz.github.io/mp-units/2.2/blog/2023/12/09/mp-units-210-released/ Sat, 09 Dec 2023 00:00:00 +0000mp-unitshttps://mpusz.github.io/mp-units/2.2/blog/2023/12/09/mp-units-210-released/ Report from the Kona 2023 ISO C++ Committee meeting mpusz WG21 <h1>Report from the Kona 2023 ISO C++ Committee meeting</h1><p><strong>Several groups in the ISO C++ Committee reviewed the <a href="https://wg21.link/p1935">P1935: A C++ Approach to Physical Units</a>proposal in Belfast 2019 and Prague 2020. All those groups expressed interest in the potentialstandardization of such a library and encouraged further work. The authors also got valuableinitial feedback that highly influenced the design of the V2 version of the mp-units library.</strong></p><p>In the following years, we scoped on getting more feedback from the production and design. Thisresulted in version 2 of the <strong>mp-units</strong> library that resolved many issues the users and Committeemembers raised. The features and interfaces of this version are close to being the best we can getwith the current version of the C++ language standard.</p>https://mpusz.github.io/mp-units/2.2/blog/2023/11/12/report-from-the-kona-2023-iso-c-committee-meeting/ Sun, 12 Nov 2023 00:00:00 +0000mp-unitshttps://mpusz.github.io/mp-units/2.2/blog/2023/11/12/report-from-the-kona-2023-iso-c-committee-meeting/ What's new in mp-units 2.0? mpusz Releases <h1>What's new in mp-units 2.0?</h1><p><strong>After a year of hard work, we've just released mp-units 2.0.0. It can be obtained from<a href="https://github.com/mpusz/mp-units/releases/tag/v2.0.0">GitHub</a> and<a href="https://conan.io/center/recipes/mp-units?version=2.0.0">Conan</a>.</strong></p><p>The list of the most significant changes introduced by the new version can be found in our<a href="../../release_notes.md#2.0.0">Release Notes</a>. We will also describe some of them in this post.</p>https://mpusz.github.io/mp-units/2.2/blog/2023/09/24/whats-new-in-mp-units-20/ Sun, 24 Sep 2023 00:00:00 +0000mp-unitshttps://mpusz.github.io/mp-units/2.2/blog/2023/09/24/whats-new-in-mp-units-20/ \ No newline at end of file + mp-unitsThe quantities and units library for C++https://mpusz.github.io/mp-units/2.2/mp-units Teamhttps://github.com/mpusz/mp-unitsen Mon, 26 Feb 2024 13:03:06 -0000 Mon, 26 Feb 2024 13:03:06 -0000 1440 MkDocs RSS plugin - v1.12.1 mp-units 2.1.0 released! mpusz Releases <h1>mp-units 2.1.0 released!</h1><p><strong>A new product version can be obtained from<a href="https://github.com/mpusz/mp-units/releases/tag/v2.1.0">GitHub</a> and<a href="https://conan.io/center/recipes/mp-units?version=2.1.0">Conan</a>.</strong></p><p>The list of the most significant changes introduced by the new version can be found in our<a href="../../release_notes.md#2.1.0">Release Notes</a>. We will also describe the most important of themin this post.</p>https://mpusz.github.io/mp-units/2.2/blog/2023/12/09/mp-units-210-released/ Sat, 09 Dec 2023 00:00:00 +0000mp-unitshttps://mpusz.github.io/mp-units/2.2/blog/2023/12/09/mp-units-210-released/ Report from the Kona 2023 ISO C++ Committee meeting mpusz WG21 <h1>Report from the Kona 2023 ISO C++ Committee meeting</h1><p><strong>Several groups in the ISO C++ Committee reviewed the <a href="https://wg21.link/p1935">P1935: A C++ Approach to Physical Units</a>proposal in Belfast 2019 and Prague 2020. All those groups expressed interest in the potentialstandardization of such a library and encouraged further work. The authors also got valuableinitial feedback that highly influenced the design of the V2 version of the mp-units library.</strong></p><p>In the following years, we scoped on getting more feedback from the production and design. Thisresulted in version 2 of the <strong>mp-units</strong> library that resolved many issues the users and Committeemembers raised. The features and interfaces of this version are close to being the best we can getwith the current version of the C++ language standard.</p>https://mpusz.github.io/mp-units/2.2/blog/2023/11/12/report-from-the-kona-2023-iso-c-committee-meeting/ Sun, 12 Nov 2023 00:00:00 +0000mp-unitshttps://mpusz.github.io/mp-units/2.2/blog/2023/11/12/report-from-the-kona-2023-iso-c-committee-meeting/ What's new in mp-units 2.0? mpusz Releases <h1>What's new in mp-units 2.0?</h1><p><strong>After a year of hard work, we've just released mp-units 2.0.0. It can be obtained from<a href="https://github.com/mpusz/mp-units/releases/tag/v2.0.0">GitHub</a> and<a href="https://conan.io/center/recipes/mp-units?version=2.0.0">Conan</a>.</strong></p><p>The list of the most significant changes introduced by the new version can be found in our<a href="../../release_notes.md#2.0.0">Release Notes</a>. We will also describe some of them in this post.</p>https://mpusz.github.io/mp-units/2.2/blog/2023/09/24/whats-new-in-mp-units-20/ Sun, 24 Sep 2023 00:00:00 +0000mp-unitshttps://mpusz.github.io/mp-units/2.2/blog/2023/09/24/whats-new-in-mp-units-20/ \ No newline at end of file diff --git a/2.2/feed_rss_updated.xml b/2.2/feed_rss_updated.xml index dfd7fe600..b5e5efffd 100644 --- a/2.2/feed_rss_updated.xml +++ b/2.2/feed_rss_updated.xml @@ -1 +1 @@ - mp-unitsThe quantities and units library for C++https://mpusz.github.io/mp-units/2.2/mp-units Teamhttps://github.com/mpusz/mp-unitsen Mon, 26 Feb 2024 12:51:21 -0000 Mon, 26 Feb 2024 12:51:21 -0000 1440 MkDocs RSS plugin - v1.12.1 mp-units 2.1.0 released! mpusz Releases <h1>mp-units 2.1.0 released!</h1><p><strong>A new product version can be obtained from<a href="https://github.com/mpusz/mp-units/releases/tag/v2.1.0">GitHub</a> and<a href="https://conan.io/center/recipes/mp-units?version=2.1.0">Conan</a>.</strong></p><p>The list of the most significant changes introduced by the new version can be found in our<a href="../../release_notes.md#2.1.0">Release Notes</a>. We will also describe the most important of themin this post.</p>https://mpusz.github.io/mp-units/2.2/blog/2023/12/09/mp-units-210-released/ Thu, 22 Feb 2024 10:23:22 +0000mp-unitshttps://mpusz.github.io/mp-units/2.2/blog/2023/12/09/mp-units-210-released/ What's new in mp-units 2.0? mpusz Releases <h1>What's new in mp-units 2.0?</h1><p><strong>After a year of hard work, we've just released mp-units 2.0.0. It can be obtained from<a href="https://github.com/mpusz/mp-units/releases/tag/v2.0.0">GitHub</a> and<a href="https://conan.io/center/recipes/mp-units?version=2.0.0">Conan</a>.</strong></p><p>The list of the most significant changes introduced by the new version can be found in our<a href="../../release_notes.md#2.0.0">Release Notes</a>. We will also describe some of them in this post.</p>https://mpusz.github.io/mp-units/2.2/blog/2023/09/24/whats-new-in-mp-units-20/ Sun, 17 Dec 2023 18:28:13 +0000mp-unitshttps://mpusz.github.io/mp-units/2.2/blog/2023/09/24/whats-new-in-mp-units-20/ Report from the Kona 2023 ISO C++ Committee meeting mpusz WG21 <h1>Report from the Kona 2023 ISO C++ Committee meeting</h1><p><strong>Several groups in the ISO C++ Committee reviewed the <a href="https://wg21.link/p1935">P1935: A C++ Approach to Physical Units</a>proposal in Belfast 2019 and Prague 2020. All those groups expressed interest in the potentialstandardization of such a library and encouraged further work. The authors also got valuableinitial feedback that highly influenced the design of the V2 version of the mp-units library.</strong></p><p>In the following years, we scoped on getting more feedback from the production and design. Thisresulted in version 2 of the <strong>mp-units</strong> library that resolved many issues the users and Committeemembers raised. The features and interfaces of this version are close to being the best we can getwith the current version of the C++ language standard.</p>https://mpusz.github.io/mp-units/2.2/blog/2023/11/12/report-from-the-kona-2023-iso-c-committee-meeting/ Sun, 17 Dec 2023 16:41:42 +0000mp-unitshttps://mpusz.github.io/mp-units/2.2/blog/2023/11/12/report-from-the-kona-2023-iso-c-committee-meeting/ \ No newline at end of file + mp-unitsThe quantities and units library for C++https://mpusz.github.io/mp-units/2.2/mp-units Teamhttps://github.com/mpusz/mp-unitsen Mon, 26 Feb 2024 13:03:06 -0000 Mon, 26 Feb 2024 13:03:06 -0000 1440 MkDocs RSS plugin - v1.12.1 mp-units 2.1.0 released! mpusz Releases <h1>mp-units 2.1.0 released!</h1><p><strong>A new product version can be obtained from<a href="https://github.com/mpusz/mp-units/releases/tag/v2.1.0">GitHub</a> and<a href="https://conan.io/center/recipes/mp-units?version=2.1.0">Conan</a>.</strong></p><p>The list of the most significant changes introduced by the new version can be found in our<a href="../../release_notes.md#2.1.0">Release Notes</a>. We will also describe the most important of themin this post.</p>https://mpusz.github.io/mp-units/2.2/blog/2023/12/09/mp-units-210-released/ Thu, 22 Feb 2024 10:23:22 +0000mp-unitshttps://mpusz.github.io/mp-units/2.2/blog/2023/12/09/mp-units-210-released/ What's new in mp-units 2.0? mpusz Releases <h1>What's new in mp-units 2.0?</h1><p><strong>After a year of hard work, we've just released mp-units 2.0.0. It can be obtained from<a href="https://github.com/mpusz/mp-units/releases/tag/v2.0.0">GitHub</a> and<a href="https://conan.io/center/recipes/mp-units?version=2.0.0">Conan</a>.</strong></p><p>The list of the most significant changes introduced by the new version can be found in our<a href="../../release_notes.md#2.0.0">Release Notes</a>. We will also describe some of them in this post.</p>https://mpusz.github.io/mp-units/2.2/blog/2023/09/24/whats-new-in-mp-units-20/ Sun, 17 Dec 2023 18:28:13 +0000mp-unitshttps://mpusz.github.io/mp-units/2.2/blog/2023/09/24/whats-new-in-mp-units-20/ Report from the Kona 2023 ISO C++ Committee meeting mpusz WG21 <h1>Report from the Kona 2023 ISO C++ Committee meeting</h1><p><strong>Several groups in the ISO C++ Committee reviewed the <a href="https://wg21.link/p1935">P1935: A C++ Approach to Physical Units</a>proposal in Belfast 2019 and Prague 2020. All those groups expressed interest in the potentialstandardization of such a library and encouraged further work. The authors also got valuableinitial feedback that highly influenced the design of the V2 version of the mp-units library.</strong></p><p>In the following years, we scoped on getting more feedback from the production and design. Thisresulted in version 2 of the <strong>mp-units</strong> library that resolved many issues the users and Committeemembers raised. The features and interfaces of this version are close to being the best we can getwith the current version of the C++ language standard.</p>https://mpusz.github.io/mp-units/2.2/blog/2023/11/12/report-from-the-kona-2023-iso-c-committee-meeting/ Sun, 17 Dec 2023 16:41:42 +0000mp-unitshttps://mpusz.github.io/mp-units/2.2/blog/2023/11/12/report-from-the-kona-2023-iso-c-committee-meeting/ \ No newline at end of file diff --git a/2.2/getting_started/quick_start/index.html b/2.2/getting_started/quick_start/index.html index 407c64a11..912fac067 100644 --- a/2.2/getting_started/quick_start/index.html +++ b/2.2/getting_started/quick_start/index.html @@ -2153,34 +2153,28 @@

Quantitiesunit_symbols namespace with a using-directive:

-
import mp_units;
-
-using namespace mp_units;
-using namespace mp_units::si::unit_symbols;  // imports all the SI symbols at once
-
-quantity q = 42 * m / s;
+
using namespace mp_units;
+using namespace mp_units::si::unit_symbols;  // imports all the SI symbols at once
+
+quantity q = 42 * m / s;
 
  • selectively select only the required and not-conflicting ones with using-declarations:

    -
    import mp_units;
    -
    -using namespace mp_units;
    -using si::unit_symbols::m;
    -using si::unit_symbols::s;
    -
    -quantity q = 42 * m / s;
    +
    using namespace mp_units;
    +using si::unit_symbols::m;
    +using si::unit_symbols::s;
    +
    +quantity q = 42 * m / s;
     
  • specify a custom not conflicting unit identifier for a unit:

    -
    import mp_units;
    -
    -using namespace mp_units;
    -constexpr Unit auto mps = si::metre / si::second;
    -
    -quantity q = 42 * mps;
    +
    using namespace mp_units;
    +constexpr Unit auto mps = si::metre / si::second;
    +
    +quantity q = 42 * mps;
     
  • diff --git a/2.2/search/search_index.json b/2.2/search/search_index.json index 8f527d53b..20c6f314e 100644 --- a/2.2/search/search_index.json +++ b/2.2/search/search_index.json @@ -1 +1 @@ -{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Welcome to mp-units!","text":"

    mp-units is a compile-time enabled feature-rich Modern C++ modular/header-only library that provides compile-time dimensional analysis and unit/quantity manipulation. Its key strengths include safety, performance, and developer experience.

    The library source code is hosted on GitHub with a permissive MIT license.

    Supported compilers

    This library tries to provide the best user experience possible with the C++ language. To achieve that, it extensively uses C++20 features and the explicit object parameter from C++23.

    Even though the library benefits from C++23 (if available), C++20 is enough to compile and use all of the library's functionality. C++23 features are hidden behind a preprocessor macro providing a backward-compatible way to use it.

    The below table provides the minimum compiler version required to compile the code using the specific feature:

    Feature gcc clang apple-clang MSVC Minimum support 12 16 15 None std::format 13 17 None None C++ modules 14 17 None None C++23 extensions 14 18 None None

    More requirements for C++ modules support can be found in the CMake's documentation.

    C++ modulesHeader files
    #include <iostream>\n#include <print>\nimport mp_units;\n\nusing namespace mp_units;\n\ninline constexpr struct smoot : named_unit<\"smoot\", mag<67> * usc::inch> {} smoot;\n\nint main()\n{\n  constexpr quantity dist = 364.4 * smoot;\n  std::println(\"Harvard Bridge length = {:{%N:.5} %U} ({:{%N:.5} %U}, {:{%N:.5} %U}) \u00b1 1 \u03b5ar\",\n               dist, dist.in(usc::foot), dist.in(si::metre));\n}\n
    #include <mp-units/format.h>\n#include <mp-units/systems/si/si.h>\n#include <mp-units/systems/usc/usc.h>\n#include <print>\n\nusing namespace mp_units;\n\ninline constexpr struct smoot : named_unit<\"smoot\", mag<67> * usc::inch> {} smoot;\n\nint main()\n{\n  constexpr quantity dist = 364.4 * smoot;\n  std::println(\"Harvard Bridge length = {:{%N:.5} %U} ({:{%N:.5} %U}, {:{%N:.5} %U}) \u00b1 1 \u03b5ar\",\n               dist, dist.in(usc::foot), dist.in(si::metre));\n}\n

    Output:

    Harvard Bridge length = 364.4 smoot (2034.6 ft, 620.14 m) \u00b1 1 \u03b5ar\n

    Try it on Compiler Explorer

    What is smoot?

    The smoot (/\u02c8smu\u02d0t/) is a nonstandard unit of length created as part of an MIT fraternity prank. It is named after Oliver R. Smoot, a fraternity pledge to Lambda Chi Alpha, who, in October 1958, lay on the Harvard Bridge (between Boston and Cambridge, Massachusetts) and was used by his fraternity brothers to measure the length of the bridge.

    One smoot equals Oliver Smoot's height at the time of the prank (five feet and seven inches). The bridge's length was measured to be 364.4 smoots plus or minus one ear, with the \"plus or minus\" intended to express the measurement uncertainty.

    Oliver Smoot graduated from MIT with the class of 1962, became a lawyer, and later became chairman of the American National Standards Institute (ANSI) and president of the International Organization for Standardization (ISO).

    More on the smoot unit of length can be found at https://en.wikipedia.org/wiki/Smoot.

    Important: Help needed!

    The mp-units library might be the subject of ISO standardization for C++29. More on this can be found in the following ISO C++ proposals:

    • P1935: A C++ Approach to Physical Units,
    • P2980: A motivation, scope, and plan for a physical quantities and units library,
    • P2981: Improving our safety with a physical quantities and units library,
    • P2982: std::quantity as a numeric type.

    We are actively looking for parties interested in field-trialing the library.

    "},{"location":"release_notes/","title":"Release Notes","text":""},{"location":"release_notes/#mp-units","title":"mp-units","text":""},{"location":"release_notes/#2.2.0","title":"2.2.0 WIP","text":"
    • (!) feat: C++ modules support added by @JohelEGP
    • (!) feat: formatting grammar improved and units formatting support added
    • (!) feat: has_unit_symbol support removed
    • (!) feat: ABI concerns resolved with introduction of u8 strings for symbols
    • feat: implicit point origins support added
    • feat: unit default point origin support added
    • feat: fma, isfinite, isinf, and isnan math function added by @NAThompson
    • feat: quantity_point support added for quantity_cast and value_cast
    • feat: value_cast<Unit, Representation> added
    • feat: interconvertible(QuantitySpec, QuantitySpec) added
    • feat: qp.quantity_from_zero() added
    • feat: underlying_type type trait added
    • feat: do not print space between a number and percent or per_mille
    • feat: ppm parts per million added by @nebkat
    • feat: atan2 2-argument arctangent added by @nebkat
    • feat: fmod floating-point division remainder added by @nebkat
    • feat: std::format support added
    • feat: unit text output support added
    • feat: formatting error messages improved
    • feat: improve types readability by eliminating extraneous () in references, prefixes, and kind_of
    • (!) refactor: zero_Fahrenheit renamed to zeroth_degree_Fahrenheit
    • (!) refactor: SI-related trigonometric functions moved to the si subnamespace
    • (!) refactor: math.h header file broke up to smaller pieces
    • (!) refactor: fixed_string interface refactored
    • (!) refactor: ReferenceOf does not take a dimension anymore
    • (!) refactor: 'o' replaced with '1' as a modifier for unit_symbol_solidus::one_denominator
    • (!) refactor: get_kind() now returns kind_of
    • refactor: math functions constraints refactored
    • refactor: si_quantities.h added to improve compile-times
    • refactor: validate_ascii_string refactored to is_basic_literal_character_set
    • fix: QuantityLike conversions required Q::rep instead of using one provided by quantity_like_traits
    • fix: QuantitySpec[Unit] replaced with make_reference in value_cast
    • fix: ice_point is now defined with the integral offset from absolute_zero
    • fix: performance regression in sudo_cast fixed
    • fix: explicit object parameter support fixed
    • fix: missing version header file added to hacks.h
    • docs: project blog and first posts added
    • docs: project documentation layout refactored
    • docs: \"Interoperability with Other Libraries\" chapter added
    • docs: \"Framework Basics\" chapters updated and cleaned up
    • docs: smoot unit example added to the main page
    • docs: \"Code Example\" chapter renamed to \"Look and Feel\" and reordered in TOC to be after \"Quick Start\"
    • docs: \"Quick Start\" chapter reworked to be simpler and include quantity points
    • docs: \"Quantity points\" chapter extended
    • docs: \"The Affine Space\" chapter updated to reflect the recent design changes
    • docs: \"Working with Legacy interfaces\" chapter added
    • docs: \"Text Output\" chapter updated
    • docs: mkdocs social plugin enabled
    • docs: project logo and custom color scheme added
    • docs: minimum compiler requirements updated
    • docs: Cairo dependency described in the MkDocs section
    • (!) build: Conan and CMake options refactored
    • (!) build: MP_UNITS_AS_SYSTEM_HEADERS support removed
    • build: gsl-lite updated to 0.41.0
    • build: catch2 updated to 3.5.1
    • build: fmt updated to 10.2.1
    • build: gitpod environment updated
    • build: check_cxx_feature_supported added
    • build(conan): generate() now set cache_variables
    • build(conan): can_run check added before running tests
    "},{"location":"release_notes/#2.1.0","title":"2.1.0 December 9, 2023","text":"
    • (!) feat: inverse() support added for dimensions, quantity_spec, units, and references (1 / s will now create quantity and not a Unit)
    • (!) feat: quantity_point does not provide zero() anymore
    • (!) feat: quantity_spec and its kind should not compare equal
    • (!) feat: mutating interface removed from fixed_string
    • (!) feat: common_type with a raw value is not needed anymore as for a long time now raw values are not convertible to the dimensionless quantities
    • (!) feat: symbol_text definition simplified
    • (!) feat: users are now allowed to inherit their own types from absolute point origins
    • (!) feat: interoperability with other libraries redesigned
    • feat: basic_fixed_string(const CharT*, std::integral_constant<std::size_t, N>) constructor added
    • feat: isq::activity added and becquerel definition updated to benefit from it
    • feat: gray and sievert now have correct associated quantity kinds
    • feat: UnitCompatibleWith concept added and applied to in(U) and force_in(U) functions
    • feat: quantities can now be multiplied and divided by units (no parenthesis needed anymore)
    • feat: Magnitude / Unit operator added
    • feat: equality for dimensions now will allow derived classes as well (but not from derived_dimension)
    • feat: zero_Fahrenheit point origin added
    • feat: equivalent point origins handling improved
    • feat(example): unit symbols added to the currency example
    • (!) refactor: unit_symbol<fmt>(U) signature refactored and the resulting text can now also be used at runtime
    • (!) refactor: make_xxx factory functions replaced with two-parameter constructors
    • (!) refactor: unit_symbol changed to consteval
    • refactor: in(U) and force_in(U) now return auto to provide better diagnostics on clang
    • refactor: quantity operators constraints refactored
    • refactor: more type members added to fixed_string definition
    • refactor: unit_symbol_formatting enums now use std::int8_t as a representation type
    • fix: symbols of named dimensionless units with the ratio = 1 were not printed
    • fix: iterator is now properly updated for all cases in unit_symbol
    • fix: Fahrenheit conversion ratio was inverted
    • fix: CommonlyInvocableQuantities was overconstrained for the current library design
    • fix: are_ingredients_convertible now mandates explicit conversion for To dimensionless quantities
    • fix: quantity_point::point_for(PO) constraints fixed
    • fix(example): latitude and longitude fixed to include 0 for N and E respectively
    • ci: clang-17 enabled
    • ci: apple-clang-15 enabled
    • ci: Added C++23 builds to the CI matrix
    • docs: \"Getting Started\" chapters updated
    • docs: \"Basic Concepts\" and \"Interface Introduction\" chapters updated
    • docs: \"Design Overview\" chapter added and \"Concepts\" chapter reworked
    • docs: \"Output stream formatting\" chapter updated
    • docs: \"Default formatting\" chapter updated
    • docs: \"Derived unit symbols generation\" chapter added
    • docs: outdated affine space chapter updated
    • docs: CameCase concept identifiers FAQ added
    • docs: gravitational_potential_energy equation fixed on a graph
    • docs: YouTube video link updated to the C++ on Sea 2023
    • docs: ISO papers reference added to docs and README
    • docs: a representation type in a dimensionless quantity FAQ fixed
    • docs: titles added to some important admonitions
    • docs: \"Terms and Definitions\" slightly updated
    • docs: \"canonical unit\" added to glossary and its documentation in code was updated
    • docs: Design overview graph updated
    "},{"location":"release_notes/#2.0.0","title":"2.0.0 September 24, 2023","text":"
    • units namespace renamed to mp_units (#317)
    • header files in the <mp-units/...> rather then in <units/...> (#317)
    • the downcasting facility is removed (#383, #211, #32)
    • unified and simplified quantity creation (#274)
    • determining the best way to create a quantity (#413)
    • V2 quantity_point (#414)
    • introduction of quantity_spec to store not only dimension but also additional information about quantities (#405)
    • quantity now takes reference object, which aggregates quantity_spec and a unit and a representation type
    • units, prefixes, dimensions, quantity specifications, and references are passed as NTTPs to templates and provide arithmetic operations and comparison
    • expression templates consistently used in all derived types to increase the readability (#351, #166)
    • derived dimensions are now factors of only base dimensions (#281)
    • convertibility of derived quantities (#427)
    • dimensions, quantity specifications, units, and references are now composable, significantly reducing the number of definitions and resulting types
    • heavily simplified unit systems definitions (no need to define unnamed derived units, systems-specific dimensions, aliases for quantities, concepts, UDLs, ... anymore)
    • improved definition of all systems
    • support for ISO 80000 Part 3-6 quantities
    • faster than lightspeed constants (#169)
    • extensions to quantity formatting with fmt
    • quantity_kind removed
    • improved casting of unit with .in(Unit), .force_in(Unit) for quantity and quantity_spec
    • numerical value accessor safety improved with .numerical_value_in(Unit) and .force_numerical_value_in(Unit)
    • quantity can no longer be constructed with a raw value (#434)
    • Implicit construction of quantities from a value (#410)
    • quantity_point can no longer be constructed with just a quantity and an explicit PointOrigin is always needed
    • ceil and floor are dangerous (#432)
    • quecto, ronto, ronna, quetta new SI prefixes support
    • comparison against zero added (#487)
    • documentation rewritten from scratch
    • many smaller changes not possible to address with the previous design (#205, #210, #134)
    "},{"location":"release_notes/#0.8.0","title":"0.8.0 June 14, 2023","text":"
    • (!) refactor: common_quantity, common_quantity_for, common_quantity_point, common_quantity_kind, and common_quantity_point_kind removed
    • (!) refactor: named_derived_unit removed as it was not used
    • (!) refactor: derived_unit renamed to derived_scaled_unit
    • (!) refactor: unit renamed to derived_unit
    • (!) refactor: U::is_named removed from the unit types and replaced with NamedUnit concept
    • (!) refactor: PrefixFamily support removed
    • (!) refactor: mi(naut) renamed to nmi
    • (!) refactor: knot unit helper renamed to kn in FPS
    • (!) refactor: knot text symbol changed from \"knot\" to \"kn\"
    • refactor: quantity op+() and op-() reimplemented in terms of reference rather then quantity types
    • refactor(example): glide_computer now use dimensionless quantities with ranged_representation as rep
    • feat: HEP system support added (thanks @RalphSteinhagen)
    • feat: floor(), ceil(), and round() support added (thanks @hofbi)
    • feat: std::format support for compliant compilers added
    • feat: conversion helpers from mp-units to std::chrono types added
    • feat: math functions can now be safely used with user-defined types
    • feat: conversion from quantity_point to std::chrono::time_point added
    • feat: nautical_mile_per_hour and knot added to si::international system
    • (!) fix: add quantity_point::origin, like std::chrono::time_point::clock
    • fix: enable any prefixes for most of the named units (beside those that use prefixes already)
    • fix: hectare definition fixed to be a prefixed version of are + other units
    • fix: account for different dimensions in quantity_point_cast's constraint
    • fix: output stream operator now properly handles state
    • fix: fmt algorithms were overconstrained with forward_iterator
    • fix: CTAD for aliases fixed
    • fix: derived_ratio calculation
    • fix: fill_t assignment operator fixed
    • fix: improve downcast mode off
    • fix: radioactivity header compilation fixed
    • fix: si::hep::dim_momentum duplicated definition fixed
    • fix: fps can now coexist with international system
    • fix: public headers fixed to be standalone
    • test: standalone public headers tests added
    • (!) build: CMake generator in Conan is no longer obtained from an environment variable
    • (!) build: Required Conan version bumped to 1.48
    • (!) build: Conan 1.48 does not set CMAKE_BUILD_TYPE in the conan_toolchain.cmake anymore
    • build: AppleClang 13 support added (thanks @fdischner)
    • build: most of the conanfile.py refactored to be Conan 2.0 ready
    • build: validate() replaced with configure() to raise errors during conan install in Conan 1.X
    • build: minimum Conan version changed to 1.40
    • build: linear-algebra Conan repo is no needed anymore
    • build: Gitpod support added
    • build: clang-format-15 support added
    • build: export config to local build (#322)
    • build: fix export name of mp-units-system
    • build: fmt updated to 8.0.1
    • build: gsl-lite updated to 0.40.0
    • build: catch2 updated to 2.13.9
    • build: doxygen updated to 1.9.4
    • build: linear_algebra/0.7.0 switched to wg21-linear_algebra/0.7.2
    • ci: VS2022, gcc-11, clang-13, clang-14, and AppleClang 13 support added
    • ci: pre-commit support added (thanks @hofbi)
    • docs: Project documentation updated
    • docs: CITATION.cff file added
    • docs: CONTRIBUTING.md updated
    "},{"location":"release_notes/#0.7.0","title":"0.7.0 May 11, 2021","text":"
    • (!) refactor: ScalableNumber renamed to Representation
    • (!) refactor: output stream operators moved to the units/quantity_io.h header file
    • (!) refactor: Refactored the library file tree
    • (!) refactor: quantity::count() renamed to quantity::number()
    • (!) refactor: data system renamed to isq::iec80000 (quantity names renamed too)
    • (!) refactor: *deduced_unit renamed to *derived_unit
    • (!) refactor: got rid of a noble_derived_unit
    • refactor: quantity (kind) point updated to reflect latest changes to quantity
    • refactor: basic concepts, quantity and quantity_cast refactored
    • refactor: abs() definition refactored to be more explicit about the return type
    • feat: quantity (point) kind support added (thanks @johelegp)
    • feat: quantity references support added (thanks @johelegp)
    • feat: quantity aliases support addded
    • feat: interoperability with std::chrono::duration and other units libraries
    • feat: CTAD for dimensionless quantity added
    • feat: modulation_rate support added (thanks @go2sh)
    • feat: SI prefixes for isq::iec80000 support added (thanks @go2sh)
    • feat: a possibility to disable quantity UDLs support with UNITS_NO_LITERALS preprocessor define
    • feat: a support to define ISQ derived dimensions in terms of different number or order of components
    • perf: preconditions check do not influence the runtime performance of a Release build
    • perf: quantity_cast() generates less assembly instructions
    • perf: temporary string creation removed from quantity::op<<()
    • perf: value initialization for quantity value removed (left with a default initialization)
    • perf: limited the equivalent trait usage
    • perf: limited the C++ Standard Library headers usage
    • perf: rvalue references support added for constructors and getters
    • (!) fix: exp() has sense only for dimensionless quantities
    • (!) fix: dim_torque now properly divides by an angle (instead of multiply) + default unit name change
    • fix: quantity's operators fixed to behave like the underlying types do
    • fix: quantity_cast() fixed to work correctly with representation types not convertible from std::intmax_t
    • fix: ambiguous case for empty type list resolved
    • fix: downcasting facility for non-default-constructible types
    • fix: restore user-warnings within the library implementation
    • fix: the text symbol of foot_pound_force and foot_pound_force_per_second
    • fix: quantity modulo arithmetics fixed
    • (!) build: Conan testing version is now hosted on Artifactory
    • (!) build: Linear Algebra is now hosted on its Artifactory
    • (!) build: BUILD_DOCS CMake option renamed to UNITS_BUILD_DOCS
    • build: doxygen updated to 1.8.20
    • build: catch2 updated to 2.13.4
    • build: fmt updated to 7.1.3
    • build: ms-gsl replaced with gsl-lite/0.38.0
    • build: Conan generator switched to cmake_find_package_multi
    • build: Conan CMakeToolchain support added
    • build: CMake scripts cleanup
    • build: ccache support added
    • ci: CI switched from Travis CI to GitHub Actions
    "},{"location":"release_notes/#0.6.0","title":"0.6.0 September 13, 2020","text":"
    • feat: quantity_point support added (thanks @johelegp)
    • feat: Added angle as SI base dimension (thanks @kwikius)
    • feat: si::angular_velocity support added (thanks @mikeford3)
    • feat: FPS system added (thanks @mikeford3)
    • feat: Added support for mathematical function exp(quantity)
    • feat: Localization support for text output added (thanks @rbrugo)
    • feat: Added STL random number distribution wrappers (thanks @yasamoka)
    • (!) refactor: Refactored and cleaned up the library file tree
    • (!) refactor: q_* UDL renamed to _q_*
    • (!) refactor: UDLs with \"per\" in name renamed from *p* to *_per_*
    • (!) refactor: ratio changed to the NTTP kind
    • (!) refactor: exp and Exp renamed to exponent and Exponent
    • (!) refactor: Scalar concept renamed to ScalableNumber
    • (!) refactor: Dimensionless quantities redesigned to be of a quantity type
    • refactor: math.h function signatures refactored to use a Quantity concept (thanks @kwikius)
    • refactor: [[nodiscard]] added to many functions
    • fix: si::day unit symbol fixed to d (thanks @komputerwiz)
    • fix: si::mole unit symbol fixed to mol (thanks @mikeford3)
    • (!) build: gcc-9 is no longer supported (at least gcc-10 is required)
    • build: Visual Studio 16.7 support added
    • build: linear_algebra updated to 0.7.0/stable
    • build: fmt updated to 7.0.3
    • build: range-v3 updated to 0.11.0
    • build: catch2 updated to 2.13.0
    • build: doxygen updated to 1.8.18
    • build: ms-gsl 3.1.0 dependency added
    • build: Removed the dependency on a git submodule with common CMake scripts
    "},{"location":"release_notes/#0.5.0","title":"0.5.0 May 17, 2020","text":"
    • Major refactoring and rewrite of the library
    • Units are now independent from dimensions
    • Dimensions now depend on units (base or coherent units are provided in a class template)
    • Quantity gets a Dimension template parameter again (as unit does not provide information about its dimension anymore)
    • Spaceship operator support added
    • Added official CGS system support
    • Added official data information system support
    • Repository file tree cleanup
    • ratio refactored to contain Exp template parameter (thanks a lot @oschonrock!)
    • SI fundamental constants added
    • q_ prefix applied to all the UDLs (thanks @kwikius)
    • unknown_unit renamed to unknown_coherent_unit
    • Project documentation greatly extended and switched to Sphinx
    • A few more usage examples added
    • ASCII-only output support added (thanks @yasamoka)
    • Representation values formatting extended (thanks @rbrugo)
    • Output streams formatting support added
    • Linear algebra from std::experimental::math support added
    • Named SI units and their dimensions added (thanks @rbrugo
    • libfmt updated to 6.2.0
    • Added absolute functions and epsilon to math.h (thanks @mikeford3)
    • Added a lot of prefixes to named units and introduced alias_unit (thanks @yasamoka)
    • Linking with Conan targets only when they exists (#98)
    • All physical dimensions and units put into physical namespace
    • CMake improvements
    • Velocity renamed to speed

    Many thanks to GitHub users @oschonrock, @kwikius, and @i-ky for their support in drafting a new library design.

    "},{"location":"release_notes/#0.4.0","title":"0.4.0 Nov 17, 2019","text":"
    • Support for derived dimensions in exp added
    • Added pow() and sqrt() operations on quantities
    • units removed from a std::experimental namespace
    • Downcasting facility refactored so the user does not have to write the boilerplate code anymore
    • From now on base dimensions should inherit from base_dimension class template
    • Added unit symbols definitions to base_dimension and derived units
    • Added support for operator<< on quantity
    • fmt support added
    • Derived unit factory helpers refactored
    • Refactored the way prefixed units are defined
    "},{"location":"release_notes/#0.3.1","title":"0.3.1 Sep 18, 2019","text":"
    • cmcstl2 dependency changed to range-v3 0.9.1
    "},{"location":"release_notes/#0.3.0","title":"0.3.0 Sep 16, 2019","text":"
    • The design as described on CppCon 2019 talk (https://youtu.be/0YW6yxkdhlU)
    • Applied the feedback from the Cologne evening session
    • upcasting_traits renamed to downcasting_traits
    • Dimension template parameter removed from quantity
    • units moved to a std::experimental namespace
    • Leading underscore prefix removed from UDLs
    • Added a few more derived dimensions
    • meter renamed to metre
    • Missing operator* added
    • Predefined dimensions moved to a dedicated directory
    • dimension_ prefix removed from names of derived dimensions
    • cmcstl2 library updated to 2019.09.19
    • base_dimension is a value provided as const& to the exp type
    • integrated with Compiler Explorer
    • gsl-lite dependency removed
    • Fractional dimension exponents support added
    • QuantityOf concept introduced
    • quantity_cast<U, Rep>() support added
    "},{"location":"release_notes/#0.2.0","title":"0.2.0 July 18, 2019","text":"
    • The design as described on C++Now 2019 talk (https://youtu.be/wKchCktZPHU)
    • Added C++20 features supported by gcc-9.1 (std::remove_cvref_t, down with typename, std::type_identity)
    • Compile-time performance optimizations (type_list, common_ratio, ratio, conditional_t)
    "},{"location":"release_notes/#0.1.0","title":"0.1.0 May 18, 2019","text":"
    • Initial library release
    • Begin semantic versioning
    • The last version to work with gcc-8
    "},{"location":"appendix/glossary/","title":"Glossary","text":""},{"location":"appendix/glossary/#iso-definitions","title":"ISO definitions","text":"

    Note

    The ISO terms provided below are only a few of many defined in the ISO/IEC Guide 99.

    quantity
    • Property of a phenomenon, body, or substance, where the property has a magnitude that can be expressed by means of a number and a reference.
    • A reference can be a measurement unit, a measurement procedure, a reference material, or a combination of such.
    • A quantity as defined here is a scalar. However, a vector or a tensor, the components of which are quantities, is also considered to be a quantity.
    • The concept \u2019quantity\u2019 may be generically divided into, e.g. \u2018physical quantity\u2019, \u2018chemical quantity\u2019, and \u2018biological quantity\u2019, or \u2018base quantity\u2019 and \u2018derived quantity\u2019.
    • Examples of quantities are: length, radius, wavelength, energy, electric charge, etc.
    kind of quantity, kind
    • Aspect common to mutually comparable quantities.
    • The division of the concept \u2018quantity\u2019 into several kinds is to some extent arbitrary, for example:
      • the quantities diameter, circumference, and wavelength are generally considered to be quantities of the same kind, namely, of the kind of quantity called length,
      • the quantities heat, kinetic energy, and potential energy are generally considered to be quantities of the same kind, namely of the kind of quantity called energy.
    • Quantities of the same kind within a given system of quantities have the same quantity dimension. However, quantities of the same dimension are not necessarily of the same kind.
      • For example, the quantities moment of force and energy are, by convention, not regarded as being of the same kind, although they have the same dimension. Similarly for heat capacity and entropy, as well as for number of entities, relative permeability, and mass fraction.
    system of quantities
    • Set of quantities together with a set of non-contradictory equations relating those quantities.
    • Examples of systems of quantities are: the International System of Quantities, the Imperial System, etc.
    base quantity
    • Quantity in a conventionally chosen subset of a given system of quantities, where no quantity in the subset can be expressed in terms of the others.
    • Base quantities are referred to as being mutually independent since a base quantity cannot be expressed as a product of powers of the other base quantities.
    • \u2018Number of entities\u2019 can be regarded as a base quantity in any system of quantities.
    derived quantity
    • Quantity, in a system of quantities, defined in terms of the base quantities of that system.
    International System of Quantities, ISQ
    • System of quantities based on the seven base quantities: length, mass, time, electric current, thermodynamic temperature, amount of substance, and luminous intensity.
    • This system of quantities is published in the ISO 80000 and IEC 80000 series Quantities and units.
    • The International System of Units (SI) is based on the ISQ.
    quantity dimension, dimension of a quantity, dimension
    • Expression of the dependence of a quantity on the base quantities of a system of quantities as a product of powers of factors corresponding to the base quantities, omitting any numerical factor.
      • e.g. in the ISQ, the quantity dimension of force is denoted by \\(\\textsf{dim }F = \\mathsf{LMT}^{\u20132}\\).
    • A power of a factor is the factor raised to an exponent. Each factor is the dimension of a base quantity.
    • In deriving the dimension of a quantity, no account is taken of its scalar, vector, or tensor character.
    • In a given system of quantities:
      • quantities of the same kind have the same quantity dimension,
      • quantities of different quantity dimensions are always of different kinds,
      • quantities having the same quantity dimension are not necessarily of the same kind.
    • Symbols representing the dimensions of the base quantities in the ISQ are:

      Base quantity Symbol for dimension length \\(\\mathsf{L}\\) mass \\(\\mathsf{M}\\) time \\(\\mathsf{T}\\) electric current \\(\\mathsf{I}\\) thermodynamic temperature \\(\\mathsf{\u0398}\\) amount of substance \\(\\mathsf{N}\\) luminous intensity \\(\\mathsf{J}\\)

      Thus, the dimension of a quantity \\(Q\\) is denoted by \\(\\textsf{dim }Q = \\mathsf{L}^\u03b1\\mathsf{M}^\u03b2\\mathsf{T}^\u03b3\\mathsf{I}^\u03b4\\mathsf{\u0398}^\u03b5\\mathsf{N}^\u03b6\\mathsf{J}^\u03b7\\) where the exponents, named dimensional exponents, are positive, negative, or zero.

    quantity of dimension one, dimensionless quantity
    • quantity for which all the exponents of the factors corresponding to the base quantities in its quantity dimension are zero.
    • The term \u201cdimensionless quantity\u201d is commonly used and is kept here for historical reasons. It stems from the fact that all exponents are zero in the symbolic representation of the dimension for such quantities. The term \u201cquantity of dimension one\u201d reflects the convention in which the symbolic representation of the dimension for such quantities is the symbol \\(1\\).
    • The measurement units and values of quantities of dimension one are numbers, but such quantities convey more information than a number.
    • Some quantities of dimension one are defined as the ratios of two quantities of the same kind.
    • Numbers of entities are quantities of dimension one.
    measurement unit, unit of measurement, unit
    • Real scalar quantity, defined and adopted by convention, with which any other quantity of the same kind can be compared to express the ratio of the two quantities as a number.
    • Measurement units are designated by conventionally assigned names and symbols.
    • Measurement units of quantities of the same quantity dimension may be designated by the same name and symbol even when the quantities are not of the same kind.
      • For example, joule per kelvin and J/K are respectively the name and symbol of both a measurement unit of heat capacity and a measurement unit of entropy, which are generally not considered to be quantities of the same kind.
    • However, in some cases special measurement unit names are restricted to be used with quantities of specific kind only.
      • For example, the measurement unit \u2018second to the power minus one\u2019 (\\(\\mathsf{1/s}\\)) is called hertz (\\(\\mathsf{Hz}\\)) when used for frequencies and becquerel (\\(\\mathsf{Bq}\\)) when used for activities of radionuclides. As another example, the joule (\\(\\mathsf{J}\\)) is used as a unit of energy, but never as a unit of moment of force, e.g. the newton metre (\\(\\mathsf{N\u00b7m}\\)).
    • Measurement units of quantities of dimension one are numbers. In some cases, these measurement units are given special names, e.g. radian, steradian, and decibel, or are expressed by quotients such as millimole per mole equal to \\(10^{\u22123}\\) and microgram per kilogram equal to \\(10^{\u22129}\\).
    base unit
    • Measurement unit that is adopted by convention for a base quantity.
    • In each coherent system of units, there is only one base unit for each base quantity.
      • e.g. in the SI, the metre is the base unit of length. In the CGS systems, the centimetre is the base unit of length.
    • A base unit may also serve for a derived quantity of the same quantity dimension.
    • For number of entities, the number one, symbol \\(1\\), can be regarded as a base unit in any system of units.
    derived unit
    • Measurement unit for a derived quantity.
    • For example, the metre per second, symbol m/s, and the centimetre per second, symbol cm/s, are derived units of speed in the SI. The kilometre per hour, symbol km/h, is a measurement unit of speed outside the SI but accepted for use with the SI. The knot, equal to one nautical mile per hour, is a measurement unit of speed outside the SI.
    coherent derived unit
    • Derived unit that, for a given system of quantities and for a chosen set of base units, is a product of powers of base units with no other proportionality factor than one.
    • A power of a base unit is the base unit raised to an exponent.
    • Coherence can be determined only with respect to a particular system of quantities and a given set of base units.
      • For example, if the metre, the second, and the mole are base units, the metre per second is the coherent derived unit of velocity when velocity is defined by the quantity equation \\(v = \\mathsf{d}r/\\mathsf{d}t\\), and the mole per cubic metre is the coherent derived unit of amount-of-substance concentration when amount-of-substance concentration is defined by the quantity equation \\(c = n/V\\). The kilometre per hour and the knot, given as examples of derived units, are not coherent derived units in such a system of quantities.
    • A derived unit can be coherent with respect to one system of quantities but not to another.
      • For example, the centimetre per second is the coherent derived unit of speed in a CGS system of units but is not a coherent derived unit in the SI.
    • The coherent derived unit for every derived quantity of dimension one in a given system of units is the number one, symbol \\(1\\). The name and symbol of the measurement unit one are generally not indicated.
    system of units
    • Set of base units and derived units, together with their multiples and submultiples, defined in accordance with given rules, for a given system of quantities.
    coherent system of units
    • System of units, based on a given system of quantities, in which the measurement unit for each derived quantity is a coherent derived unit.
    • A system of units can be coherent only with respect to a system of quantities and the adopted base units.
    • For a coherent system of units, numerical value equations have the same form, including numerical factors, as the corresponding quantity equations.
    off-system measurement unit, off-system unit
    • Measurement unit that does not belong to a given system of units.
    • For example, the electronvolt (about \\(1.602\\;18 \u00d7 10^{\u201319}\\;\\mathsf{J}\\)) is an off-system measurement unit of energy with respect to the SI. Day, hour, minute are off-system measurement units of time with respect to the SI.
    International System of Units, SI
    • System of units, based on the International System of Quantities, their names and symbols, including a series of prefixes and their names and symbols, together with rules for their use, adopted by the General Conference on Weights and Measures (CGPM).
    quantity value, value of a quantity, value
    • Number and reference together expressing magnitude of a quantity.
      • For example, length of a given rod: \\(5.34\\;\\mathsf{m}\\) or \\(534\\;\\mathsf{cm}\\).
    • The number can be complex.
    • A quantity value can be presented in more than one way.
    • In the case of vector or tensor quantities, each component has a quantity value.
      • For example, force acting on a given particle, e.g. in Cartesian components \\((F_x; F_y; F_z) = (\u221231.5; 43.2; 17.0)\\;\\mathsf{N}\\).
    numerical quantity value, numerical value of a quantity, numerical value
    • Number in the expression of a quantity value, other than any number serving as the reference
      • For example, in an amount-of-substance fraction equal to \\(3\\;\\mathsf{mmol/mol}\\), the numerical quantity value is \\(3\\) and the unit is \\(\\mathsf{mmol/mol}\\). The unit \\(\\mathsf{mmol/mol}\\) is numerically equal to \\(0.001\\), but this number \\(0.001\\) is not part of the numerical quantity value, which remains \\(3\\).
    quantity equation
    • Mathematical relation between quantities in a given system of quantities, independent of measure\u00adment units.
    • For example, \\(T = (1/2) mv^2\\) where \\(T\\) is the kinetic energy and \\(v\\) the speed of a specified particle of mass \\(m\\).
    unit equation
    • Mathematical relation between base units, coher\u00adent derived units or other measurement units.
    • For example, \\(\\mathsf{J} := \\mathsf{kg}\\:\\mathsf{m}^2/\\mathsf{s}^2\\), where, \\(\\mathsf{J}\\), \\(\\mathsf{kg}\\), \\(\\mathsf{m}\\), and \\(\\mathsf{s}\\) are the symbols for the joule, kilogram, metre, and second, respectively. (The symbol \\(:=\\) denotes \u201cis by definition equal to\u201d as given in the ISO 80000 and IEC 80000 series.). \\(1\\;\\mathsf{km/h} = (1/3.6)\\;\\mathsf{m/s}\\).
    numerical value equation, numerical quantity value equation
    • Mathematical relation between numerical quantity values, based on a given quantity equation and specified measurement units.
    • For example, in the quantity equation for kinetic energy of a particle, \\(T = (1/2) mv^2\\), if \\(m = 2\\;\\mathsf{kg}\\) and \\(v = 3\\;\\mathsf{m/s}\\), then \\({T} = (1/2)\\:\u00d7\\:2\\:\u00d7\\:3^2\\) is a numerical value equation giving the numerical value \\(9\\) of \\(T\\) in joules.
    "},{"location":"appendix/glossary/#other-definitions","title":"Other definitions","text":"

    Info

    The below terms extend the official ISO glossary and are commonly referred to by the mp-units library.

    base dimension
    • A dimension of a base quantity.
    derived dimension
    • A dimension of a derived quantity.
    • Implemented as an expression template being the result of the dimension equation on base dimensions.
    dimension equation
    • Mathematical relation between dimensions in a given system of quantities, independent of measure\u00adment units.
    quantity kind hierarchy, quantity hierarchy
    • Quantities of the same kind form a hierarchy that determines their:
      • convertibility (e.g. every width is a length, but width should not be convertible to height)
      • common quantity type (e.g. width + height -> length)
    quantity character, character of a quantity, character
    • Scalars, vectors and tensors are mathematical objects that can be used to denote certain physical quantities and their values. They are as such independent of the particular choice of a coordinate system, whereas each scalar component of a vector or a tensor and each component vector and component tensor depend on that choice.
    • A vector is a tensor of the first order and a scalar is a tensor of order zero.
    • For vectors and tensors, the components are quantities that can be expressed as a product of a number and a unit.
    • Vectors and tensors can also be expressed as a numerical value vector or tensor, respectively, multiplied by a unit.
    • Quantities of different characters support different set of operations.
      • For example, a quantity can be multiplied by another one only if any of them has scalar character. Vectors and tensors can't be multiplied or divided, but they support additional operations like dot and cross products, which are not available for scalars.
    • The term \u2019character\u2019 was borrowed from the below quote:

    ISO 80000-1_2009

    In deriving the dimension of a quantity, no account is taken of its scalar, vector, or tensor character.

    quantity specification, quantity_spec
    • An entity storing all the information about a specific quantity:
      • location in a quantity hierarchy
      • quantity equation
      • dimension of a quantity
      • quantity kind
      • quantity character
      • additional constraints (e.g. non-negative)
    • Dimension of a quantity is not enough to specify all the properties of a quantity.
    unit with an associated quantity, associated unit
    • Unit that is used to measure quantities of a specific kind in a given system of units.
    quantity reference, reference
    • According to its definition, quantity can be expressed by means of a number and a reference
    • In the mp-units library, a reference describes all the required meta-information associated with a specific quantity (quantity specification and unit).
    canonical representation of a unit, canonical unit
    • A canonical representation of a unit consists of:
      • a reference unit being the result of extraction of all the intermediate derived units,
      • a magnitude being a product of all the prefixes and magnitudes of extracted scaled units.
    • All units having the same canonical unit are deemed equal.
    • All units having the same reference unit are convertible (their magnitude may differ and is used during conversion).
    reference unit

    See canonical representation of a unit

    absolute quantity point origin, absolute point origin
    • An explicit point on an axis of values of a specific quantity type that serves as an absolute reference point for all quantity points which definitions are (explicitly or implicitly) based on it.
    • For example, mean sea level is commonly used as an absolute reference point to measure altitudes.
    relative quantity point origin, relative point origin
    • An explicit, known at compile-time, point on an axis of values of a specific quantity type serving as a reference for other quantities.
    • For example, an ice point is a quantity point with a value of \\(273.15\\;\\mathsf{K}\\) that is used as the zero point of a degree Celsius scale.
    quantity point origin, point origin
    • Either an absolute point origin or a relative point origin.
    quantity point, absolute quantity
    • An absolute quantity with respect to an origin.
    • For example, timestamp (as opposed to duration), altitude (as opposed to height), absolute temperature (as opposed to temperature difference).
    "},{"location":"appendix/references/","title":"References","text":"ISO80000

    ISO 80000-1:2009(E) \"Quantities and units \u2014 Part 1: General\", International Organization for Standardization.

    Quincey \"Angles in the SI: a detailed proposal for solving the problem, Quincey, Paul (1 October 2021). SIBrochure

    The International System of Units (SI), International Bureau of Weights and Measures (20 May 2019), ISBN 978-92-822-2272-0.

    "},{"location":"blog/","title":"Blog","text":""},{"location":"blog/2023/09/24/whats-new-in-mp-units-20/","title":"What's new in mp-units 2.0?","text":"

    After a year of hard work, we've just released mp-units 2.0.0. It can be obtained from GitHub and Conan.

    The list of the most significant changes introduced by the new version can be found in our Release Notes. We will also describe some of them in this post.

    "},{"location":"blog/2023/09/24/whats-new-in-mp-units-20/#why-20-if-10-was-never-released","title":"Why 2.0 if 1.0 was never released?","text":"

    Version 2 of the mp-units project is a huge change and a new quality for the users. We did not want to pretend that 2.0 is an evolutionary upgrade of the previous version of the project. It feels like a different product.

    We could start a new repo named \"mp-units-v2\" similarly to range-v3 but we decided not to go this path. We kept the same repo and made the scope of the changes and potential breakage explicit with a drastic bump in the project version.

    "},{"location":"blog/2023/09/24/whats-new-in-mp-units-20/#what-has-changed","title":"What has changed?","text":"

    The answer is \"nearly everything\". The whole library and its documentation were rewritten nearly from scratch.

    Here are the significant changes that the users can observe:

    • Repository name

      If you didn't notice, the repository name was changed from \"mpusz/units\" to \"mpusz/mp-units\".

    • Header files content and layout

      Previously, all the header files resided in the include/units directory. Now, they can be found in include/mp-units. The project file tree was significantly changed as well. Many files were moved to different subdirectories or renamed.

    • Namespace

      Previously, all the definitions were provided in the units namespace, and now they are in the mp_units one.

    • Abstractions, interfaces, definitions

      The interfaces of all of the types were refactored. We got unit symbols and a new way to construct a quantity and quantity_point. The readability of the generated types was improved thanks to the introduction of expression templates. Nearly all of the template arguments are now passed by values thanks to class NTTP extensions in C++20. As a result, unit definitions are much easier and terser. Also, the V2 has a powerful ability to model systems of quantities and provides definitions for many ISQ quantities.

    • Conan 2.0

      Also, now we support Conan 2.0, which provides an updated way of handling dependencies.

    "},{"location":"blog/2023/09/24/whats-new-in-mp-units-20/#what-is-gone","title":"What is gone?","text":"

    Some cornerstones of the initial design did not prove in practice and were removed while we moved to version 2.

    "},{"location":"blog/2023/09/24/whats-new-in-mp-units-20/#the-downcasting-facility","title":"The downcasting facility","text":"

    The first and the most important of such features was removing the downcasting facility. This feature is still a powerful metaprogramming technique that allows users to map long class template instantiations to nicely named, short, and easy-to-understand user's strong types.

    Such mapping works perfectly fine for 1-to-1 relationships. However, we often deal with N-to-1 connections in the quantities and units domain. Here are only a few such examples:

    • work and torque have the same dimension \\(L^2MT^{-2}\\),
    • becquerel and hertz have the same definition of \\(s^{-1}\\),
    • litre and cubic decimetre have the same factor.

    In the above examples, multiple entities \"wanted\" to register different names for identical class template instantiations, resulting in compile-time errors. We had to invent some hacks and workarounds to make it work, but we were never satisfied with the outcome.

    Additionally, this facility could easily lead to ODR violations or provide different results depending on which header files were included in the translation units. This was too vulnerable to be considered a good practice here.

    "},{"location":"blog/2023/09/24/whats-new-in-mp-units-20/#no-udls-anymore","title":"No UDLs anymore","text":"

    Over the years, we have learned that UDLs are not a good solution. More information on this subject can be found in the Why don't we use UDLs to create quantities? chapter.

    "},{"location":"blog/2023/09/24/whats-new-in-mp-units-20/#no-construction-of-a-quantity-from-a-raw-value","title":"No construction of a quantity from a raw value","text":"

    To improve safety, we no longer allow the construction of quantities from raw values. In the new design, we always need to explicitly specify a unit to create a quantity:

    quantity q1 = 42 * m;\nquantity<si::metre> = 2 * km;\nquantity q3(42, si::metre);\n

    The previous approach was reported to be error-prone under maintenance. More on this subject can be found in the Why can't I create a quantity by passing a number to a constructor? chapter.

    "},{"location":"blog/2023/09/24/whats-new-in-mp-units-20/#new-look-and-feel","title":"New look and feel","text":"

    Here is a concise example showing you the new look and feel of the library:

    #include <mp-units/format.h>\n#include <mp-units/systems/isq/isq.h>\n#include <mp-units/systems/si/si.h>\n#include <format>\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\nquantity<isq::speed[m / s]> avg_speed(quantity<si::metre> d,\n                                      quantity<si::second> t)\n{ return d / t; }\n\nint main()\n{\n  auto speed = avg_speed(220 * km, 2 * h);\n  std::println(\"{}\", speed);  // 30.5556 m/s\n}\n

    All of the changes we provided, although breaking ones, resulted in much better, easier, and safer abstractions. These offer a new quantity on the market and hopefully will be appreciated by our users.

    Please check our new documentation to learn about the latest version of the project and find out how to benefit from all the new cool stuff we have here.

    "},{"location":"blog/2023/12/09/mp-units-210-released/","title":"mp-units 2.1.0 released!","text":"

    A new product version can be obtained from GitHub and Conan.

    The list of the most significant changes introduced by the new version can be found in our Release Notes. We will also describe the most important of them in this post.

    "},{"location":"blog/2023/12/09/mp-units-210-released/#no-more-parenthesis-while-creating-quantities-with-derived-units","title":"No more parenthesis while creating quantities with derived units","text":"

    The V2 design introduced a way to create a quantity by multiplying a raw value and a unit:

    quantity q1 = 42 * m;\n

    However, this meant that when we wanted to create a quantity having a derived unit, we had to put parenthesis around the unit equation or create a custom value of the named unit:

    quantity q2 = 60 * (km / h);\n\nconstexpr auto kmph = km / h;\nquantity q3 = 60 * kmph;\n\nquantity q4 = 50 * (1 / s);\n

    With the new version, we removed this restriction, and now we can type:

    quantity q5 = 60 * km / h;\nquantity q6 = 50 / s;\n

    As a side effect, we introduced a breaking change. We can't use the following definition of hertz anymore:

    inline constexpr struct hertz : named_unit<\"Hz\", 1 / second, kind_of<isq::frequency>> {} hertz;\n

    and have to type either:

    inline constexpr struct hertz : named_unit<\"Hz\", one / second, kind_of<isq::frequency>> {} hertz;\n

    or

    inline constexpr struct hertz : named_unit<\"Hz\", inverse(second), kind_of<isq::frequency>> {} hertz;\n

    To be consistent, we applied the same change to the dimensions and quantity specifications definitions. Now, to define a frequency we have to type:

    C++23C++20Portable
    inline constexpr struct frequency : quantity_spec<inverse(period_duration)> {} frequency;\n
    inline constexpr struct frequency : quantity_spec<frequency, inverse(period_duration)> {} frequency;\n
    QUANTITY_SPEC(frequency, inverse(period_duration));\n
    "},{"location":"blog/2023/12/09/mp-units-210-released/#make_xxx-factory-functions-replaced-with-two-parameter-constructors","title":"make_xxx factory functions replaced with two-parameter constructors","text":"

    In the initial version of the V2 framework, if someone did not like the multiply syntax to create a quantity we provided the make_quantity() factory function. A similar approach was used for quantity_point creation.

    This version removes those (breaking change) and introduces two parameter constructors:

    quantity q(42, si::metre);\nquantity_point qp(q, mean_sea_level);\n

    The above change encourages a better design and results in a terser code.

    "},{"location":"blog/2023/12/09/mp-units-210-released/#improved-definitions-of-becquerel-gray-and-sievert","title":"Improved definitions of becquerel, gray, and sievert","text":"

    In the initial V2 version, we lacked the definitions of the atomic and nuclear physics quantities, which resulted in simplified and unsafe definitions of becquerel, gray, and sievert units. We still do not model most of the quantities from this domain, but we've added the ones that are necessary for the definition of those units.

    Thanks to the above, the following expressions will not compile:

    quantity q1 = 1 * Hz + 1 * Bq;\nquantity<si::sievert> q2 = 42 * Gy;\n
    "},{"location":"blog/2023/12/09/mp-units-210-released/#compatibility-with-other-libraries-redesigned","title":"Compatibility with other libraries redesigned","text":"

    Another significant improvement in this version was redesigning the way we provide compatibility with other similar libraries. The interfaces of quantity_like_traits and quantity_point_like_traits were changed and extended to provide conversion not only from but also to entities from other libraries (breaking change).

    We've also introduced an innovative approach that allows us to specify if such conversions should happen implicitly or if they need to be forced explicitly.

    More on this subject can be found in the Interoperability with Other Libraries chapter.

    "},{"location":"blog/2023/12/09/mp-units-210-released/#point-origins-can-now-be-derived-from-each-other","title":"Point origins can now be derived from each other","text":"

    Previously, each class derived from absolute_point_origin was considered a unique independent point origin. On the other hand, it was OK to derive multiple classes from the same relative_point_origin, and those were specifying the same point in the domain. We found this confusing and limiting. This is why, in this version, the absolute_point_origin uses a CRTP idiom to be able to detect between points that should be considered different from the ones that should be equivalent.

    If we derive from the same instantiation of absolute_point_origin we end up with an equivalent point origin. This change allows us to provide different names for the same temperature points:

    inline constexpr struct absolute_zero : absolute_point_origin<absolute_zero, isq::thermodynamic_temperature> {} absolute_zero;\ninline constexpr struct zeroth_kelvin : decltype(absolute_zero) {} zeroth_kelvin;\n\ninline constexpr struct ice_point : relative_point_origin<absolute_zero + 273.15 * kelvin> {} ice_point;\ninline constexpr struct zeroth_degree_Celsius : decltype(ice_point) {} zeroth_degree_Celsius;\n

    Please note that this is a breaking change as well.

    "},{"location":"blog/2023/12/09/mp-units-210-released/#unit-symbol-text-can-now-be-properly-used-at-runtime","title":"Unit symbol text can now be properly used at runtime","text":"

    The interface of the previous definition of unit_symbol function allowed the use of the returned buffer only at compile-time. This was too limiting as users often want to use unit symbols at runtime (e.g., print them to the console). The new version redesigned the interface of this function (breaking change) to return a buffer that can be properly used at both compilation and runtime:

    std::string_view unit1 = unit_symbol(m / s);\nstd::cout << unit1 << \"\\n\";     // m/s\nstd::string_view unit2 = unit_symbol<{.solidus = unit_symbol_solidus::never}>(m / s);\nstd::cout << unit2 << \"\\n\";     // m s\u207b\u00b9\n
    "},{"location":"blog/2023/11/12/report-from-the-kona-2023-iso-c-committee-meeting/","title":"Report from the Kona 2023 ISO C++ Committee meeting","text":"

    Several groups in the ISO C++ Committee reviewed the P1935: A C++ Approach to Physical Units proposal in Belfast 2019 and Prague 2020. All those groups expressed interest in the potential standardization of such a library and encouraged further work. The authors also got valuable initial feedback that highly influenced the design of the V2 version of the mp-units library.

    In the following years, we scoped on getting more feedback from the production and design. This resulted in version 2 of the mp-units library that resolved many issues the users and Committee members raised. The features and interfaces of this version are close to being the best we can get with the current version of the C++ language standard.

    We submitted three new proposals related to the standardization of the quantities and units library for the last ISO C++ Committee meeting:

    • P2980: A motivation, scope, and plan for a physical quantities and units library,
    • P2981: Improving our safety with a physical quantities and units library,
    • P2982: std::quantity as a numeric type.

    Those were reviewed and briefly discussed in several groups: Numerics (SG6), Safety & Security (SG23), and Library Evolution Working Group (LEWG). Most of the feedback was positive, and the Committee is interested in spending more time on such proposals.

    The following poll was taken by the LEWG:

    LEWG POLL: Given that our time is limited, more time should be promised for a quantities and units library

    Strongly in Favor In favor Neutral Against Strongly Against 10 13 4 0 0

    Attendance: 22 + 6

    Number of Authors: 4

    Authors\u2019 position: 4x SF

    Outcome: Strong consensus in favor

    Additionally, some concerns were raised about the large scope of the proposal. We were encouraged to return with more details and design rationale in a unified paper. This is what we are working on now for the next Committee meeting that will happen in mid-March 2024 in Tokyo.

    "},{"location":"getting_started/faq/","title":"Frequently Asked Questions","text":""},{"location":"getting_started/faq/#why-do-we-spell-metre-instead-of-meter","title":"Why do we spell metre instead of meter?","text":"

    This is how the BIPM defines it in the SI Brochure (British English spelling by default).

    "},{"location":"getting_started/faq/#why-dont-we-use-udls-to-create-quantities","title":"Why don't we use UDLs to create quantities?","text":"

    Many reasons make UDLs a poor choice for a physical units library:

    1. UDLs work only with literals (compile-time known values). Our observation is that besides the unit tests, there are only a few compile-time known quantity values used in the production code. Please note that for physical constants, we recommend using Faster-than-lightspeed Constants.
    2. Typical implementations of UDLs tend to always use the widest representation type available. In the case of std::chrono::duration, the following is true:

      using namespace std::chrono_literals;\nauto d1 = 42s;\nauto d2 = 42.s;\nstatic_assert(std::is_same_v<decltype(d1)::rep, std::int64_t>);\nstatic_assert(std::is_same_v<decltype(d2)::rep, long double>);\n

    When such UDL is intermixed in arithmetics with any quantity type of a shorter representation type, it will always expand it to the longest one. In other words, such long type spreads until all types use it everywhere.

    1. While increasing the coverage for the library, we learned that many unit symbols conflict with built-in types or numeric extensions. A few of those are: F (farad), J (joule), W (watt), K (kelvin), d (day), l or L (litre), erg, ergps. Usage of the _ prefix would make it work for mp-units, but in case the library is standardized, those naming collisions would be a big issue. This is why we came up with the _q_ prefix that would become q_ after standardization (e.g., 42q_s), which is not that nice anymore.

    2. UDLs with the same identifiers defined in different namespace can't be disambiguated in the C++ language. If both SI and CGS systems define _q_s UDL for a second unit, then it would not be possible to specify which one to use in case both namespaces are \"imported\" with using directives.

    3. Another bad property of UDLs is that they do not compose. A coherent unit of angular momentum would have a UDL specified as _q_kg_m2_per_s. Now imagine that we want to make every possible user happy. How many variations of that unit would we predefine for differently scaled versions of all unit ingredients?

    4. UDLs are also really expensive to define and specify. Typically, for each unit, we need two definitions. One for integral and another one for floating-point representation. Before the V2 framework, the coherent unit of angular momentum was defined as:

      constexpr auto operator\"\" _q_kg_m2_per_s(unsigned long long l)\n{\n  gsl_ExpectsAudit(std::in_range<std::int64_t>(l));\n  return angular_momentum<kilogram_metre_sq_per_second, std::int64_t>(static_cast<std::int64_t>(l));\n}\n\nconstexpr auto operator\"\" _q_kg_m2_per_s(long double l)\n{\n  return angular_momentum<kilogram_metre_sq_per_second, long double>(l);\n}\n
    "},{"location":"getting_started/faq/#why-cant-i-create-a-quantity-by-passing-a-number-to-a-constructor","title":"Why can't I create a quantity by passing a number to a constructor?","text":"

    A quantity class template in the mp-units library has no publicly available constructor taking a raw value. Such support is provided by the std::chrono::duration and was pointed out to us as a red flag safety issue by a few parties already.

    Consider the following structure and a code using it:

    struct X {\n  std::vector<std::chrono::milliseconds> vec;\n  // ...\n};\n
    X x;\nx.vec.emplace_back(42);\n

    Everything works fine for years until, at some point, someone changes the structure to:

    struct X {\n  std::vector<std::chrono::microseconds> vec;\n  // ...\n};\n

    The code continues to compile just fine, but all the calculations are off now. This is why we decided to not follow this path.

    In the mp-units library, both a number and a unit have to always be explicitly provided in order to form a quantity.

    Note

    The same applies to the construction of quantity_point using an explicit point origin. To prevent similar safety issues during maintenance, the initialization always requires providing both a quantity and a PointOrigin that we use as a reference point.

    "},{"location":"getting_started/faq/#why-a-dimensionless-quantity-is-not-just-a-fundamental-arithmetic-type","title":"Why a dimensionless quantity is not just a fundamental arithmetic type?","text":"

    In the initial design of this library, the resulting type of division of two quantities was their common representation type:

    static_assert(std::is_same_v<decltype(10 * km / (5 * km)), int>);\n

    First of all, this was consistent with std::chrono::duration behavior. Additional reasoning behind it was not providing a false impression of a strong quantity type for something that looks and feels like a regular number. Also, all of the mathematic and trigonometric functions were working fine out of the box with such representation types, so we did not have to rewrite sin(), cos(), exp(), and others.

    However, the feedback we got from the production usage was that such an approach is really bad for generic programming. It is hard to handle the result of the two quantities' division (or multiplication) as it might be either a quantity or a fundamental type. If we want to raise such a result to some power, we must use units::pow or std::pow depending on the resulting type. Those are only a few issues related to such an approach.

    Moreover, suppose we divide quantities of the same dimension but with units of significantly different magnitudes. In that case, we may end up with a really small or a huge floating-point value, which may result in losing lots of precision. Returning a dimensionless quantity from such cases allows us to benefit from all the properties of scaled units and is consistent with the rest of the library.

    Note

    More information on the current design can be found in the Dimensionless Quantities chapter.

    "},{"location":"getting_started/faq/#why-do-the-identifiers-for-concepts-in-the-library-use-camelcase","title":"Why do the identifiers for concepts in the library use CamelCase?","text":"

    Initially, C++20 was meant to use CamelCase for all the concept identifiers. All the concepts from the std::ranges library were merged with such names into the standard document draft. Frustratingly, CamelCase concepts got dropped from the C++ standard at the last moment before releasing C++20. Now, we are facing the predictable consequences of running out of names.

    As long as some concepts in the library could be easily named with a standard_case there are some that are hard to distinguish from the corresponding type names, such as Quantity, QuantityPoint, QuantitySpec, or Reference. This is why we decided to use CamelCase consistently for all the concept identifiers to make it clear when we are talking about a type or concept identifier.

    However, we are aware that this might be a temporary solution. In case the library gets standardized, we can expect the ISO C++ Committee to bikeshed/rename all of the concept identifiers to a standard_case, even if it will result in a harder to understand code.

    Note

    In case you have a good idea of how to rename existing concepts to the standard_case, please let us know in the associated GitHub Issue.

    "},{"location":"getting_started/faq/#why-unicode-quantity-symbols-are-used-by-default-instead-of-ascii-only-characters","title":"Why Unicode quantity symbols are used by default instead of ASCII-only characters?","text":"

    Both C++ and ISO 80000 are standardized by the ISO. ISO 80000 and the SI standards specify Unicode symbols as the official unit names for some quantities (e.g. \u03a9 symbol for the resistance quantity). As the mp-units library will be proposed for standardization as a part of the C++ Standard Library we have to obey the rules and be consistent with ISO specifications.

    Note

    We do understand engineering reality and the constraints of some environments. This is why the library has the option of ASCII-only Quantity Symbols.

    "},{"location":"getting_started/faq/#why-dont-we-have-cmake-options-to-disable-the-building-of-tests-and-examples","title":"Why don't we have CMake options to disable the building of tests and examples?","text":"

    Over time, many people provided PRs proposing adding options to build tests and examples conditionally. Here are a few examples:

    • Add CMake options for disabling docs, examples and tests
    • build: add options to disable part of the build
    • CMake Refactoring and Option Cleanup

    We admit this is a common practice in the industry, but we also believe this is a bad pattern.

    First, the only need for such options comes when a user wants to use add_subdirectory() in CMake to handle dependencies. Such an approach does not scale and should be discouraged. There is little use for such a practice in times when we have dedicated package managers like Conan.

    The second thing is that our observation is that many people are fixed on disabling \"unneeded\" subdirectories from compilation, but they do not see or address the biggest issue, which is polluting user's build environment with our development-specific settings. Propagating our restrictive compilation flags to user's project is not the best idea as it might cause a lot of harm if this project stops to compile because of that.

    Last but not least, not having those options is on purpose. Top level CMakeLists.txt file should only be used by mp-units developers and contributors as an entry point for the project's development. We want to ensure that everyone will build ALL the code correctly before pushing a commit. Having such options would allow unintended issues to leak to PRs and CI.

    This is why our projects have two entry points:

    • ./CMakeLists.txt is to be used by projects developers to build ALL the project code with really restrictive compilation flags,
    • ./src/CMakeLists.txt contains only a pure library definition and should be used by the customers that prefer to use CMake's add_subdirectory() to handle the dependencies.

    Note

    For more details on this please refer to the CMake + Conan: 3 Years Later - Mateusz Pusz lecture that Mateusz Pusz provided at the C++Now 2021 conference.

    "},{"location":"getting_started/installation_and_usage/","title":"Installation And Usage","text":"

    This chapter provides all the necessary information to obtain and build the code using mp-units. It also describes how to build or distribute the library and generate its documentation.

    "},{"location":"getting_started/installation_and_usage/#cpp-compiler-support","title":"C++ compiler support","text":"

    Info

    mp-units library tries to provide the best user experience possible with the C++ language. To achieve that, it extensively uses C++20 features and the explicit object parameter from C++23.

    Even though the library benefits from C++23 (if available), C++20 is enough to compile and use all of the library's functionality. C++23 features are hidden behind a preprocessor macro providing a backward-compatible way to use it.

    The below table provides the minimum compiler version required to compile the code using the specific feature:

    Feature gcc clang apple-clang MSVC Minimum support 12 16 15 None std::format 13 17 None None C++ modules 14 17 None None C++23 extensions 14 18 None None

    More requirements for C++ modules support can be found in the CMake's documentation.

    "},{"location":"getting_started/installation_and_usage/#modules","title":"Modules","text":"

    The mp-units library provides the following C++ modules.

    flowchart TD\n    mp_units --- mp_units.systems --- mp_units.core
    C++ Module CMake Target Contents mp_units.core mp-units::core Core library framework and systems-independent utilities mp_units.systems mp-units::systems All the systems of quantities and units mp_units mp-units::mp-units Core + Systems

    Note

    C++ modules are provided within the package only when cxx_modules Conan option is set to True.

    "},{"location":"getting_started/installation_and_usage/#repository-structure-and-dependencies","title":"Repository structure and dependencies","text":"

    This repository contains three independent CMake-based projects:

    • ./src

      • header-only project containing whole mp-units library
      • ./src/CMakeList.txt file is intended as an entry point for library users
      • in case this library becomes part of the C++ standard, it will have no external dependencies but until then, it depends on the following:

        • gsl-lite to verify runtime contracts with the gsl_Expects macro,
        • {fmt} to provide text formatting of quantities (if std::format is not supported yet on a specific compiler).
    • .

      • project used as an entry point for library development and CI/CD
      • it wraps ./src project together with usage examples and tests
      • additionally to the dependencies of ./src project, it uses:

        • Catch2 library as a unit tests framework,
        • linear algebra library based on proposal P1385 used in some examples and tests.
    • ./test_package

      • CMake library installation and Conan package verification.

    Important: Library users should not use the top-level CMake file

    Top level CMakeLists.txt file should only be used by mp-units developers and contributors as an entry point for the project's development. We want to ensure that everyone will build ALL the code correctly before pushing a commit. Having such options would allow unintended issues to leak to PRs and CI.

    This is why our projects have two entry points:

    • ./CMakeLists.txt is to be used by projects developers to build ALL the project code with really restrictive compilation flags,
    • ./src/CMakeLists.txt contains only a pure library definition and should be used by the customers that prefer to use CMake's add_subdirectory() to handle the dependencies.

    To learn more about the rationale, please check our FAQ.

    "},{"location":"getting_started/installation_and_usage/#obtaining-dependencies","title":"Obtaining dependencies","text":"

    This library assumes that most of the dependencies will be provided by the Conan Package Manager. If you want to obtain required dependencies by other means, some modifications to the library's CMake files might be needed. The rest of the dependencies responsible for documentation generation are provided by python3-pip.

    "},{"location":"getting_started/installation_and_usage/#conan-quick-intro","title":"Conan quick intro","text":"

    In case you are not familiar with Conan, to install it (or upgrade) just do:

    pip3 install -U conan\n

    After that, you might need to add a custom profile file for your development environment in ~/.conan2/profiles directory. An example profile can look as follows:

    ~/.conan2/profiles/gcc12
    [settings]\narch=x86_64\nbuild_type=Release\ncompiler=gcc\ncompiler.cppstd=20\ncompiler.libcxx=libstdc++11\ncompiler.version=12\nos=Linux\n\n[conf]\ntools.build:compiler_executables={\"c\": \"gcc-12\", \"cpp\": \"g++-12\"}\n

    Setting the language version

    Please note that the mp-units library requires at least C++20 to be set in a Conan profile or forced via the Conan command line. If we do the former, we will not need to provide -s compiler.cppstd=20 every time we run a Conan command line (as provided in the command line instructions below).

    Using Ninja as a CMake generator for Conan

    It is highly recommended to set Ninja as a CMake generator for Conan. To do so, we should create a ~/.conan2/global.conf file that will set tools.cmake.cmaketoolchain:generator to one of the Ninja generators. For example:

    ~/.conan2/global.conf
    tools.cmake.cmaketoolchain:generator=\"Ninja Multi-Config\"\n

    Separate build folders for different configurations

    ~/.conan2/global.conf file may also set tools.cmake.cmake_layout:build_folder_vars which makes working with several compilers or build configurations easier. For example, the below line will force Conan to generate separate CMake presets and folders for each compiler and C++ standard version:

    ~/.conan2/global.conf
    tools.cmake.cmake_layout:build_folder_vars=[\"settings.compiler\", \"settings.compiler.version\", \"settings.compiler.cppstd\"]\n

    In such a case, we will need to use a configuration-specific preset name in the Conan instructions provided below rather than just conan-default and conan-release (e.g. conan-gcc-13-23 and conan-gcc-13-23-release)

    "},{"location":"getting_started/installation_and_usage/#build-options","title":"Build options","text":""},{"location":"getting_started/installation_and_usage/#conan-options","title":"Conan options","text":"cxx_modules

    2.2.0 \u00b7 True/False (Default: False)

    Configures CMake to add C++ modules to the list of default targets.

    use_fmtlib

    2.2.0 \u00b7 True/False (Default: False)

    Forces usage of {fmt} library instead of the C++20 Standard Library features.

    "},{"location":"getting_started/installation_and_usage/#conan-configuration-properties","title":"Conan configuration properties","text":"user.mp-units.build:all

    2.2.0 \u00b7 True/False (Default: False)

    Enables compilation of all the source code, including tests and examples. To support this, it requires some additional Conan build dependencies described in Repository Structure and Dependencies. It also runs unit tests during Conan build (unless tools.build:skip_test configuration property is set to True).

    user.mp-units.build:skip_la

    2.2.0 \u00b7 True/False (Default: False)

    If user.mp-units.build:all is enabled, among others, Conan installs the external wg21-linear_algebra dependency and enables the compilation of linear algebra-based tests and usage examples. Such behavior can be disabled with this option.

    "},{"location":"getting_started/installation_and_usage/#cmake-options","title":"CMake options","text":"MP_UNITS_BUILD_CXX_MODULES

    2.2.0 \u00b7 ON/OFF (Default: OFF)

    Adds C++ modules to the list of default targets.

    MP_UNITS_USE_FMTLIB

    2.2.0 \u00b7 ON/OFF (Default: OFF)

    Forces usage of {fmt} library instead of the C++20 Standard Library features.

    MP_UNITS_BUILD_LA

    2.0.0 \u00b7 ON/OFF (Default: ON)

    Enables building code depending on the linear algebra library.

    MP_UNITS_IWYU

    2.0.0 \u00b7 ON/OFF (Default: OFF)

    Enables include-what-you-use when compiling with a clang compiler. Additionally turns on MP_UNITS_AS_SYSTEM_HEADERS.

    "},{"location":"getting_started/installation_and_usage/#cmake-with-presets-support","title":"CMake with presets support","text":"

    It is recommended to use at least CMake 3.23 to build this project as this version introduced support for CMake Presets schema version 4, used now by Conan to generate presets files. All build instructions below assume that you have such support. If not, your CMake invocations have to be replaced with something like:

    mkdir build && cd build\ncmake .. -G \"Ninja Multi-Config\" -DCMAKE_TOOLCHAIN_FILE=<path_to_generators_dir>/conan_toolchain.cmake\ncmake --build . --config Release\n

    Tip

    In case you can't use CMake 3.23 but you have access to CMake 3.20 or later, you can append -c tools.cmake.cmaketoolchain.presets:max_schema_version=2 to the conan install command which will force Conan to use an older version of the CMake Presets schema.

    "},{"location":"getting_started/installation_and_usage/#installation-and-reuse","title":"Installation and reuse","text":"

    There are many different ways of installing/reusing mp-units in your project. Below we mention only a few of many options possible.

    Important: Prefer using Conan if possible

    The easiest and most recommended way to obtain mp-units is with the Conan package manager. See Conan + CMake (release) for a detailed instruction.

    "},{"location":"getting_started/installation_and_usage/#copy","title":"Copy","text":"

    As mp-units is a C++ header-only library you can simply copy all needed src/*/include subdirectories to your source tree.

    Note

    In such a case, you are on your own to ensure all the dependencies are installed and their header files can be located during the build. Please also note that some compiler-specific flags are needed to make the code compile without issues.

    "},{"location":"getting_started/installation_and_usage/#copy-cmake","title":"Copy + CMake","text":"

    If you copy the whole mp-units repository to your project's file tree, you can reuse CMake targets defined by the library. To do so, you should use CMakeLists.txt file from the ./src directory:

    add_subdirectory(<path_to_units_folder>/src)\n# ...\ntarget_link_libraries(<your_target> <PUBLIC|PRIVATE|INTERFACE> mp-units::mp-units)\n

    Note

    You are still on your own to make sure all the dependencies are installed and their header and CMake configuration files can be located during the build.

    "},{"location":"getting_started/installation_and_usage/#conan-cmake-release","title":"Conan + CMake (release)","text":"

    Tip

    If you are new to the Conan package manager, it is highly recommended to read Obtaining Dependencies and refer to Consuming packages chapter of the official Conan documentation for more information.

    mp-units releases are hosted on Conan-Center. The following steps may be performed to obtain an official library release:

    1. Create Conan configuration file (either conanfile.txt or conanfile.py) in your project's top-level directory and add mp-units as a dependency of your project. For example, the simplest file may look as follows:

      conanfile.txt
      [requires]\nmp-units/2.1.0\n\n[options]\nmp-units:cxx_modules=True\n\n[layout]\ncmake_layout\n\n[generators]\nCMakeToolchain\nCMakeDeps\n
    2. Import mp-units and its dependencies definitions to your project's build procedure with find_package:

      find_package(mp-units REQUIRED)\n
    3. Link your CMake targets with mp-units:

      target_link_libraries(<your_target> <PUBLIC|PRIVATE|INTERFACE> mp-units::mp-units)\n
    4. Download, build, and install Conan dependencies before running the CMake configuration step:

      conan install . -pr <your_conan_profile> -s compiler.cppstd=20 -b=missing\ncmake --preset conan-default\ncmake --build --preset conan-release\n
    "},{"location":"getting_started/installation_and_usage/#conan-cmake-live-at-head","title":"Conan + CMake (Live At Head)","text":"

    This chapter describes the procedure to Live At Head, which means using the latest stable version of mp-units all the time.

    Note

    Please note that even though the Conan packages that you will be using are generated ONLY for builds that are considered stable (passed our CI tests), some minor regressions may happen (our CI and C++20 build environment is not perfect yet). Also, please expect that the library interface might, and probably will, change occasionally. Even though we do our best, such changes might not be reflected in the project's documentation right away.

    The procedure is similar to the one described in Conan + CMake (release) with the following differences:

    1. Before starting the previous procedure, add mp-units remote to your Conan configuration:

      conan remote add conan-mpusz https://mpusz.jfrog.io/artifactory/api/conan/conan-oss\n
    2. In your Conan configuration file, provide the package identifier of the mpusz/testing stream:

      conanfile.txt
      [requires]\nmp-units/2.2.0@mpusz/testing\n\n[options]\nmp-units:cxx_modules=True\n\n[layout]\ncmake_layout\n\n[generators]\nCMakeToolchain\nCMakeDeps\n

      Tip

      The identifiers of the latest packages can always be found in the project's README file or on the project's Artifactory.

    3. Force Conan to check for updated recipes with -u:

      conan install . -pr <your_conan_profile> -s compiler.cppstd=20 -b=missing -u\n
    "},{"location":"getting_started/installation_and_usage/#install","title":"Install","text":"

    In case you don't want to use Conan in your project and just want to install the mp-units library on your file system and use find_package(mp-units) from another repository to find it; it is enough to perform the following steps:

    conan install . -pr <your_conan_profile> -s compiler.cppstd=20 -b=missing\nmv CMakeUserPresets.json src\ncd src\ncmake --preset conan-default -DCMAKE_INSTALL_PREFIX=<your_installation_path>\ncmake --build --preset conan-release --target install\n
    "},{"location":"getting_started/installation_and_usage/#contributing-or-just-building-all-the-tests-and-examples","title":"Contributing (or just building all the tests and examples)","text":"

    In case you would like to build all the mp-units source code (with unit tests and examples), you should:

    1. Use the CMakeLists.txt from the top-level directory.
    2. Run Conan with user.mp-units.build:all = True.
    git clone https://github.com/mpusz/mp-units.git && cd units\nconan build . -pr <your_conan_profile> -s compiler.cppstd=23 -o cxx_modules=True -c user.mp-units.build:all=True -b missing\n

    The above will download and install all of the dependencies needed for the development of the library, build all of the source code, and run unit tests.

    If you prefer to build the project via CMake rather than Conan, then you should replace the conan build with conan install command and then follow with a regular CMake build:

    cmake --preset conan-default\ncmake --build --preset conan-release\ncmake --build --preset conan-release --target all_verify_interface_header_sets\ncmake --build --preset conan-release --target test\n
    "},{"location":"getting_started/installation_and_usage/#building-documentation","title":"Building documentation","text":"

    Starting from mp-units 2.0 we are using Material for MkDocs to build our documentation. The easiest way to install all the required dependencies is with pip:

    pip install -U mkdocs-material mkdocs-rss-plugin\n

    Additionally, a Cairo Graphics library is required by Material for MkDocs. Please follow the official MkDocs documentation to install it.

    After that, you can either:

    • easily start a live server to preview the documentation as you write

      mkdocs serve\n
    • build the documentation

      mkdocs build\n
    "},{"location":"getting_started/installation_and_usage/#packaging","title":"Packaging","text":"

    To test CMake installation and Conan packaging or create a Conan package run:

    conan create . --user <username> --channel <channel> -pr <your_conan_profile> -s compiler.cppstd=20 -o cxx_modules=True -c user.mp-units.build:all=True -b missing\n

    The above will create a Conan package and run tests provided in ./test_package directory.

    "},{"location":"getting_started/installation_and_usage/#uploading-mp-units-package-to-the-conan-server","title":"Uploading mp-units package to the Conan server","text":"
    conan upload -r <remote-name> --all mp-units/2.1.0@<user>/<channel>\n
    "},{"location":"getting_started/introduction/","title":"Introduction","text":"

    mp-units is a Modern C++ library that provides compile-time dimensional analysis and unit/quantity manipulation. The initial versions of the library were inspired by the std::chrono::duration but with each release, the interfaces diverged from the original to provide a better user experience.

    Info

    A brief introduction to the library's interfaces and the rationale for changes in version 2.0 of mp-units were provided in detail by Mateusz Pusz in the \"The Power of C++ Templates With mp-units: Lessons Learned & a New Library Design\" talk at the C++ on Sea 2023 conference.

    "},{"location":"getting_started/introduction/#open-source","title":"Open Source","text":"

    mp-units is Free and Open Source, with a permissive MIT license. Check out the source code and issue tracking (for questions and support, reporting bugs, suggesting feature requests and improvements) at https://github.com/mpusz/mp-units.

    "},{"location":"getting_started/introduction/#with-the-users-experience-in-mind","title":"With the User's Experience in Mind","text":"

    Most of the critical design decisions in the library are dictated by the requirement of providing the best user experience possible. Other C++ physical units libraries are \"famous\" for their enormous and hard-to-understand error messages (one line of the error log often does not fit on one slide). The ultimate goal of mp-units is to improve this and make compile-time errors and debugging as easy and user-friendly as possible.

    To achieve this goal, several techniques are applied:

    • usage of C++20 concepts that improve compile-times and the readability of error messages when compared to the traditional template metaprogramming with SFINAE,
    • usage of strong types for framework entities (instead of type aliases),
    • usage of expression templates to improve the readability of generated types,
    • limiting the number of template arguments to the bare minimum.

    Important: It is all about errors

    In many generic C++ libraries, compile-time errors do not happen often. It is hard to break std::string or std::vector in a way that won't compile with a huge error log. Physical quantities and units libraries are different. Generation of compile-time errors is the main reason to use such a library.

    "},{"location":"getting_started/introduction/#key-features","title":"Key Features","text":"Feature Description Safety - The affine space strong types (quantity and quantity_point)- Compile-time checked conversions of quantities and units- Unique support for many quantities of the same kind- Type-safe equations on scalar, vector, and tensor quantities and their units- Value-preserving conversions Performance - All the compile-time logic implemented as immediate (consteval) functions- As fast or even faster than working with fundamental types- No space size overhead needed to implement high-level abstractions Great User Experience - Optimized for readable compilation errors and great debugging experience- Efficient and composable way to specify a unit of choice- Value-based dimension, unit, and quantity equations Feature Rich - Systems of Quantities- Systems of Units- Scalar, vector, and tensor quantities- The affine space- Different models of the universe (e.g. natural units systems)- Strong dimensionless quantities- Strong angular system- Supports any unit's magnitude (huge, small, floating-point)- Faster-than-lightspeed constants- Highly adjustable text-output formatting Easy to Extend - Each entity can be defined with a single line of code- User can easily extend the systems with custom dimensions, quantities, and units Low Standardization Cost - Small number of predefined entities thanks to their composability- No external dependencies (assuming full C++20 support)- No macros in the user interface (besides portability and standard-compliance issues)- Possibility to be standardized as a freestanding part of the C++ Standard Library"},{"location":"getting_started/look_and_feel/","title":"Look and Feel","text":"

    Here is a small example of operations possible on scalar quantities:

    C++ modulesHeader files
    import mp_units;\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\n// simple numeric operations\nstatic_assert(10 * km / 2 == 5 * km);\n\n// conversions to common units\nstatic_assert(1 * h == 3600 * s);\nstatic_assert(1 * km + 1 * m == 1001 * m);\n\n// derived quantities\nstatic_assert(1 * km / (1 * s) == 1000 * m / s);\nstatic_assert(2 * km / h * (2 * h) == 4 * km);\nstatic_assert(2 * km / (2 * km / h) == 1 * h);\n\nstatic_assert(2 * m * (3 * m) == 6 * m2);\n\nstatic_assert(10 * km / (5 * km) == 2 * one);\n\nstatic_assert(1000 / (1 * s) == 1 * kHz);\n
    #include <mp-units/systems/si/si.h>\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\n// simple numeric operations\nstatic_assert(10 * km / 2 == 5 * km);\n\n// conversions to common units\nstatic_assert(1 * h == 3600 * s);\nstatic_assert(1 * km + 1 * m == 1001 * m);\n\n// derived quantities\nstatic_assert(1 * km / (1 * s) == 1000 * m / s);\nstatic_assert(2 * km / h * (2 * h) == 4 * km);\nstatic_assert(2 * km / (2 * km / h) == 1 * h);\n\nstatic_assert(2 * m * (3 * m) == 6 * m2);\n\nstatic_assert(10 * km / (5 * km) == 2 * one);\n\nstatic_assert(1000 / (1 * s) == 1 * kHz);\n

    Try it on Compiler Explorer

    This library requires some C++20 features (concepts and constraints, classes as NTTP, ...). Thanks to them, a user gets a powerful but still easy-to-use interface where all unit conversions and dimensional analysis can be performed without sacrificing accuracy. Please see the below example for a quick preview of basic library features:

    C++ modulesHeader files
    #include <format>\n#include <iomanip>\n#include <iostream>\n#include <print>\nimport mp_units;\n\nusing namespace mp_units;\n\nconstexpr QuantityOf<isq::speed> auto avg_speed(QuantityOf<isq::length> auto d,\n                                                QuantityOf<isq::time> auto t)\n{\n  return d / t;\n}\n\nint main()\n{\n  using namespace mp_units::si::unit_symbols;\n  using namespace mp_units::international::unit_symbols;\n\n  constexpr quantity v1 = 110 * km / h;\n  constexpr quantity v2 = 70 * mph;\n  constexpr quantity v3 = avg_speed(220. * isq::distance[km], 2 * h);\n  constexpr quantity v4 = avg_speed(isq::distance(140. * mi), 2 * h);\n  constexpr quantity v5 = v3.in(m / s);\n  constexpr quantity v6 = value_cast<m / s>(v4);\n  constexpr quantity v7 = value_cast<int>(v6);\n\n  std::cout << v1 << '\\n';                                        // 110 km/h\n  std::cout << std::setw(10) << std::setfill('*') << v2 << '\\n';  // ***70 mi/h\n  std::cout << std::format(\"{:*^10}\\n\", v3);                      // *110 km/h*\n  std::println(\"{:%N in %U}\", v4);                                // 70 in mi/h\n  std::println(\"{:{%N:.2f}%?%U}\", v5);                            // 30.56 m/s\n  std::println(\"{:{%N:.2f}%?{%U:n}}\", v6);                        // 31.29 m s\u207b\u00b9\n  std::println(\"{:%N}\", v7);                                      // 31\n}\n
    #include <mp-units/format.h>\n#include <mp-units/ostream.h>\n#include <mp-units/systems/international/international.h>\n#include <mp-units/systems/isq/isq.h>\n#include <mp-units/systems/si/si.h>\n#include <format>\n#include <iomanip>\n#include <iostream>\n#include <print>\n\nusing namespace mp_units;\n\nconstexpr QuantityOf<isq::speed> auto avg_speed(QuantityOf<isq::length> auto d,\n                                                QuantityOf<isq::time> auto t)\n{\n  return d / t;\n}\n\nint main()\n{\n  using namespace mp_units::si::unit_symbols;\n  using namespace mp_units::international::unit_symbols;\n\n  constexpr quantity v1 = 110 * km / h;\n  constexpr quantity v2 = 70 * mph;\n  constexpr quantity v3 = avg_speed(220. * isq::distance[km], 2 * h);\n  constexpr quantity v4 = avg_speed(isq::distance(140. * mi), 2 * h);\n  constexpr quantity v5 = v3.in(m / s);\n  constexpr quantity v6 = value_cast<m / s>(v4);\n  constexpr quantity v7 = value_cast<int>(v6);\n\n  std::cout << v1 << '\\n';                                        // 110 km/h\n  std::cout << std::setw(10) << std::setfill('*') << v2 << '\\n';  // ***70 mi/h\n  std::cout << std::format(\"{:*^10}\\n\", v3);                      // *110 km/h*\n  std::println(\"{:%N in %U}\", v4);                                // 70 in mi/h\n  std::println(\"{:{%N:.2f}%?%U}\", v5);                            // 30.56 m/s\n  std::println(\"{:{%N:.2f}%?{%U:n}}\", v6);                        // 31.29 m s\u207b\u00b9\n  std::println(\"{:%N}\", v7);                                      // 31\n}\n

    Try it on Compiler Explorer

    Note

    More code examples can be found in the Examples chapter.

    "},{"location":"getting_started/quick_start/","title":"Quick Start","text":"

    This chapter provides a quick introduction to get you started with mp-units. Much more details can be found in our User's Guide.

    "},{"location":"getting_started/quick_start/#quantities","title":"Quantities","text":"

    A quantity is a concrete amount of a unit representing a quantity type of a specified dimension with a specific representation. It is represented in the library with a quantity class template.

    The SI Brochure says:

    SI Brochure

    The value of the quantity is the product of the number and the unit. The space between the number and the unit is regarded as a multiplication sign (just as a space between units implies multiplication).

    Following the above, the value of a quantity in the mp-units library is created by multiplying a number with a predefined unit:

    C++ modulesHeader files
    import mp_units;\n\nusing namespace mp_units;\n\nquantity q = 42 * si::metre / si::second;\n
    #include <mp-units/systems/si/si.h>\n\nusing namespace mp_units;\n\nquantity q = 42 * si::metre / si::second;\n

    Info

    In case someone doesn't like the multiply syntax or there is an ambiguity between operator* provided by this and other libraries, a quantity can also be created with a two-parameter constructor:

    C++ modulesHeader files
    import mp_units;\n\nusing namespace mp_units;\n\nquantity q{42, si::metre / si::second};\n
    #include <mp-units/systems/si/si.h>\n\nusing namespace mp_units;\n\nquantity q{42, si::metre / si::second};\n

    The above creates an instance of quantity<derived_unit<si::metre, per<si::second>>{}, int>. The same can be obtained using optional unit symbols:

    C++ modulesHeader files
    import mp_units;\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\nquantity q = 42 * m / s;\n
    #include <mp-units/systems/si/si.h>\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\nquantity q = 42 * m / s;\n

    Important

    Unit symbols introduce a lot of short identifiers into the current scope, which may cause naming collisions with unrelated but already existing identifiers in the code base. This is why unit symbols are opt-in.

    A user has several options here to choose from depending on the required scenario and possible naming conflicts:

    • explicitly \"import\" all of them from a dedicated unit_symbols namespace with a using-directive:

      import mp_units;\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;  // imports all the SI symbols at once\n\nquantity q = 42 * m / s;\n
    • selectively select only the required and not-conflicting ones with using-declarations:

      import mp_units;\n\nusing namespace mp_units;\nusing si::unit_symbols::m;\nusing si::unit_symbols::s;\n\nquantity q = 42 * m / s;\n
    • specify a custom not conflicting unit identifier for a unit:

      import mp_units;\n\nusing namespace mp_units;\nconstexpr Unit auto mps = si::metre / si::second;\n\nquantity q = 42 * mps;\n

    Quantities of the same kind can be added, subtracted, and compared to each other:

    C++ modulesHeader files
    import mp_units;\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\nstatic_assert(1 * km + 50 * m == 1050 * m);\n
    #include <mp-units/systems/si/si.h>\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\nstatic_assert(1 * km + 50 * m == 1050 * m);\n

    Various quantities can be multiplied or divided by each other:

    static_assert(140 * km / (2 * h) == 70 * km / h);\n

    Note

    In case you wonder why this library does not use UDLs to create quantities, please check our FAQ.

    "},{"location":"getting_started/quick_start/#quantity-points","title":"Quantity points","text":"

    The quantity point specifies an absolute quantity with respect to an origin. If no origin is provided explicitly, an implicit one will be provided by the library.

    Together with quantities, they model The Affine Space.

    Quantity points should be used in all places where adding two values is meaningless (e.g., temperature points, timestamps, altitudes, readouts from the car's odometer, etc.).

    The set of operations that can be done on quantity points is limited compared to quantities. This introduces an additional type-safety.

    C++ modulesHeader files
    #include <print>\nimport mp_units;\n\nint main()\n{\n  using namespace mp_units;\n  using namespace mp_units::si::unit_symbols;\n  using namespace mp_units::usc::unit_symbols;\n\n  quantity_point temp{20. * deg_C};\n  std::println(\"Temperature: {} ({})\",\n               temp.quantity_from_zero(),\n               temp.in(deg_F).quantity_from_zero());\n}\n
    #include <mp-units/format.h>\n#include <mp-units/systems/si/si.h>\n#include <mp-units/systems/usc/usc.h>\n#include <print>\n\nint main()\n{\n  using namespace mp_units;\n  using namespace mp_units::si::unit_symbols;\n  using namespace mp_units::usc::unit_symbols;\n\n  quantity_point temp{20. * deg_C};\n  std::println(\"Temperature: {} ({})\",\n               temp.quantity_from_zero(),\n               temp.in(deg_F).quantity_from_zero());\n}\n

    The above outputs:

    Temperature: 20 \u00b0C (68 \u00b0F)\n

    Info

    Check The Affine Space chapter to learn more about quantity points.

    "},{"location":"users_guide/terms_and_definitions/","title":"Terms and Definitions","text":"

    The mp-units project consistently uses the official metrology vocabulary defined by the ISO and BIPM. You can find essential project-related definitions in our documentation's \"Glossary\" chapter. Even more, terms are provided in the official metrology vocabulary of the ISO and BIPM.

    Tip

    Please familiarize yourself with terms from \"Glossary\" to better understand the documentation and improve domain-related communication and discussions.

    "},{"location":"users_guide/examples/avg_speed/","title":"avg_speed","text":"

    Try it on Compiler Explorer

    Let's continue the previous example. This time, our purpose will not be to showcase as many library features as possible, but we will scope on different interfaces one can provide with the mp-units. We will also describe some advantages and disadvantages of presented solutions.

    First, we either import a module or include all the necessary header files and import all the identifiers from the mp_units namespace:

    avg_speed.cpp
    #include <exception>\n#include <iostream>\n#ifdef MP_UNITS_MODULES\nimport mp_units;\n#else\n#include <mp-units/ostream.h>\n#include <mp-units/systems/cgs/cgs.h>\n#include <mp-units/systems/international/international.h>\n#include <mp-units/systems/isq/space_and_time.h>\n#include <mp-units/systems/si/unit_symbols.h>\n#endif\n\nnamespace {\n\nusing namespace mp_units;\n

    Next, we define two functions calculating average speed based on quantities of fixed units and integral and floating-point representation types, respectively, and a third function that we introduced in the previous example:

    avg_speed.cpp
    constexpr quantity<si::metre / si::second, int> fixed_int_si_avg_speed(quantity<si::metre, int> d,\n                                                                       quantity<si::second, int> t)\n{\n  return d / t;\n}\n\nconstexpr quantity<si::metre / si::second> fixed_double_si_avg_speed(quantity<si::metre> d, quantity<si::second> t)\n{\n  return d / t;\n}\n\nconstexpr QuantityOf<isq::speed> auto avg_speed(QuantityOf<isq::length> auto d, QuantityOf<isq::time> auto t)\n{\n  return d / t;\n}\n

    We also added a simple utility to print our results:

    avg_speed.cpp
    template<QuantityOf<isq::length> D, QuantityOf<isq::time> T, QuantityOf<isq::speed> V>\nvoid print_result(D distance, T duration, V speed)\n{\n  const auto result_in_kmph = speed.force_in(si::kilo<si::metre> / non_si::hour);\n  std::cout << \"Average speed of a car that makes \" << distance << \" in \" << duration << \" is \" << result_in_kmph\n            << \".\\n\";\n}\n

    Now, let's analyze how those three utility functions behave with different sets of arguments. First, we are going to use quantities of SI units and integral representation:

    avg_speed.cpp
    void example()\n{\n  using namespace mp_units::si::unit_symbols;\n\n  // SI (int)\n  {\n    constexpr auto distance = 220 * km;\n    constexpr auto duration = 2 * h;\n\n    std::cout << \"SI units with 'int' as representation\\n\";\n\n    print_result(distance, duration, fixed_int_si_avg_speed(distance, duration));\n    print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));\n    print_result(distance, duration, avg_speed(distance, duration));\n  }\n

    The above provides the following output:

    SI units with 'int' as representation\nAverage speed of a car that makes 220 km in 2 h is 108 km/h.\nAverage speed of a car that makes 220 km in 2 h is 110 km/h.\nAverage speed of a car that makes 220 km in 2 h is 110 km/h.\n

    Please note that in the first two cases, we must convert length from km to m and time from h to s. The converted values are used to calculate speed in m/s which is then again converted to the one in km/h. Those conversions not only impact the application's runtime performance but may also affect the precision of the final result. Such truncation can be easily observed in the first case where we deal with integral representation types (the resulting speed is 108 km/h).

    The second scenario is really similar to the previous one, but this time, function arguments have floating-point representation types:

    avg_speed.cpp
      // SI (double)\n  {\n    constexpr auto distance = 220. * km;\n    constexpr auto duration = 2. * h;\n\n    std::cout << \"\\nSI units with 'double' as representation\\n\";\n\n    // conversion from a floating-point to an integral type is a truncating one so an explicit cast is needed\n    print_result(distance, duration, fixed_int_si_avg_speed(value_cast<int>(distance), value_cast<int>(duration)));\n    print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));\n    print_result(distance, duration, avg_speed(distance, duration));\n  }\n

    Conversion from floating-point to integral representation types is considered value-truncating and that is why now, in the first case, we need an explicit call to value_cast<int>.

    In the text output, we can observe that, again, the resulting value gets truncated during conversions in the first cast:

    SI units with 'double' as representation\nAverage speed of a car that makes 220 km in 2 h is 108 km/h.\nAverage speed of a car that makes 220 km in 2 h is 110 km/h.\nAverage speed of a car that makes 220 km in 2 h is 110 km/h.\n

    Next, let's do the same for integral and floating-point representations, but this time using international mile:

    avg_speed.cpp
      // International mile (int)\n  {\n    using namespace mp_units::international::unit_symbols;\n\n    constexpr auto distance = 140 * mi;\n    constexpr auto duration = 2 * h;\n\n    std::cout << \"\\nInternational mile with 'int' as representation\\n\";\n\n    // it is not possible to make a lossless conversion of miles to meters on an integral type\n    // (explicit cast needed)\n    print_result(distance, duration, fixed_int_si_avg_speed(distance.force_in(m), duration));\n    print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));\n    print_result(distance, duration, avg_speed(distance, duration));\n  }\n\n  // International mile (double)\n  {\n    using namespace mp_units::international::unit_symbols;\n\n    constexpr auto distance = 140. * mi;\n    constexpr auto duration = 2. * h;\n\n    std::cout << \"\\nInternational mile with 'double' as representation\\n\";\n\n    // conversion from a floating-point to an integral type is a truncating one so an explicit cast is needed\n    // also it is not possible to make a lossless conversion of miles to meters on an integral type\n    // (explicit cast needed)\n    print_result(distance, duration, fixed_int_si_avg_speed(value_cast<m, int>(distance), value_cast<int>(duration)));\n    print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));\n    print_result(distance, duration, avg_speed(distance, duration));\n  }\n

    One important difference here is the fact that as it is not possible to make a lossless conversion of miles to meters on a quantity using an integral representation type, so this time, we need a value_cast<m, int> to force it.

    If we check the text output of the above, we will see the following:

    International mile with 'int' as representation\nAverage speed of a car that makes 140 mi in 2 h is 111 km/h.\nAverage speed of a car that makes 140 mi in 2 h is 112.654 km/h.\nAverage speed of a car that makes 140 mi in 2 h is 112 km/h.\n\nInternational mile with 'double' as representation\nAverage speed of a car that makes 140 mi in 2 h is 111 km/h.\nAverage speed of a car that makes 140 mi in 2 h is 112.654 km/h.\nAverage speed of a car that makes 140 mi in 2 h is 112.654 km/h.\n

    Please note how the first and third results get truncated using integral representation types.

    In the end, we repeat the scenario for CGS units:

    avg_speed.cpp
      {\n    constexpr auto distance = 22'000'000 * cgs::centimetre;\n    constexpr auto duration = 7200 * cgs::second;\n\n    std::cout << \"\\nCGS units with 'int' as representation\\n\";\n\n    // it is not possible to make a lossless conversion of centimeters to meters on an integral type\n    // (explicit cast needed)\n    print_result(distance, duration, fixed_int_si_avg_speed(distance.force_in(m), duration));\n    print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));\n    print_result(distance, duration, avg_speed(distance, duration));\n  }\n\n  // CGS (double)\n  {\n    constexpr auto distance = 22'000'000. * cgs::centimetre;\n    constexpr auto duration = 7200. * cgs::second;\n\n    std::cout << \"\\nCGS units with 'double' as representation\\n\";\n\n    // conversion from a floating-point to an integral type is a truncating one so an explicit cast is needed\n    // it is not possible to make a lossless conversion of centimeters to meters on an integral type\n    // (explicit cast needed)\n    print_result(distance, duration, fixed_int_si_avg_speed(value_cast<m, int>(distance), value_cast<int>(duration)));\n\n    print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));\n    print_result(distance, duration, avg_speed(distance, duration));\n  }\n}\n\n}  // namespace\n

    Again, we observe value_cast being used in the same places and consistent truncation errors in the text output:

    CGS units with 'int' as representation\nAverage speed of a car that makes 22000000 cm in 7200 s is 108 km/h.\nAverage speed of a car that makes 22000000 cm in 7200 s is 110 km/h.\nAverage speed of a car that makes 22000000 cm in 7200 s is 109 km/h.\n\nCGS units with 'double' as representation\nAverage speed of a car that makes 2.2e+07 cm in 7200 s is 108 km/h.\nAverage speed of a car that makes 2.2e+07 cm in 7200 s is 110 km/h.\nAverage speed of a car that makes 2.2e+07 cm in 7200 s is 110 km/h.\n

    The example file ends with a simple main() function:

    avg_speed.cpp
    int main()\n{\n  try {\n    example();\n  } catch (const std::exception& ex) {\n    std::cerr << \"Unhandled std exception caught: \" << ex.what() << '\\n';\n  } catch (...) {\n    std::cerr << \"Unhandled unknown exception caught\\n\";\n  }\n}\n
    ","tags":["CGS System","International System","Text Formatting"]},{"location":"users_guide/examples/hello_units/","title":"hello_units","text":"

    Try it on Compiler Explorer

    This is a really simple example showcasing the features of the mp-units library.

    First, we either import the mp_units module or include the headers for:

    • an International System of Quantities (ISQ)
    • an International System of units (SI)
    • units derived from the International Yard and Pound
    • text formatting and stream output support
    hello_units.cpp
    #include <mp-units/compat_macros.h>\n#include <iomanip>\n#include <iostream>\n#ifdef MP_UNITS_MODULES\nimport mp_units;\n#else\n#include <mp-units/format.h>\n#include <mp-units/ostream.h>\n#include <mp-units/systems/international/international.h>\n#include <mp-units/systems/isq/isq.h>\n#include <mp-units/systems/si/si.h>\n#endif\n

    Also, to shorten the definitions, we \"import\" all the symbols from the mp_units namespace.

    hello_units.cpp
    using namespace mp_units;\n

    Next, we define a simple function that calculates the average speed based on the provided arguments of length and time:

    hello_units.cpp
    constexpr QuantityOf<isq::speed> auto avg_speed(QuantityOf<isq::length> auto d, QuantityOf<isq::time> auto t)\n{\n  return d / t;\n}\n

    The above function template takes any quantities implicitly convertible to isq::length and isq::time, respectively. Those quantities can use any compatible unit and a representation type. The function returns a result of a straightforward equation and ensures that its quantity type is implicitly convertible to isq::speed.

    Tip

    Besides verifying the type returned from the function, constraining a generic return 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.

    hello_units.cpp
    int main()\n{\n  using namespace mp_units::si::unit_symbols;\n  using namespace mp_units::international::unit_symbols;\n

    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.

    hello_units.cpp
      constexpr quantity v1 = 110 * km / h;\n  constexpr quantity v2 = 70 * mph;\n  constexpr quantity v3 = avg_speed(220. * km, 2 * h);\n  constexpr quantity v4 = avg_speed(isq::distance(140. * mi), 2 * isq::duration[h]);\n  constexpr quantity v5 = v3.in(m / s);\n  constexpr quantity v6 = value_cast<m / s>(v4);\n  constexpr quantity v7 = value_cast<int>(v6);\n
    • Lines 21 & 22 create a quantity of kind isq::length / isq::time with the numbers and units provided. Such quantities can be converted or assigned to any other quantity with a matching kind.
    • Line 23 calls our function template with quantities of kind isq::length and isq::time and number and units provided.
    • Line 24 explicitly provides quantity types of the quantities passed to a function template. This time, those will not be quantity kinds anymore and will have more restrictive conversion rules.
    • Line 25 changes the unit of a quantity v3 to m / s in a value-preserving way (floating-point representations are considered to be value-preserving).
    • Line 26 does a similar operation, but this time, it would also succeed for value-truncating cases (if that was the case).
    • Line 27 does a value-truncating conversion of changing the underlying representation type from double to int.
    hello_units.cpp
      std::cout << v1 << '\\n';                                             // 110 km/h\n  std::cout << std::setw(10) << std::setfill('*') << v2 << '\\n';       // ***70 mi/h\n  std::cout << MP_UNITS_STD_FMT::format(\"{:*^10}\\n\", v3);              // *110 km/h*\n  std::cout << MP_UNITS_STD_FMT::format(\"{:%N in %U}\\n\", v4);          // 70 in mi/h\n  std::cout << MP_UNITS_STD_FMT::format(\"{:{%N:.2f}%?%U}\\n\", v5);      // 30.56 in m/s\n  std::cout << MP_UNITS_STD_FMT::format(\"{:{%N:.2f}%?{%U:n}}\\n\", v6);  // 31.29 in m s\u207b\u00b9\n  std::cout << MP_UNITS_STD_FMT::format(\"{:%N}\\n\", v7);                // 31\n}\n

    The above presents various ways to print a quantity. Both stream insertion operations and std::format facilities are supported.

    Tip

    MP_UNITS_STD_FMT is used for compatibility reasons. If a specific compiler does not support std::format or a user prefers to use the {fmt} library, this macro will resolve to fmt namespace. Otherwise, the std namespace will be used.

    ","tags":["International System","Text Formatting"]},{"location":"users_guide/examples/si_constants/","title":"si_constants","text":"

    Try it on Compiler Explorer

    The next example presents all the seven defining constants of the SI system. We can observe how Faster-than-lightspeed Constants work in practice.

    si_constants.cpp
    #include <mp-units/compat_macros.h>\n#include <iostream>\n#ifdef MP_UNITS_MODULES\nimport mp_units;\n#else\n#include <mp-units/format.h>\n#include <mp-units/systems/si/si.h>\n#endif\n\ntemplate<class T>\n  requires mp_units::is_scalar<T>\ninline constexpr bool mp_units::is_vector<T> = true;\n

    As always, we start with the inclusion of all the needed header files. After that, for the simplicity of this example, we hack the character of quantities to be able to express vector quantities with simple scalar types.

    si_constants.cpp
    int main()\n{\n  using namespace mp_units;\n  using namespace mp_units::si;\n  using namespace mp_units::si::unit_symbols;\n\n  std::cout << \"The seven defining constants of the SI and the seven corresponding units they define:\\n\";\n  std::cout << MP_UNITS_STD_FMT::format(\"- hyperfine transition frequency of Cs: {} = {:{%N:.0} %U}\\n\",\n                                        1. * si2019::hyperfine_structure_transition_frequency_of_cs,\n                                        (1. * si2019::hyperfine_structure_transition_frequency_of_cs).in(Hz));\n  std::cout << MP_UNITS_STD_FMT::format(\"- speed of light in vacuum:             {} = {:{%N:.0} %U}\\n\",\n                                        1. * si2019::speed_of_light_in_vacuum,\n                                        (1. * si2019::speed_of_light_in_vacuum).in(m / s));\n  std::cout << MP_UNITS_STD_FMT::format(\"- Planck constant:                      {} = {:{%N:.8e} %U}\\n\",\n                                        1. * si2019::planck_constant, (1. * si2019::planck_constant).in(J * s));\n  std::cout << MP_UNITS_STD_FMT::format(\"- elementary charge:                    {} = {:{%N:.9e} %U}\\n\",\n                                        1. * si2019::elementary_charge, (1. * si2019::elementary_charge).in(C));\n  std::cout << MP_UNITS_STD_FMT::format(\"- Boltzmann constant:                   {} = {:{%N:.6e} %U}\\n\",\n                                        1. * si2019::boltzmann_constant, (1. * si2019::boltzmann_constant).in(J / K));\n  std::cout << MP_UNITS_STD_FMT::format(\"- Avogadro constant:                    {} = {:{%N:.8e} %U}\\n\",\n                                        1. * si2019::avogadro_constant, (1. * si2019::avogadro_constant).in(one / mol));\n  std::cout << MP_UNITS_STD_FMT::format(\"- luminous efficacy:                    {} = {}\\n\",\n                                        1. * si2019::luminous_efficacy, (1. * si2019::luminous_efficacy).in(lm / W));\n}\n

    The main part of the example prints all of the SI-defining constants. While analyzing the output of this program (provided below), we can easily notice that a direct printing of the quantity provides just a value 1 with a proper constant symbol. This is the main power of the Faster-than-lightspeed Constants feature. Only after we explicitly convert the unit of a quantity to proper SI units we get an actual numeric value of the constant.

    The seven defining constants of the SI and the seven corresponding units they define:\n- hyperfine transition frequency of Cs: 1 \u0394\u03bd_Cs = 9192631770 Hz\n- speed of light in vacuum:             1 c = 299792458 m/s\n- Planck constant:                      1 h = 6.62607015e-34 J s\n- elementary charge:                    1 e = 1.602176634e-19 C\n- Boltzmann constant:                   1 k = 1.380649e-23 J/K\n- Avogadro constant:                    1 N_A = 6.02214076e+23 1/mol\n- luminous efficacy:                    1 K_cd = 683 lm/W\n
    ","tags":["Physical Constants","Text Formatting"]},{"location":"users_guide/examples/tags_index/","title":"Tags Index","text":"

    Note

    mp-units usage example applications are meant to be built on all of the supported compilers. This is why they benefit from the Wide Compatibility mode.

    Tip

    All usage examples in this chapter are categorized with appropriate tags to simplify navigation and search of relevant code. You can either read all the examples one-by-one in the order provided by the documentation authors or, thanks to the tagging system, jump straight to the example that is the most interesting for you.

    "},{"location":"users_guide/examples/tags_index/#cgs-system","title":"CGS System","text":"
    • avg_speed
    "},{"location":"users_guide/examples/tags_index/#international-system","title":"International System","text":"
    • avg_speed
    • hello_units
    "},{"location":"users_guide/examples/tags_index/#physical-constants","title":"Physical Constants","text":"
    • si_constants
    "},{"location":"users_guide/examples/tags_index/#text-formatting","title":"Text Formatting","text":"
    • avg_speed
    • hello_units
    • si_constants
    "},{"location":"users_guide/framework_basics/character_of_a_quantity/","title":"Character of a Quantity","text":"

    Warning

    This chapter's features are experimental and subject to change or removal. Please share your feedback if something seems wrong or could be improved.

    "},{"location":"users_guide/framework_basics/character_of_a_quantity/#scalars-vectors-and-tensors","title":"Scalars, vectors, and tensors","text":"

    ISO 80000-2

    Scalars, vectors and tensors are mathematical objects that can be used to denote certain physical quantities and their values. They are as such independent of the particular choice of a coordinate system, whereas each scalar component of a vector or a tensor and each component vector and component tensor depend on that choice.

    Such distinction is important because each quantity character represents different properties and allows different operations to be done on its quantities.

    For example, imagine a physical units library that allows the creation of a \\(speed\\) quantity from both \\(length / time\\) and \\(length * time\\). It wouldn't be too safe to use such a product, right?

    Now we have to realize that both of the above operations (multiplication and division) are not even mathematically defined for linear algebra types such as vectors or tensors. On the other hand, two vectors can be passed as arguments to dot and cross-product operations. The result of the first one is a scalar. The second one results in a vector that is perpendicular to both vectors passed as arguments. Again, it wouldn't be safe to allow replacing those two operations with each other or expect the same results from both cases. This simply can't work.

    "},{"location":"users_guide/framework_basics/character_of_a_quantity/#isq-defines-quantities-of-all-characters","title":"ISQ defines quantities of all characters","text":"

    While defining quantities ISO 80000 explicitly mentions when a specific quantity has a vector or tensor character. Here are some examples:

    Quantity Character Quantity Equation \\(duration\\) scalar {base quantity} \\(mass\\) scalar {base quantity} \\(length\\) scalar {base quantity} \\(path\\; length\\) scalar {base quantity} \\(radius\\) scalar {base quantity} \\(position\\; vector\\) vector {base quantity} \\(velocity\\) vector \\(position\\; vector / duration\\) \\(acceleration\\) vector \\(velocity / duration\\) \\(force\\) vector \\(mass * acceleration\\) \\(power\\) scalar \\(force \\cdot velocity\\) \\(moment\\; of\\; force\\) vector \\(position\\; vector \\times force\\) \\(torque\\) scalar \\(moment\\; of\\; force \\cdot \\{unit\\; vector\\}\\) \\(surface\\; tension\\) scalar \\(\\lvert force \\rvert / length\\) \\(angular\\; displacement\\) scalar \\(path\\; length / radius\\) \\(angular\\; velocity\\) vector \\(angular\\; displacement / duration * \\{unit\\; vector\\}\\) \\(momentum\\) vector \\(mass * velocity\\) \\(angular\\; momentum\\) vector \\(position\\; vector \\times momentum\\) \\(moment\\; of\\; inertia\\) tensor \\(angular\\; momentum \\otimes angular\\; velocity\\)

    In the above equations:

    • \\(a * b\\) - regular multiplication where one of the arguments has to be scalar
    • \\(a / b\\) - regular division where the divisor has to be scalar
    • \\(a \\cdot b\\) - dot product of two vectors
    • \\(a \\times b\\) - cross product of two vectors
    • \\(\\lvert a \\rvert\\) - magnitude of a vector
    • \\(\\{unit\\; vector\\}\\) - a special vector with the magnitude of \\(1\\)
    • \\(a \\otimes b\\) - tensor product of two vectors or tensors

    Note

    As of now, all of the C++ physical units libraries on the market besides mp-units do not support the operations mentioned above. They expose only multiplication and division operators, which do not work for linear algebra-based representation types. If a user of those libraries would like to create the quantities provided in the above table properly, this would result in a compile-time error stating that multiplication and division of two linear algebra vectors is impossible.

    "},{"location":"users_guide/framework_basics/character_of_a_quantity/#characters-dont-apply-to-dimensions-and-units","title":"Characters don't apply to dimensions and units","text":"

    ISO 80000 explicitly states that dimensions are orthogonal to quantity characters:

    ISO 80000-1:2009

    In deriving the dimension of a quantity, no account is taken of its scalar, vector, or tensor character.

    Also, it explicitly states that:

    ISO 80000-2

    All units are scalars.

    "},{"location":"users_guide/framework_basics/character_of_a_quantity/#defining-vector-and-tensor-quantities","title":"Defining vector and tensor quantities","text":"

    To specify that a specific quantity has a vector or tensor character a value of quantity_character enumeration can be appended to the quantity_spec describing such a quantity type:

    C++23C++20Portable
    inline constexpr struct position_vector : quantity_spec<length, quantity_character::vector> {} position_vector;\ninline constexpr struct displacement : quantity_spec<length, quantity_character::vector> {} displacement;\n
    inline constexpr struct position_vector : quantity_spec<position_vector, length, quantity_character::vector> {} position_vector;\ninline constexpr struct displacement : quantity_spec<displacement, length, quantity_character::vector> {} displacement;\n
    QUANTITY_SPEC(position_vector, length, quantity_character::vector);\nQUANTITY_SPEC(displacement, length, quantity_character::vector);\n

    With the above, all the quantities derived from position_vector or displacement will have a correct character determined according to the kind of operations included in the quantity equation defining a derived quantity.

    For example, velocity in the below definition will be defined as a vector quantity (no explicit character override is needed):

    C++23C++20Portable
    inline constexpr struct velocity : quantity_spec<speed, position_vector / duration> {} velocity;\n
    inline constexpr struct velocity : quantity_spec<velocity, speed, position_vector / duration> {} velocity;\n
    QUANTITY_SPEC(velocity, speed, position_vector / duration);\n
    "},{"location":"users_guide/framework_basics/character_of_a_quantity/#representation-types-for-vector-and-tensor-quantities","title":"Representation types for vector and tensor quantities","text":"

    As we remember, the quantity class template is defined as follows:

    template<Reference auto R,\n         RepresentationOf<get_quantity_spec(R).character> Rep = double>\nclass quantity;\n

    The second template parameter is constrained with a RepresentationOf concept that checks if the provided representation type satisfies the requirements for the character associated with this quantity type.

    Note

    The current version of the C++ Standard Library does not provide any types that could be used as a representation type for vector and tensor quantities. This is why users are on their own here .

    To provide examples and implement unit tests, our library uses the types proposed in the P1385 and available as a Conan package in the Conan Center. However, thanks to the provided customization points, any linear algebra library types can be used as a vector or tensor quantity representation type.

    To enable the usage of a user-defined type as a representation type for vector or tensor quantities, we need to provide a partial specialization of is_vector or is_tensor customization points.

    For example, here is how it can be done for the P1385 types:

    #include <matrix>\n\nusing la_vector = STD_LA::fixed_size_column_vector<double, 3>;\n\ntemplate<>\ninline constexpr bool mp_units::is_vector<la_vector> = true;\n

    With the above, we can use la_vector as a representation type for our quantity:

    Quantity auto q = la_vector{1, 2, 3} * isq::velocity[m / s];\n

    In case there is an ambiguity of operator* between mp-units and a linear algebra library, we can either:

    • use two-parameter constructor

      Quantity auto q = quantity{la_vector{1, 2, 3}, isq::velocity[m / s]};\n
    • provide a dedicated overload of operator* that will resolve the ambiguity and wrap the above

      template<Reference R>\nQuantity auto operator*(la_vector rep, R)\n{\n  return quantity{rep, R{}};\n}\n

    Note

    The following does not work:

    Quantity auto q1 = la_vector{1, 2, 3} * m / s;\nQuantity auto q2 = isq::velocity(la_vector{1, 2, 3} * m / s);\nquantity<isq::velocity[m/s]> q3{la_vector{1, 2, 3} * m / s};\n

    In all the cases above, the SI unit m / s has an associated scalar quantity of isq::length / isq::time. la_vector is not a correct representation type for a scalar quantity so the construction fails.

    "},{"location":"users_guide/framework_basics/character_of_a_quantity/#hacking-the-character","title":"Hacking the character","text":"

    Sometimes we want to use a vector quantity, but we don't care about its direction. For example, the standard gravity acceleration constant always points down, so we might not care about this in a particular scenario. In such a case, we may want to \"hack\" the library to allow scalar types to be used as a representation type for scalar quantities.

    For example, we can do the following:

    template<class T>\n  requires mp_units::is_scalar<T>\ninline constexpr bool mp_units::is_vector<T> = true;\n

    which says that every type that can be used as a scalar representation is also allowed for vector quantities.

    Doing the above is actually not such a big \"hack\" as the ISO 80000 explicitly allows it:

    ISO 80000-2

    A vector is a tensor of the first order and a scalar is a tensor of order zero.

    Despite it being allowed by ISO 80000, for type-safety reasons, we do not allow such a behavior by default, and a user has to opt into such scenarios explicitly.

    "},{"location":"users_guide/framework_basics/concepts/","title":"Concepts","text":"

    This chapter enumerates all the user-facing concepts in the mp-units library.

    "},{"location":"users_guide/framework_basics/concepts/#Dimension","title":"Dimension<T>","text":"

    Dimension concept matches a dimension of either a base or derived quantity:

    • Base dimensions are explicitly defined by the user by inheriting from the instantiation of a base_dimension class template. It should be instantiated with a unique symbol identifier describing this dimension in a specific system of quantities.
    • Derived dimensions are implicitly created by the library's framework based on the quantity equation provided in the quantity specification.
    "},{"location":"users_guide/framework_basics/concepts/#DimensionOf","title":"DimensionOf<T, V>","text":"

    DimensionOf concept is satisfied when both arguments satisfy a Dimension concept and when they compare equal.

    "},{"location":"users_guide/framework_basics/concepts/#QuantitySpec","title":"QuantitySpec<T>","text":"

    QuantitySpec concept matches all the quantity specifications including:

    • Base quantities defined by a user by inheriting from the quantity_spec class template instantiated with a base dimension argument.
    • Derived named quantities defined by a user by inheriting from the quantity_spec class template instantiated with a result of a quantity equation passed as an argument.
    • Other named quantities forming a hierarchy of quantities of the same kind defined by a user by inheriting from the quantity_spec class template instantiated with another \"parent\" quantity specification passed as an argument.
    • Quantity kinds describing a family of mutually comparable quantities.
    • Intermediate derived quantity specifications being a result of a quantity equations on other specifications.
    "},{"location":"users_guide/framework_basics/concepts/#QuantitySpecOf","title":"QuantitySpecOf<T, V>","text":"

    QuantitySpecOf concept is satisfied when both arguments satisfy a QuantitySpec concept and when T is implicitly convertible to V.

    More details

    Additionally:

    • T should not be a nested quantity specification of V
    • either T is quantity kind or V should not be a nested quantity specification of T

    Those additional conditions are required to make the following work:

    static_assert(ReferenceOf<si::radian, isq::angular_measure>);\nstatic_assert(!ReferenceOf<si::radian, dimensionless>);\nstatic_assert(!ReferenceOf<isq::angular_measure[si::radian], dimensionless>);\nstatic_assert(ReferenceOf<one, isq::angular_measure>);\nstatic_assert(!ReferenceOf<dimensionless[one], isq::angular_measure>);\n
    "},{"location":"users_guide/framework_basics/concepts/#Unit","title":"Unit<T>","text":"

    Unit concept matches all the units in the library including:

    • Base units defined by a user by inheriting from the named_unit class template instantiated with a unique symbol identifier describing this unit in a specific system of units.
    • Named scaled units defined by a user by inheriting from the named_unit class template instantiated with a unique symbol identifier and a product of multiplying another unit with some magnitude.
    • Prefixed units defined by a user by inheriting from the prefixed_unit class template instantiated with a prefix symbol, a magnitude, and a unit to be prefixed.
    • Derived named units defined by a user by inheriting from the named_unit class template instantiated with a unique symbol identifier and a result of unit equation passed as an argument.
    • Derived unnamed units being a result of a unit equations on other units.

    Note

    In the mp-units library, physical constants are also implemented as units.

    "},{"location":"users_guide/framework_basics/concepts/#AssociatedUnit","title":"AssociatedUnit<T>","text":"

    AssociatedUnit concept describes a unit with an associated quantity and is satisfied by:

    • All units derived from a named_unit class template instantiated with a unique symbol identifier and a QuantitySpec of a quantity kind.
    • All units being a result of unit equations on other associated units.
    Examples

    All units in the SI have associated quantities. For example, si::second is specified to measure isq::time.

    Natural units typically do not have an associated quantity. For example, if we assume c = 1, a natural::second unit can be used to measure both time and length. In such case, speed would have a unit of one.

    "},{"location":"users_guide/framework_basics/concepts/#PrefixableUnit","title":"PrefixableUnit<T>","text":"

    PrefixableUnit concept is satisfied by all units derived from a named_unit class template for which a customization point unit_can_be_prefixed<T{}> was not explicitly set to false. Such units can be passed as an argument to a prefixed_unit class template.

    Examples

    All units in the SI can be prefixed with SI-defined prefixes.

    Some off-system units like non_si::day can't be prefixed. To enforce that, the following has to be provided:

    template<> inline constexpr bool unit_can_be_prefixed<non_si::day> = false;\n
    "},{"location":"users_guide/framework_basics/concepts/#UnitOf","title":"UnitOf<T, V>","text":"

    UnitOf concept is satisfied for all units T matching an AssociatedUnit concept with an associated quantity type implicitly convertible to V.

    More details

    Additionally, the kind of V and the kind of quantity type associated with T must be the same, or the quantity type associated with T may not be derived from the kind of V.

    This condition is required to make dimensionless[si::radian] invalid as si::radian should be only used for isq::angular_measure, which is a nested quantity kind within the dimensionless quantities tree.

    "},{"location":"users_guide/framework_basics/concepts/#UnitCompatibleWith","title":"UnitCompatibleWith<T, V1, V2>","text":"

    UnitCompatibleWith concept is satisfied for all units T when:

    • V1 is a Unit,
    • V2 is a QuantitySpec,
    • T and V1 are defined in terms of the same reference unit,
    • if T is an AssociatedUnit it should satisfy UnitOf<V2>.
    "},{"location":"users_guide/framework_basics/concepts/#Reference","title":"Reference<T>","text":"

    Reference concept is satisfied by all quantity reference types. Such types provide all the meta-information required to create a Quantity. A Reference can either be:

    • An AssociatedUnit.
    • The instantiation of a reference class template with a QuantitySpec passed as the first template argument and a Unit passed as the second one.
    "},{"location":"users_guide/framework_basics/concepts/#ReferenceOf","title":"ReferenceOf<T, V>","text":"

    ReferenceOf concept is satisfied by references T which have a quantity specification that satisfies QuantitySpecOf<V> concept. |

    "},{"location":"users_guide/framework_basics/concepts/#Representation","title":"Representation<T>","text":"

    Representation concept constraints a type of a number that stores the value of a quantity.

    "},{"location":"users_guide/framework_basics/concepts/#RepresentationOf","title":"RepresentationOf<T, Ch>","text":"

    RepresentationOf concept is satisfied by all Representation types that are of a specified quantity character Ch.

    A user can declare a custom representation type to be of a specific character by providing the specialization with true for one or more of the following variable templates:

    • is_scalar<T>
    • is_vector<T>
    • is_tensor<T>
    Tip

    If we want to use scalar types to also express vector quantities (e.g., ignoring the \"direction\" of the vector) the following definition can be provided to enable such a behavior:

    template<class T>\n  requires mp_units::is_scalar<T>\ninline constexpr bool mp_units::is_vector<T> = true;\n
    "},{"location":"users_guide/framework_basics/concepts/#Quantity","title":"Quantity<T>","text":"

    Quantity concept matches every quantity in the library and is satisfied by all types being or deriving from an instantiation of a quantity class template.

    "},{"location":"users_guide/framework_basics/concepts/#QuantityOf","title":"QuantityOf<T, V>","text":"

    QuantityOf concept is satisfied by all the quantities for which a QuantitySpecOf<V> is true.

    "},{"location":"users_guide/framework_basics/concepts/#PointOrigin","title":"PointOrigin<T>","text":"

    PointOrigin concept matches all quantity point origins in the library. It is satisfied by either:

    • All types derived from an absolute_point_origin class template.
    • All types derived from a relative_point_origin class template.
    "},{"location":"users_guide/framework_basics/concepts/#PointOriginFor","title":"PointOriginFor<T, V>","text":"

    PointOriginFor concept is satisfied by all PointOrigin types that have quantity type implicitly convertible from quantity specification V, which means that V must satisfy QuantitySpecOf<T::quantity_spec>.

    Examples

    si::ice_point can serve as a point origin for points of isq::Celsius_temperature because this quantity type implicitly converts to isq::thermodynamic_temperature.

    However, if we define mean_sea_level in the following way:

    inline constexpr struct mean_sea_level : absolute_point_origin<isq::altitude> {} mean_sea_level;\n

    then it can't be used as a point origin for points of isq::length or isq::width as none of them is implicitly convertible to isq::altitude:

    • not every length is an altitude,
    • width is not compatible with altitude.
    "},{"location":"users_guide/framework_basics/concepts/#QuantityPoint","title":"QuantityPoint<T>","text":"

    QuantityPoint concept is satisfied by all types being either a specialization or derived from quantity_point class template.

    "},{"location":"users_guide/framework_basics/concepts/#QuantityPointOf","title":"QuantityPointOf<T, V>","text":"

    QuantityPointOf concept is satisfied by all the quantity points T that match the following value V:

    V Condition QuantitySpec The quantity point quantity specification satisfies QuantitySpecOf<V> concept. PointOrigin The point and V have the same absolute point origin."},{"location":"users_guide/framework_basics/concepts/#QuantityLike","title":"QuantityLike<T>","text":"

    QuantityLike concept provides interoperability with other libraries and is satisfied by a type T for which an instantiation of quantity_like_traits type trait yields a valid type that provides:

    • Static data member reference that matches the Reference concept,
    • rep type that matches RepresentationOf concept with the character provided in reference.
    • to_numerical_value(T) static member function returning a raw value of the quantity packed in either convert_explicitly or convert_implicitly wrapper that enables implicit conversion in the latter case.
    • from_numerical_value(rep) static member function returning T packed in either convert_explicitly or convert_implicitly wrapper that enables implicit conversion in the latter case.
    Examples

    This is how support for std::chrono::seconds can be provided:

    template<>\nstruct mp_units::quantity_like_traits<std::chrono::seconds> {\n  static constexpr auto reference = si::second;\n  using rep = std::chrono::seconds::rep;\n\n  [[nodiscard]] static constexpr convert_implicitly<rep> to_numerical_value(const std::chrono::seconds& q)\n  {\n    return q.count();\n  }\n\n  [[nodiscard]] static constexpr convert_implicitly<std::chrono::seconds> from_numerical_value(const rep& v)\n  {\n    return std::chrono::seconds(v);\n  }\n};\n\nquantity q = 42s;\nstd::chrono::seconds dur = 42 * s;\n
    "},{"location":"users_guide/framework_basics/concepts/#QuantityPointLike","title":"QuantityPointLike<T>","text":"

    QuantityPointLike concept provides interoperability with other libraries and is satisfied by a type T for which an instantiation of quantity_point_like_traits type trait yields a valid type that provides:

    • Static data member reference that matches the Reference concept.
    • Static data member point_origin that matches the PointOrigin concept.
    • rep type that matches RepresentationOf concept with the character provided in reference.
    • to_quantity(T) static member function returning the quantity being the offset of the point from the origin packed in either convert_explicitly or convert_implicitly wrapper that enables implicit conversion in the latter case.
    • from_quantity(quantity<reference, rep>) static member function returning T packed in either convert_explicitly or convert_implicitly wrapper that enables implicit conversion in the latter case.
    Examples

    This is how support for a std::chrono::time_point of std::chrono::seconds can be provided:

    template<typename C>\nstruct mp_units::quantity_point_like_traits<std::chrono::time_point<C, std::chrono::seconds>> {\n  using T = std::chrono::time_point<C, std::chrono::seconds>;\n  static constexpr auto reference = si::second;\n  static constexpr struct point_origin : absolute_point_origin<isq::time> {} point_origin{};\n  using rep = std::chrono::seconds::rep;\n\n  [[nodiscard]] static constexpr convert_implicitly<quantity<reference, rep>> to_quantity(const T& qp)\n  {\n    return quantity{qp.time_since_epoch()};\n  }\n\n  [[nodiscard]] static constexpr convert_implicitly<T> from_quantity(const quantity<reference, rep>& q)\n  {\n    return T(q);\n  }\n};\n\nquantity_point qp = time_point_cast<std::chrono::seconds>(std::chrono::system_clock::now());\nstd::chrono::sys_seconds q = qp + 42 * s;\n
    "},{"location":"users_guide/framework_basics/design_overview/","title":"Design Overview","text":"

    The most important entities in the mp-units library are:

    • quantity,
    • quantity point,
    • unit,
    • dimension,
    • quantity specification
    • quantity reference,
    • quantity representation.

    The graph provided below presents how those and a few other entities depend on each other:

    flowchart TD\n    Unit --- Reference\n    Dimension --- QuantitySpec[\"Quantity specification\"]\n    quantity_character[\"Quantity character\"] --- QuantitySpec\n    QuantitySpec --- Reference[\"Quantity reference\"]\n    Reference --- Quantity\n    quantity_character -.- Representation\n    Representation --- Quantity\n    Quantity --- QuantityPoint[\"Quantity point\"]\n    PointOrigin[\"Point origin\"] --- QuantityPoint\n\n    click Dimension \"#dimension\"\n    click quantity_character \"#quantity-character\"\n    click QuantitySpec \"#quantity-specification\"\n    click Unit \"#unit\"\n    click Reference \"#quantity-reference\"\n    click Representation \"#quantity-representation\"\n    click Quantity \"#quantity\"\n    click PointOrigin \"#point-origin\"\n    click QuantityPoint \"#quantity-point\"
    "},{"location":"users_guide/framework_basics/design_overview/#dimension","title":"Dimension","text":"

    Dimension specifies the dependence of a quantity on the base quantities of a particular system of quantities. It is represented as a product of powers of factors corresponding to the base quantities, omitting any numerical factor.

    In the mp-units library, we use the terms:

    • base dimension to refer to the dimension of a base quantity,
    • derived dimension to refer to the dimension of a derived quantity.

    For example:

    • length (\\(\\mathsf{L}\\)), mass (\\(\\mathsf{M}\\)), time (\\(\\mathsf{T}\\)), electric current (\\(\\mathsf{I}\\)), thermodynamic temperature (\\(\\mathsf{\u0398}\\)), amount of substance (\\(\\mathsf{N}\\)), and luminous intensity (\\(\\mathsf{J}\\)) are the base dimensions of the ISQ.
    • A derived dimension of force in the ISQ is denoted by \\(\\textsf{dim }F = \\mathsf{LMT}^{\u20132}\\).
    • The implementation of IEC 80000 in this library provides iec80000::dim_traffic_intensity base dimension to extend ISQ with strong information technology quantities.

    Base dimensions can be defined by the user in the following way:

    inline constexpr struct dim_length : base_dimension<\"L\"> {} dim_length;\ninline constexpr struct dim_time : base_dimension<\"T\"> {} dim_time;\n

    Derived dimensions are implicitly created by the library's framework based on the quantity equation provided in the quantity specification:

    C++23C++20Portable
    inline constexpr struct length : quantity_spec<dim_length> {} length;\ninline constexpr struct time : quantity_spec<dim_time> {} time;\ninline constexpr struct speed : quantity_spec<length / time> {} speed;\n\nstatic_assert(speed.dimension == dim_length / dim_time);\n
    inline constexpr struct length : quantity_spec<length, dim_length> {} length;\ninline constexpr struct time : quantity_spec<time, dim_time> {} time;\ninline constexpr struct speed : quantity_spec<speed, length / time> {} speed;\n\nstatic_assert(speed.dimension == dim_length / dim_time);\n
    QUANTITY_SPEC(length, dim_length);\nQUANTITY_SPEC(time, dim_time);\nQUANTITY_SPEC(speed, length / time);\n\nstatic_assert(speed.dimension == dim_length / dim_time);\n

    Important

    Users should not explicitly define any derived dimensions. Those should always be implicitly created by the framework.

    The multiplication/division on quantity specifications also multiplies/divides their dimensions:

    static_assert((length / time).dimension == dim_length / dim_time);\n

    The dimension equation of isq::dim_length / isq::dim_time results in the derived_dimension<isq::dim_length, per<isq::dim_time>> type.

    "},{"location":"users_guide/framework_basics/design_overview/#quantity-character","title":"Quantity character","text":"

    ISO 80000 explicitly states that quantities (even of the same kind) may have different characters:

    • scalar,
    • vector,
    • tensor.

    The quantity character in the mp-units library is implemented with the quantity_character enumeration:

    enum class quantity_character { scalar, vector, tensor };\n

    Info

    You can read more on quantity characters in the \"Character of a Quantity\" chapter.

    "},{"location":"users_guide/framework_basics/design_overview/#quantity-specification","title":"Quantity specification","text":"

    Dimension is not enough to describe a quantity. This is why the ISO 80000 provides hundreds of named quantity types. It turns out that there are many more quantity types in the ISQ than the named units in the SI.

    This is why the mp-units library introduces a quantity specification entity that stores:

    • dimension,
    • quantity type/name,
    • quantity character,
    • the quantity equation being the recipe to create this quantity (only for derived quantities that specify such a recipe).

    Note

    We know that it might be sometimes confusing to talk about quantities, quantity types/names, and quantity specifications. However, it might be important to notice here that even the ISO 80000 admits that:

    It is customary to use the same term, \"quantity\", to refer to both general quantities, such as length, mass, etc., and their instances, such as given lengths, given masses, etc. Accordingly, we are used to saying both that length is a quantity and that a given length is a quantity by maintaining the specification \u2013 \"general quantity, \\(Q\\)\" or \"individual quantity, \\(Q_\\textsf{a}\\)\" \u2013 implicit and exploiting the linguistic context to remove the ambiguity.

    In the mp-units library, we have a:

    • quantity - implemented as a quantity class template,
    • quantity specification - implemented with a quantity_spec class template that among others identifies a specific quantity type/name.

    For example:

    • isq::length, isq::mass, isq::time, isq::electric_current, isq::thermodynamic_temperature, isq::amount_of_substance, and isq::luminous_intensity are the specifications of base quantities in the ISQ.
    • isq::width, isq::height, isq::radius, and isq::position_vector are only a few of many quantities of a kind length specified in the ISQ.
    • isq::area, isq::speed, isq::moment_of_force are only a few of many derived quantities provided in the ISQ.

    Quantity specification can be defined by the user in one of the following ways:

    C++23C++20Portable
    inline constexpr struct length : quantity_spec<dim_length> {} length;\ninline constexpr struct height : quantity_spec<length> {} height;\ninline constexpr struct speed : quantity_spec<length / time> {} speed;\n
    inline constexpr struct length : quantity_spec<length, dim_length> {} length;\ninline constexpr struct height : quantity_spec<height, length> {} height;\ninline constexpr struct speed : quantity_spec<speed, length / time> {} speed;\n
    QUANTITY_SPEC(length, dim_length);\nQUANTITY_SPEC(height, length);\nQUANTITY_SPEC(speed, length / time);\n

    The quantity equation of isq::length / isq::time results in the derived_quantity_spec<isq::length, per<isq::time>> type.

    "},{"location":"users_guide/framework_basics/design_overview/#unit","title":"Unit","text":"

    A unit is a concrete amount of a quantity that allows us to measure the values of quantities of the same kind and represent the result as a number being the ratio of the two quantities.

    For example:

    • si::second, si::metre, si::kilogram, si::ampere, si::kelvin, si::mole, and si::candela are the base units of the SI.
    • si::kilo<si::metre> is a prefixed unit of length.
    • si::radian, si::newton, and si::watt are examples of named derived units within the SI.
    • non_si::minute is an example of a scaled unit of time.
    • si::si2019::speed_of_light_in_vacuum is a physical constant standardized by the SI in 2019.

    Note

    In the mp-units library, physical constants are also implemented as units.

    A unit can be defined by the user in one of the following ways:

    template<PrefixableUnit auto U> struct kilo_ : prefixed_unit<\"k\", mag_power<10, 3>, U> {};\ntemplate<PrefixableUnit auto U> inline constexpr kilo_<U> kilo;\n\ninline constexpr struct second : named_unit<\"s\", kind_of<isq::time>> {} second;\ninline constexpr struct minute : named_unit<\"min\", mag<60> * second> {} minute;\ninline constexpr struct gram : named_unit<\"g\", kind_of<isq::mass>> {} gram;\ninline constexpr struct kilogram : decltype(kilo<gram>) {} kilogram;\ninline constexpr struct newton : named_unit<\"N\", kilogram * metre / square(second)> {} newton;\n\ninline constexpr struct speed_of_light_in_vacuum : named_unit<\"c\", mag<299'792'458> * metre / second> {} speed_of_light_in_vacuum;\n

    The unit equation of si::metre / si::second results in the derived_unit<si::metre, per<si::second>> type.

    "},{"location":"users_guide/framework_basics/design_overview/#quantity-reference","title":"Quantity reference","text":"

    ISO defines a quantity as:

    Quote

    property of a phenomenon, body, or substance, where the property has a magnitude that can be expressed as a number and a reference

    After that, it says:

    Quote

    A reference can be a measurement unit, a measurement procedure, a reference material, or a combination of such.

    In the mp-units library, a quantity reference provides all the domain-specific metadata for the quantity besides its numerical value:

    • all the data stored in the quantity specification,
    • unit.

    Together with the value of a representation type, it forms a quantity.

    In the library, we have two different ways to provide a reference:

    • every unit with the associated quantity kind is a valid reference,
    • providing a unit to an indexing operator of a quantity specification explicitly instantiates a reference class template with this quantity spec and a unit passed as arguments.

    Note

    All the units of the SI have associated quantity kinds and may serve as a reference.

    For example:

    • si::metre is defined in the SI as a unit of isq::length and thus can be used as a reference to instantiate a quantity of length (e.g., 42 * m).
    • The expression isq::height[m] results with reference<isq::height, si::metre>, which can be used to instantiate a quantity of isq::height with a unit of si::metre (e.g., 42 * isq::height[m]).
    "},{"location":"users_guide/framework_basics/design_overview/#quantity-representation","title":"Quantity representation","text":"

    Quantity representation defines the type used to store the numerical value of a quantity. Such a type should be of a specific quantity character provided in the quantity specification.

    Note

    By default, all floating-point and integral (besides bool) types are treated as scalars.

    "},{"location":"users_guide/framework_basics/design_overview/#quantity","title":"Quantity","text":"

    ISO defines a quantity as:

    Quote

    property of a phenomenon, body, or substance, where the property has a magnitude that can be expressed as a number and a reference

    This is why a quantity class template is defined in the library as:

    template<Reference auto R,\n         RepresentationOf<get_quantity_spec(R).character> Rep = double>\nclass quantity;\n

    Its value can be easily created by multiplying/dividing the numerical value and a reference.

    For example:

    • All of 42 * m, 42 * si::metre, 42 * isq::height[m], and isq::height(42 * m) create a quantity.
    • A quantity type can also be specified explicitly (e.g., quantity<si::metre, int>, quantity<isq::height[m]>).
    "},{"location":"users_guide/framework_basics/design_overview/#point-origin","title":"Point origin","text":"

    In the affine space theory, the point origin specifies where the \"zero\" of our measurement's scale is.

    In the mp-units library, we have two types of point origins:

    • absolute - defines an absolute \"zero\" for our point,
    • relative - defines an origin that has some \"offset\" relative to an absolute point.

    For example:

    • the absolute point origin can be defined in the following way:
    inline constexpr struct absolute_zero : absolute_point_origin<isq::thermodynamic_temperature> {} absolute_zero;\n
    • the relative point origin can be defined in the following way:
    inline constexpr struct ice_point : relative_point_origin<absolute_zero + 273'150 * milli<kelvin>> {} ice_point;\n
    "},{"location":"users_guide/framework_basics/design_overview/#quantity-point","title":"Quantity point","text":"

    Quantity point implements a point in the affine space theory.

    In the mp-units library, the quantity point is implemented as:

    template<Reference auto R,\n         PointOriginFor<get_quantity_spec(R)> auto PO,\n         RepresentationOf<get_quantity_spec(R).character> Rep = double>\nclass quantity_point;\n

    Its value can be easily created by adding/subtracting the quantity with a point origin.

    For example:

    • The following specifies a quantity point defined in terms of an ice_point provided in the previous example:
    constexpr auto room_reference_temperature = ice_point + isq::Celsius_temperature(21 * deg_C);\n
    "},{"location":"users_guide/framework_basics/dimensionless_quantities/","title":"Dimensionless Quantities","text":"

    The quantities we discussed so far always had some specific type and physical dimension. However, this is not always the case. While performing various computations, we sometimes end up with so-called \"dimensionless\" quantities, which ISO defines as quantities of dimension one:

    ISO/IEC Guide 99

    • Quantity for which all the exponents of the factors corresponding to the base quantities in its quantity dimension are zero.
    • The measurement units and values of quantities of dimension one are numbers, but such quantities convey more information than a number.
    • Some quantities of dimension one are defined as the ratios of two quantities of the same kind.
    • Numbers of entities are quantities of dimension one.
    "},{"location":"users_guide/framework_basics/dimensionless_quantities/#dividing-two-quantities-of-the-same-kind","title":"Dividing two quantities of the same kind","text":"

    Dividing two quantities of the same kind always results in a quantity of dimension one. However, depending on what type of quantities we divide or what their units are, we may end up with slightly different results.

    Note

    In mp-units, dividing two quantities of the same dimension always results in a quantity with the dimension being dimension_one. This is often different for other physical units libraries, which may return a raw representation type for such cases. A raw value is also always returned from the division of two std::chrono::duration objects.

    To read more about the reasoning for this design decision, please check our FAQ.

    "},{"location":"users_guide/framework_basics/dimensionless_quantities/#dividing-quantities-of-the-same-type","title":"Dividing quantities of the same type","text":"

    First, let's analyze what happens if we divide two quantities of the same type:

    constexpr QuantityOf<dimensionless> auto q = isq::height(200 * m) / isq::height(50 * m);\n

    In such a case, we end up with a dimensionless quantity that has the following properties:

    static_assert(q.quantity_spec == dimensionless);\nstatic_assert(q.dimension == dimension_one);\nstatic_assert(q.unit == one);\n

    In case we would like to print its value, we would see a raw value of 4 in the output with no unit being printed.

    "},{"location":"users_guide/framework_basics/dimensionless_quantities/#dividing-quantities-of-different-types","title":"Dividing quantities of different types","text":"

    Now let's see what happens if we divide quantities of the same dimension and unit but which have different quantity types:

    constexpr QuantityOf<dimensionless> auto q = isq::work(200 * J) / isq::heat(50 * J);\n

    Again we end up with dimension_one and one, but this time:

    static_assert(q.quantity_spec == isq::work / isq::heat);\n

    As shown above, the result is not of a dimensionless type anymore. Instead, we get a quantity type derived from the performed quantity equation. According to the ISQ, work divided by heat is the recipe for the thermodynamic efficiency quantity, thus:

    static_assert(implicitly_convertible(q.quantity_spec, isq::efficiency_thermodynamics));\n

    Note

    The quantity of isq::efficiency_thermodynamics is of a kind dimensionless, so it is implicitly convertible to dimensionless and satisfies the QuantityOf<dimensionless> concept.

    "},{"location":"users_guide/framework_basics/dimensionless_quantities/#dividing-quantities-of-different-units","title":"Dividing quantities of different units","text":"

    Now, let's see what happens when we divide two quantities of the same type but different units:

    constexpr QuantityOf<dimensionless> auto q = isq::height(4 * km) / isq::height(2 * m);\n

    This time, we still get a quantity of the dimensionless type with a dimension_one as its dimension. However, the resulting unit is not one anymore:

    static_assert(q.unit == mag_power<10, 3> * one);\n

    In case we would print the text output of this quantity, we would not see a raw value of 2000, but 2 km/m.

    First, it may look surprising, but this is consistent with dividing quantities of different dimensions. For example, if we divide 4 * km / 2 * s, we do not expect km to be \"expanded\" to m before the division, right? We would expect the result of 2 km/s, which is exactly what we get when we divide quantities of the same kind.

    This is a compelling feature that allows us to express huge or tiny ratios without the need for big and expensive representation types. With this, we can easily define things like a Hubble's constant that uses a unit that is proportional to the ratio of kilometers per megaparsecs, which are both units of length:

    inline constexpr struct hubble_constant :\n    named_unit<{u8\"H\u2080\", \"H_0\"}, mag<ratio{701, 10}> * si::kilo<si::metre> / si::second / si::mega<parsec>> {} hubble_constant;\n
    "},{"location":"users_guide/framework_basics/dimensionless_quantities/#counts-of-things","title":"Counts of things","text":"

    Another important use case for dimensionless quantities is to provide strong types for counts of things. For example:

    • ISO-80000-3 provides a rotation quantity defined as the number of revolutions,
    • IEC-80000-6 provides a number of turns in a winding quantity,
    • IEC-80000-13 provides a Hamming distance quantity defined as the number of digit positions in which the corresponding digits of two words of the same length are different.

    Thanks to assigning strong names to such quantities, later on, they can be explicitly used as arguments in the quantity equations of other quantities deriving from them.

    "},{"location":"users_guide/framework_basics/dimensionless_quantities/#predefined-units-of-the-dimensionless-quantity","title":"Predefined units of the dimensionless quantity","text":"

    As we observed above, the most common unit for dimensionless quantities is one. It has the ratio of 1 and does not output any textual symbol.

    Important: one is an identity

    A unit one is special in the entire type system of units as it is considered to be an identity operand in the unit expression templates. This means that, for example:

    static_assert(one * one == one);\nstatic_assert(one * si::metre == si::metre);\nstatic_assert(si::metre / si::metre == one);\n

    The same is also true for dimension_one and dimensionless in the domains of dimensions and quantity specifications.

    Besides the unit one, there are a few other scaled units predefined in the library for usage with dimensionless quantities:

    inline constexpr struct percent : named_unit<\"%\", mag<ratio{1, 100}> * one> {} percent;\ninline constexpr struct per_mille : named_unit<{u8\"\u2030\", \"%o\"}, mag<ratio(1, 1000)> * one> {} per_mille;\ninline constexpr struct parts_per_million : named_unit<\"ppm\", mag<ratio(1, 1'000'000)> * one> {} parts_per_million;\ninline constexpr auto ppm = parts_per_million;\n
    "},{"location":"users_guide/framework_basics/dimensionless_quantities/#angular-quantities","title":"Angular quantities","text":"

    Special, often controversial, examples of dimensionless quantities are an angular measure and solid angular measure quantities that are defined in the ISQ to be the result of a division of \\(arc\\; length / radius\\) and \\(area / radius^2\\) respectively. Moreover, ISQ also explicitly states that both can be expressed in the unit one. This means that both angular measure and solid angular measure should be of a kind dimensionless.

    On the other hand, ISQ also specifies that a unit radian can be used for angular measure, and a unit steradian can be used for solid angular measure. Those should not be mixed or used to express other types of dimensionless quantities. This means that both angular measure and solid angular measure should also be quantity kinds by themselves.

    Note

    Many people claim that angle being a dimensionless quantity is a bad idea. There are proposals submitted to make an angle a base quantity and rad to become a base unit. More on this topic can be found in the \"Strong Angular System\" chapter.

    "},{"location":"users_guide/framework_basics/dimensionless_quantities/#nested-quantity-kinds","title":"Nested quantity kinds","text":"

    Angular quantities are not the only ones with such a \"strange\" behavior. Another but a similar case is a storage capacity quantity specified in IEC-80000-13 that again allows expressing it in both one and bit units.

    Those cases make dimensionless quantities an exceptional tree in the library. This is the only quantity hierarchy that contains more than one quantity kind in its tree:

    flowchart TD\n    dimensionless[\"dimensionless\\n[one]\"]\n    dimensionless --- rotation\n    dimensionless --- efficiency\n    dimensionless --- angular_measure[\"angular_measure\\n[rad]\"]\n    angular_measure --- rotational_displacement\n    angular_measure --- phase_angle\n    dimensionless --- solid_angular_measure[\"solid_angular_measure\\n[sr]\"]\n    dimensionless --- drag_factor\n    dimensionless --- storage_capacity[\"storage_capacity\\n[bit]\"] --- equivalent_binary_storage_capacity\n    dimensionless --- ...

    To provide such support in the library, we provided an is_kind specifier that can be appended to the quantity specification:

    C++23C++20Portable
    inline constexpr struct angular_measure : quantity_spec<dimensionless, arc_length / radius, is_kind> {} angular_measure;\ninline constexpr struct solid_angular_measure : quantity_spec<dimensionless, area / pow<2>(radius), is_kind> {} solid_angular_measure;\ninline constexpr struct storage_capacity : quantity_spec<dimensionless, is_kind> {} storage_capacity;\n
    inline constexpr struct angular_measure : quantity_spec<angular_measure, dimensionless, arc_length / radius, is_kind> {} angular_measure;\ninline constexpr struct solid_angular_measure : quantity_spec<solid_angular_measure, dimensionless, area / pow<2>(radius), is_kind> {} solid_angular_measure;\ninline constexpr struct storage_capacity : quantity_spec<storage_capacity, dimensionless, is_kind> {} storage_capacity;\n
    QUANTITY_SPEC(angular_measure, dimensionless, arc_length / radius, is_kind);\nQUANTITY_SPEC(solid_angular_measure, dimensionless, area / pow<2>(radius), is_kind);\nQUANTITY_SPEC(storage_capacity, dimensionless, is_kind);\n

    With the above, we can constrain radian, steradian, and bit to be allowed for usage with specific quantity kinds only:

    inline constexpr struct radian : named_unit<\"rad\", metre / metre, kind_of<isq::angular_measure>> {} radian;\ninline constexpr struct steradian : named_unit<\"sr\", square(metre) / square(metre), kind_of<isq::solid_angular_measure>> {} steradian;\ninline constexpr struct bit : named_unit<\"bit\", one, kind_of<storage_capacity>> {} bit;\n

    but still allow the usage of one and its scaled versions for such quantities.

    "},{"location":"users_guide/framework_basics/faster_than_lightspeed_constants/","title":"Faster-than-lightspeed Constants","text":"

    In most libraries, physical constants are implemented as constant (possibly constexpr) quantity values. Such an approach has some disadvantages, often affecting the run time performance and causing a loss of precision.

    "},{"location":"users_guide/framework_basics/faster_than_lightspeed_constants/#simplifying-constants-in-an-equation","title":"Simplifying constants in an equation","text":"

    When dealing with equations involving physical constants, they often occur more than once in an expression. Such a constant may appear both in a numerator and denominator of a quantity equation. As we know from fundamental physics, we can simplify such an expression by striking a constant out of the equation. Supporting such behavior allows a faster runtime performance and often a better precision of the resulting value.

    "},{"location":"users_guide/framework_basics/faster_than_lightspeed_constants/#physical-constants-as-units","title":"Physical constants as units","text":"

    The mp-units library allows and encourages the implementation of physical constants as regular units. With that, the constant's value is handled at compile-time, and under favorable circumstances, it can be simplified in the same way as all other repeated units do. If it is not simplified, the value is stored in a type, and the expensive multiplication or division operations can be delayed in time until a user selects a specific unit to represent/print the data.

    Such a feature often also allows using simpler or faster representation types in the equation. For example, instead of always having to multiply a small integral value with a big floating-point constant number, we can just use the integral type all the way. Only in case a constant will not simplify in the equation, and the user will require a specific unit, such a multiplication will be lazily invoked, and the representation type will need to be expanded to facilitate that. With that, addition, subtractions, multiplications, and divisions will always be the fastest - compiled away or done in out-of-order execution.

    To benefit from all of the above, in the mp-units library, SI defining and other constants are implemented as units in the following way:

    namespace si {\n\nnamespace si2019 {\n\ninline constexpr struct speed_of_light_in_vacuum :\n  named_unit<\"c\", mag<299'792'458> * metre / second> {} speed_of_light_in_vacuum;\n\n}  // namespace si2019\n\ninline constexpr struct magnetic_constant :\n  named_unit<{u8\"\u03bc\u2080\", \"u_0\"}, mag<4> * mag_pi * mag_power<10, -7> * henry / metre> {} magnetic_constant;\n\n}  // namespace mp_units::si\n
    "},{"location":"users_guide/framework_basics/faster_than_lightspeed_constants/#usage-examples","title":"Usage examples","text":"

    With the above definitions, we can calculate vacuum permittivity as:

    constexpr auto permeability_of_vacuum = 1. * si::magnetic_constant;\nconstexpr auto speed_of_light_in_vacuum = 1 * si::si2019::speed_of_light_in_vacuum;\n\nQuantityOf<isq::permittivity_of_vacuum> auto q = 1 / (permeability_of_vacuum * pow<2>(speed_of_light_in_vacuum));\n\nstd::println(\"permittivity of vacuum = {} = {:{%N:.3e} %U}\", q, q.in(F / m));\n

    The above first prints the following:

    permittivity of vacuum = 1  \u03bc\u2080\u207b\u00b9 c\u207b\u00b2 = 8.854e-12 F/m\n

    As we can clearly see, all the calculations above were just about multiplying and dividing the number 1 with the rest of the information provided as a compile-time type. Only when a user wants a specific SI unit as a result, the unit ratios are lazily resolved.

    Another similar example can be an equation for total energy:

    QuantityOf<isq::mechanical_energy> auto total_energy(QuantityOf<isq::momentum> auto p,\n                                                     QuantityOf<isq::mass> auto m,\n                                                     QuantityOf<isq::speed> auto c)\n{\n  return isq::mechanical_energy(sqrt(pow<2>(p * c) + pow<2>(m * pow<2>(c))));\n}\n
    constexpr auto GeV = si::giga<si::electronvolt>;\nconstexpr QuantityOf<isq::speed> auto c = 1. * si::si2019::speed_of_light_in_vacuum;\nconstexpr auto c2 = pow<2>(c);\n\nconst auto p1 = isq::momentum(4. * GeV / c);\nconst QuantityOf<isq::mass> auto m1 = 3. * GeV / c2;\nconst auto E = total_energy(p1, m1, c);\n\nstd::cout << \"in `GeV` and `c`:\\n\"\n          << \"p = \" << p1 << \"\\n\"\n          << \"m = \" << m1 << \"\\n\"\n          << \"E = \" << E << \"\\n\";\n\nconst auto p2 = p1.in(GeV / (m / s));\nconst auto m2 = m1.in(GeV / pow<2>(m / s));\nconst auto E2 = total_energy(p2, m2, c).in(GeV);\n\nstd::cout << \"\\nin `GeV`:\\n\"\n          << \"p = \" << p2 << \"\\n\"\n          << \"m = \" << m2 << \"\\n\"\n          << \"E = \" << E2 << \"\\n\";\n\nconst auto p3 = p1.in(kg * m / s);\nconst auto m3 = m1.in(kg);\nconst auto E3 = total_energy(p3, m3, c).in(J);\n\nstd::cout << \"\\nin SI base units:\\n\"\n          << \"p = \" << p3 << \"\\n\"\n          << \"m = \" << m3 << \"\\n\"\n          << \"E = \" << E3 << \"\\n\";\n

    The above prints the following:

    in `GeV` and `c`:\np = 4 GeV/c\nm = 3 GeV/c\u00b2\nE = 5 GeV\n\nin `GeV`:\np = 1.33426e-08 GeV s/m\nm = 3.33795e-17 GeV s\u00b2/m\u00b2\nE = 5 GeV\n\nin SI base units:\np = 2.13771e-18 kg m/s\nm = 5.34799e-27 kg\nE = 8.01088e-10 J\n
    "},{"location":"users_guide/framework_basics/generic_interfaces/","title":"Generic Interfaces","text":"

    Using a concrete unit in the interface often makes a lot of sense. It is especially useful if we store the data internally in the object. In such a case, we have to select a specific unit anyway.

    For example, let's consider a simple storage tank:

    class StorageTank {\n  quantity<horizontal_area[m2]> base_;\n  quantity<isq::height[m]> height_;\n  quantity<isq::mass_density[kg / m3]> density_ = air_density;\npublic:\n  constexpr StorageTank(const quantity<horizontal_area[m2]>& base, const quantity<isq::height[m]>& height) :\n      base_(base), height_(height)\n  {\n  }\n\n  // ...\n};\n

    As the quantities provided in the function's interface are then stored in the class, there is probably no sense in using generic interfaces here.

    "},{"location":"users_guide/framework_basics/generic_interfaces/#the-issues-with-unit-specific-interfaces","title":"The issues with unit-specific interfaces","text":"

    However, in many cases, using a specific unit in the interface is counterproductive. Let's consider the following function:

    quantity<km / h> avg_speed(quantity<km> distance, quantity<h> duration)\n{\n  return distance / duration;\n}\n

    Everything seems fine for now. It also works great if we call it with:

    quantity<km / h> s1 = avg_speed(220 * km, 2 * h);\n

    However, if the user starts doing the following:

    quantity<mi / h> s2 = avg_speed(140 * mi, 2 * h);\nquantity<m / s> s3 = avg_speed(20 * m, 2 * s);\n

    some issues start to be clearly visible:

    1. The arguments must be converted to units mandated by the function's parameters at each call. This involves potentially expensive multiplication/division operations at runtime.
    2. After the function returns the speed in a unit of km/h, another potentially expensive multiplication/division operations must be performed to convert the resulting quantity into a unit being the derived unit of the initial function's arguments.
    3. Besides the obvious runtime cost, some unit conversions may result in a value truncation, which means that the result will not be exactly equal to a direct division of the function's arguments.
    4. We have to use a floating-point representation type (the quantity class template by default uses double as a representation type) which is considered value-preserving. Trying to use an integral type in this scenario will work only for s1, while s2 and s3 will fail to compile. Failing to compile is a good thing here as the library tries to prevent the user from doing a clearly wrong thing. To make the code compile, the user needs to use dedicated value_cast or force_in like this:

      quantity<isq::speed[mi / h]> s2 = avg_speed(value_cast<km>(140 * mi), 2 * h);\nquantity<isq::speed[m / s]> s3 = avg_speed((20 * m).force_in(km), (2 * s).force_in(h));\n

      but the above will obviously provide an incorrect behavior (e.g., division by 0 in the evaluation of s3).

    "},{"location":"users_guide/framework_basics/generic_interfaces/#a-naive-solution","title":"A naive solution","text":"

    A naive solution here would be to implement the function as an unconstrained function template:

    auto avg_speed(auto distance, auto duration)\n{\n  return distance / duration;\n}\n

    Beware, this is not a good solution. The above code is too generic. Such a function template accepts everything:

    • quantities of other types
      • the compiler will not prevent accidental reordering of the function's arguments,
      • quantities of different types can be passed as well,
    • plain double arguments,
    • std::vector and std::lock_guard will be accepted as well (of course, this will fail in the instantiation of a function's body later in the compilation process).

    Note

    The usage of auto instead of a function parameter type is a C++20 feature. It makes such a code a function template where the type of such a parameter will be deduced during the template instantiation process from the argument type passed by the user.

    "},{"location":"users_guide/framework_basics/generic_interfaces/#constraining-function-template-arguments-with-concepts","title":"Constraining function template arguments with concepts","text":"

    Much better generic code can be implemented using basic concepts provided with the library:

    Original template notationThe shorthand notationTerse notation
    template<typename Distance, typename Duration>\n  requires QuantityOf<Distance, isq::length> && QuantityOf<Duration, isq::time>\nauto avg_speed(Distance distance, Duration duration)\n{\n  return isq::speed(distance / duration);\n}\n
    template<QuantityOf<isq::length> Distance, QuantityOf<isq::time> Duration>\nauto avg_speed(Distance distance, Duration duration)\n{\n  return isq::speed(distance / duration);\n}\n
    auto avg_speed(QuantityOf<isq::length> auto distance,\n               QuantityOf<isq::time> auto duration)\n{\n  return isq::speed(distance / duration);\n}\n

    This explicitly states that the arguments passed by the user must not only satisfy a Quantity concept, but also their quantity specification must be implicitly convertible to isq::length and isq::time accordingly. This no longer leaves room for error while still allowing the compiler to generate the most efficient code.

    Tip

    Please note that now it is safe just to use integral types all the way which again improves the runtime performance as the multiplication/division operations are often faster on the integral rather than floating-point types.

    "},{"location":"users_guide/framework_basics/generic_interfaces/#constraining-function-template-return-type","title":"Constraining function template return type","text":"

    The above function template resolves all of the issues described before. However, we can do even better here by additionally constraining the return type:

    QuantityOf<isq::speed> auto avg_speed(QuantityOf<isq::length> auto distance,\n                                      QuantityOf<isq::time> auto duration)\n{\n  return isq::speed(distance / duration);\n}\n

    Doing so has two important benefits:

    1. It informs the users of our interface about what to expect to be the result of a function invocation. It is superior to just returning auto, which does not provide any hint about the thing being returned there.
    2. Such a concept constrains the type returned from the function. This means that it works as a unit test to verify if our function actually performs what it is supposed to do. If there is an error in quantity equations, we will learn about it right away.
    "},{"location":"users_guide/framework_basics/generic_interfaces/#constraining-a-variable-on-the-stack","title":"Constraining a variable on the stack","text":"

    If we know precisely what the function does in its internals and if we know the exact argument types passed to such a function, we often know the exact type that will be returned from its invocation.

    However, if we care about performance, we should often use the generic interfaces described in this chapter. A side effect is that we sometimes are unsure about the return type. Even if we know it today, it might change a week from now due to some code refactoring.

    In such cases, we can again use auto to denote the type:

    auto s1 = avg_speed(220 * km, 2 * h);\nauto s2 = avg_speed(140 * mi, 2 * h);\nauto s3 = avg_speed(20 * m, 2 * s);\n

    or benefit from CTAD:

    quantity s1 = avg_speed(220 * km, 2 * h);\nquantity s2 = avg_speed(140 * mi, 2 * h);\nquantity s3 = avg_speed(20 * m, 2 * s);\n

    In both cases, it is probably OK to do so as the avg_speed function name explicitly provides the information on what to expect as a result.

    In other scenarios where the returned quantity type is not so obvious, it is again helpful to constrain the type with a concept like so:

    QuantityOf<isq::speed> auto s1 = avg_speed(220 * km, 2 * h);\nQuantityOf<isq::speed> auto s2 = avg_speed(140 * mi, 2 * h);\nQuantityOf<isq::speed> auto s3 = avg_speed(20 * m, 2 * s);\n

    The above explicitly provides additional information about the quantity we are dealing with in the code, and it serves as a unit test checking if the \"thing\" returned from a function is actually what we expected here.

    Note

    The QuantityOf and QuantityPointOf concepts are probably the most useful, but there are a few more to play with. A list of all the concepts can be found in the Basic Concepts chapter.

    "},{"location":"users_guide/framework_basics/interface_introduction/","title":"Interface Introduction","text":""},{"location":"users_guide/framework_basics/interface_introduction/#new-style-of-definitions","title":"New style of definitions","text":"

    The mp-units library decided to use a rather unusual pattern to define entities. Here is how we define metre and second SI base units:

    inline constexpr struct metre : named_unit<\"m\", kind_of<isq::length>> {} metre;\ninline constexpr struct second : named_unit<\"s\", kind_of<isq::time>> {} second;\n

    Please note that the above reuses the same identifier for a type and its value. The rationale behind this is that:

    • Users always work with values and never have to spell such a type name.
    • The types appear in the compilation errors and during debugging.

    Important

    To improve compiler errors' readability and make it easier to correlate them with a user's written code, a new idiom in the library is to use the same identifier for a type and its instance.

    "},{"location":"users_guide/framework_basics/interface_introduction/#strong-types-instead-of-aliases","title":"Strong types instead of aliases","text":"

    Let's look again at the above units definitions. Another important point to notice is that all the types describing entities in the library are short, nicely named identifiers that derive from longer, more verbose class template instantiations. This is really important to improve the user experience while debugging the program or analyzing the compilation error.

    Note

    Such a practice is rare in the industry. Some popular C++ physical units libraries generate enormously long error messages where even only the first line failed to fit on a slide with a tiny font.

    "},{"location":"users_guide/framework_basics/interface_introduction/#entities-composability","title":"Entities composability","text":"

    Many physical units libraries (in C++ or any other programming language) assign strong types to library entities (e.g., derived units). While metre_per_second as a type may not look too scary, consider, for example, units of angular momentum. If we followed this path, its coherent unit would look like kilogram_metre_sq_per_second. Now, consider how many scaled versions of this unit you would predefine in the library to ensure that all users are happy with your choice? How expensive would it be from the implementation point of view? What about potential future standardization efforts?

    This is why in mp-units, we put a strong requirement to make everything as composable as possible. For example, to create a quantity with a unit of speed, one may write:

    quantity<si::metre / si::second> q;\n

    In case we use such a unit often and would prefer to have a handy helper for it, we can always do something like this:

    constexpr auto metre_per_second = si::metre / si::second;\nquantity<metre_per_second> q;\n

    or choose any shorter identifier of our choice.

    Coming back to the angular momentum case, thanks to the composability of units, a user can create such a quantity in the following way:

    using namespace mp_units::si::unit_symbols;\nauto q = la_vector{1, 2, 3} * isq::angular_momentum[kg * m2 / s];\n

    It is a much better solution. It is terse and easy to understand. Please also notice how easy it is to obtain any scaled version of such a unit (e.g., mg * square(mm) / min) without having to introduce hundreds of types to predefine them.

    "},{"location":"users_guide/framework_basics/interface_introduction/#value-based-equations","title":"Value-based equations","text":"

    The mp-units library is based on C++20, significantly improving user experience. One of such improvements is the usage of value-based equations.

    As we have learned above, the entities are being used as values in the code, and they compose. Moreover, derived entities can be defined in the library using such value-based equations. This is a huge improvement compared to what we can find in other physical units libraries or what we have to deal with when we want to write some equations for std::ratio.

    For example, below are a few definitions of the SI derived units showing the power of C++20 extensions to Non-Type Template Parameters, which allow us to directly pass a result of the value-based unit equation to a class template definition:

    inline constexpr struct newton : named_unit<\"N\", kilogram * metre / square(second)> {} newton;\ninline constexpr struct pascal : named_unit<\"Pa\", newton / square(metre)> {} pascal;\ninline constexpr struct joule : named_unit<\"J\", newton * metre> {} joule;\n
    "},{"location":"users_guide/framework_basics/interface_introduction/#expression-templates","title":"Expression templates","text":"

    The previous chapter provided a rationale for not having predefined types for derived entities. In many libraries, such an approach results in long and unreadable compilation errors, as framework-generated types are typically far from being easy to read and understand.

    The mp-units library greatly improves the user experience by extensively using expression templates. Such expressions are used consistently throughout the entire library to describe the results of:

    • dimension equation - the result is put into the derived_dimension<> class template
    • quantity equation - the result is put into the derived_quantity_spec<> class template
    • unit equation - the result is put into the derived_unit<> class template

    For example, if we take the above-defined base units and put the results of their division into the quantity class template like this:

    quantity<metre / second> q;\n

    we will observe the following type in the debugger

    (gdb) ptype q\ntype = class mp_units::quantity<mp_units::derived_unit<metre, mp_units::per<second>>(), double> [with Rep = double] {\n

    The same type identifier will be visible in the compilation error (in case it happens).

    Important

    Expressions templates are extensively used throughout the library to improve the readability of the resulting types.

    "},{"location":"users_guide/framework_basics/interface_introduction/#identities","title":"Identities","text":"

    As mentioned above, equations can be performed on dimensions, quantities, and units. Each such domain must introduce an identity object that can be used in the resulting expressions. Here is the list of identities used in the library:

    Domain Concept Identity Dimension dimension_one QuantitySpec dimensionless Unit one

    In the equations, a user can explicitly refer to an identity object. For example:

    constexpr auto my_unit = one / second;\n

    Note

    Another way to achieve the same result is to call an inverse() function:

    constexpr auto my_unit = inverse(second);\n

    Both cases will result in the same expression template being generated and put into the wrapper class template.

    "},{"location":"users_guide/framework_basics/interface_introduction/#supported-operations-and-their-results","title":"Supported operations and their results","text":"

    There are only a few operations that one can do on such entities, and the result of each of them has its unique representation in the library:

    Operation Resulting template expression arguments A * B A, B B * A A, B A * A power<A, 2> {identity} * A A A * {identity} A A / B A, per<B> A / A {identity} A / {identity} A {identity} / A {identity}, per<A> pow<2>(A) power<A, 2> pow<2>({identity}) {identity} sqrt(A) or pow<1, 2>(A) power<A, 1, 2> sqrt({identity}) or pow<1, 2>({identity}) {identity}"},{"location":"users_guide/framework_basics/interface_introduction/#simplifying-the-resulting-expression-templates","title":"Simplifying the resulting expression templates","text":"

    To limit the length and improve the readability of generated types, there are many rules to simplify the resulting expression template.

    1. Ordering

      The resulting comma-separated arguments of multiplication are always sorted according to a specific predicate. This is why:

      static_assert(A * B == B * A);\nstatic_assert(std::is_same_v<decltype(A * B), decltype(B * A)>);\n

      This is probably the most important of all the steps, as it allows comparing types and enables the rest of the simplification rules.

    2. Aggregation

      In case two of the same identifiers are found next to each other on the argument list they will be aggregated in one entry:

      Before After A, A power<A, 2> A, power<A, 2> power<A, 3> power<A, 1, 2>, power<A, 2> power<A, 5, 2> power<A, 1, 2>, power<A, 1, 2> A
    3. Simplification

      In case two of the same identifiers are found in the numerator and denominator argument lists; they are being simplified into one entry:

      Before After A, per<A> {identity} power<A, 2>, per<A> A power<A, 3>, per<A> power<A, 2> A, per<power<A, 2>> {identity}, per<A>
    4. Repacking

      In case an expression uses two results of other operations, the components of its arguments are repacked into one resulting type and simplified there.

      For example, assuming:

      constexpr auto X = A / B;\n

      then:

      Operation Resulting template expression arguments X * B A X * A power<A, 2>, per<B> X * X power<A, 2>, per<power<B, 2>> X / X {identity} X / A {identity}, per<B> X / B A, per<power<B, 2>>
    "},{"location":"users_guide/framework_basics/interface_introduction/#example","title":"Example","text":"

    Thanks to all of the features described above, a user may write the code like this one:

    using namespace mp_units::si::unit_symbols;\nquantity speed = 60. * isq::speed[km / h];\nquantity duration = 8 * s;\nquantity acceleration = speed / duration;\nstd::cout << \"acceleration: \" << acceleration << \" (\" << acceleration.in(m / s2) << \")\\n\";\n

    The acceleration quantity, being the result of the above code, has the following type (after stripping the mp_units namespace for brevity):

    quantity<reference<derived_quantity_spec<isq::speed, per<isq::time>>{}, derived_unit<si::kilo_<si::metre{}>, per<non_si::hour, si::second>>{}>{}, int>\n

    and the text output presents:

    acceleration: 7.5 km h\u207b\u00b9 s\u207b\u00b9 (2.08333 m/s\u00b2)\n
    "},{"location":"users_guide/framework_basics/obtaining_metadata/","title":"Obtaining Metadata","text":""},{"location":"users_guide/framework_basics/obtaining_metadata/#quantity-spec","title":"quantity spec","text":""},{"location":"users_guide/framework_basics/obtaining_metadata/#unit","title":"unit","text":""},{"location":"users_guide/framework_basics/obtaining_metadata/#reference","title":"reference","text":""},{"location":"users_guide/framework_basics/obtaining_metadata/#quantity","title":"quantity","text":""},{"location":"users_guide/framework_basics/quantity_arithmetics/","title":"Quantity Arithmetics","text":""},{"location":"users_guide/framework_basics/quantity_arithmetics/#quantity-is-a-numeric-wrapper","title":"quantity is a numeric wrapper","text":"

    If we think about it, the quantity class template is just a \"smart\" numeric wrapper. It exposes properly constrained set of arithmetic operations on one or two operands.

    Important: quantity propagates the underlying interface

    Every single arithmetic operator is exposed by the quantity class template only if the underlying representation type provides it as well, and when its implementation has proper semantics (e.g., returns a reasonable type).

    For example, in the following code, -a will compile only if MyInt exposes such an operation as well:

    quantity a = MyInt{42} * m;\nquantity b = -a;\n

    Assuming that:

    • q is our quantity,
    • qi is a quantity implicitly convertible to q,
    • qk is a quantity of the same kind as q,
    • q1 is a quantity of dimension_one with the unit one,
    • qq is any other quantity,
    • number is a value of a type \"compatible\" with q's representation type,

    here is the list of all the supported operators:

    • unary:
      • +q
      • -q
      • ++q
      • q++
      • --q
      • q--
    • compound assignment:
      • q += qi
      • q -= qi
      • q %= qi
      • q *= number
      • q *= q1
      • q /= number
      • q /= q1
    • binary:
      • q + qk
      • q - qk
      • q % qk
      • q * qq
      • q * number
      • number * q
      • q / qq
      • q / number
      • number / q
    • ordering and comparison:
      • q == qk
      • q <=> qk

    As we can see, there are plenty of operations one can do on a value of a quantity type. As most of them are obvious, in the following chapters, we will discuss only the most important or non-trivial aspects of quantity arithmetics.

    "},{"location":"users_guide/framework_basics/quantity_arithmetics/#addition-and-subtraction","title":"Addition and subtraction","text":"

    Quantities can easily be added or subtracted from each other:

    static_assert(1 * m + 1 * m == 2 * m);\nstatic_assert(2 * m - 1 * m == 1 * m);\nstatic_assert(isq::height(1 * m) + isq::height(1 * m) == isq::height(2 * m));\nstatic_assert(isq::height(2 * m) - isq::height(1 * m) == isq::height(1 * m));\n

    The above uses the same types for LHS, RHS, and the result, but in general, we can add, subtract, or compare the values of any quantity type as long as both quantities are of the same kind. The result of such an operation will be the common type of the arguments:

    static_assert(1 * km + 1.5 * m == 1001.5 * m);\nstatic_assert(isq::height(1 * m) + isq::width(1 * m) == isq::length(2 * m));\nstatic_assert(isq::height(2 * m) - isq::distance(0.5 * m) == 1.5 * m);\nstatic_assert(isq::radius(1 * m) - 0.5 * m == isq::radius(0.5 * m));\n

    Note

    Please note that for the compound assignment operators, both arguments have to either be of the same type or the RHS has to be implicitly convertible to the LHS, as the type of LHS is always the result of such an operation:

    static_assert((1 * m += 1 * km) == 1001 * m);\nstatic_assert((isq::height(1.5 * m) -= 1 * m) == isq::height(0.5 * m));\n

    If we break those rules, the following code will not compile:

    static_assert((1 * m -= 0.5 * m) == 0.5 * m);                       // Compile-time error(1)\nstatic_assert((1 * km += 1 * m) == 1001 * m);                       // Compile-time error(2)\nstatic_assert((isq::height(1 * m) += isq::length(1 * m)) == 2 * m); // Compile-time error(3)\n
    1. The floating-point to integral representation type is considered narrowing.
    2. Conversion of quantity with integral representation type from a unit of a higher resolution to the one with a lower resolution is considered narrowing.
    3. Conversion from a more generic quantity type to a more specific one is considered unsafe.
    "},{"location":"users_guide/framework_basics/quantity_arithmetics/#multiplication-and-division","title":"Multiplication and division","text":"

    Multiplying or dividing a quantity by a number does not change its quantity type or unit. However, its representation type may change. For example:

    static_assert(isq::height(3 * m) * 0.5 == isq::height(1.5 * m));\n

    Note

    Unless we use a compound assignment operator, in which case truncating operations are again not allowed:

    static_assert((isq::height(3 * m) *= 0.5) == isq::height(1.5 * m)); // Compile-time error(1)\n
    1. The floating-point to integral representation type is considered narrowing.

    However, suppose we multiply or divide quantities of the same or different types or we divide a raw number by a quantity. In that case, we most probably will end up in a quantity of yet another type:

    static_assert(120 * km / (2 * h) == 60 * km / h);\nstatic_assert(isq::width(2 * m) * isq::length(2 * m) == isq::area(4 * m2));\nstatic_assert(50 / isq::time(1 * s) == isq::frequency(50 * Hz));\n

    Note

    An exception from the above rule happens when one of the arguments is a dimensionless quantity. If we multiply or divide by such a quantity, the quantity type will not change. If such a quantity has a unit one, also the unit of a quantity will not change:

    static_assert(120 * m / (2 * one) == 60 * m);\n

    An interesting special case happens when we divide the same quantity kinds or multiply a quantity by its inverted type. In such a case, we end up with a dimensionless quantity.

    static_assert(isq::height(4 * m) / isq::width(2 * m) == 2 * one); // (1)!\nstatic_assert(5 * h / (120 * min) == 0 * one);  // (2)!\nstatic_assert(5. * h / (120 * min) == 2.5 * one);\n
    1. The resulting quantity type of the LHS is isq::height / isq::width, which is a quantity of the dimensionless kind.
    2. The resulting quantity of the LHS is 0 * dimensionless[h / min]. To be consistent with the division of different quantity types, we do not convert quantity values to a common unit before the division.

    Important: Beware of integral division

    The physical units library can't do any runtime branching logic for the division operator. All logic must be done at compile-time when the actual values are unknown, and the quantity types can't change at runtime.

    If we expect 120 * km / (2 * h) to return 60 km / h, we have to agree with the fact that 5 * km / (24 * h) returns 0 km/h. We can't do a range check at runtime to dynamically adjust scales and types based on the values of provided function arguments.

    This is why we often prefer floating-point representation types when dealing with units. Some popular physical units libraries even forbid integer division at all.

    "},{"location":"users_guide/framework_basics/quantity_arithmetics/#modulo","title":"Modulo","text":"

    Now that we know how addition, subtraction, multiplication, and division work, it is time to discuss modulo. What would we expect to be returned from the following quantity equation?

    auto q = 5 * h % (120 * min);\n

    Most of us would probably expect to see 1 h or 60 min as a result. And this is where the problems start.

    C++ language defines its / and % operators with the quotient-remainder theorem:

    q = a / b;\nr = a % b;\nq * b + r == a;\n

    The important property of the modulo operation is that it only works for integral representation types (it is undefined what modulo for floating-point types means). However, as we saw in the previous chapter, integral types are tricky because they often truncate the value.

    From the quotient-remainder theorem, the result of modulo operation is r = a - q * b. Let's see what we get from such a quantity equation on integral representation types:

    const quantity a = 5 * h;\nconst quantity b = 120 * min;\nconst quantity q = a / b;\nconst quantity r = a - q * b;\n\nstd::cout << \"reminder: \" << r << \"\\n\";\n

    The above code outputs:

    reminder: 5 h\n

    And now, a tough question needs an answer. Do we really want modulo operation on physical units to be consistent with the quotient-remainder theorem and return 5 h for 5 * h % (120 * min)?

    This is exactly why we decided not to follow this hugely surprising path in the mp-units library. The selected approach was also consistent with the feedback from the C++ experts. For example, this is what Richard Smith said about this issue:

    Richard Smith

    I think the quotient-remainder property is a less important motivation here than other factors -- the constraints on % and / are quite different, so they lack the inherent connection they have for integers. In particular, I would expect that A / B works for all quantities A and B, whereas A % B is only meaningful when A and B have the same dimension. It seems like a nice-to-have for the property to apply in the case where both / and % are defined, but internal consistency of / across all cases seems much more important to me.

    I would expect 61 min % 1 h to be 1 min, and 1 h % 59 min to also be 1 min, so my intuition tells me that the result type of A % B, where A and B have the same dimension, should have the smaller unit of A and B (and if the smaller one doesn't divide the larger one, we should either use the gcd / std::common_type of the units of A and B or perhaps just produce an error). I think any other behavior for % is hard to defend.

    On the other hand, for division it seems to me that the choice of unit should probably not affect the result, and so if we want that 5 mm / 120 min = 0 mm/min, then 5 h / 120 min == 0 hc (where hc is a dimensionless \"hexaconta\", or 60x, unit). I don't like the idea of taking SI base units into account; that seems arbitrary and like it would do the wrong thing as often as it does the right thing, especially when the units have a multiplier that is very large or small. We could special-case the situation of a dimensionless quantity, but that could lead to problematic overflow pretty easily: a calculation such as 10 s * 5 GHz * 2 uW would overflow an int if it produces a dimensionless quantity for 10 s * 5 GHz, but it could equally produce 50 G * 2 uW = 100 kW without any overflow, and presumably would if the terms were merely reordered.

    If people want to use integer-valued quantities, I think it's fundamental that you need to know what the units of the result of an operation will be, and take that into account in how you express computations; the simplest rule for heterogeneous operators like * or / seems to be that the units of the result are determined by applying the operator to the units of the operands -- and for homogeneous operators like + or %, it seems like the only reasonable option is that you get the std::common_type of the units of the operands.

    To summarize, the modulo operation on physical units has more in common with addition and division operators than with the quotient-remainder theorem. To avoid surprising results, the operation uses a common unit to do the calculation and provide its result:

    static_assert(5 * h / (120 * min) == 0 * one);\nstatic_assert(5 * h % (120 * min) == 60 * min);\nstatic_assert(61 * min % (1 * h) == 1 * min);\nstatic_assert(1 * h % (59 * min) == 1 * min);\n
    "},{"location":"users_guide/framework_basics/quantity_arithmetics/#comparison-against-zero","title":"Comparison against zero","text":"

    In our code, we often want to compare the value of a quantity against zero. For example, we do it every time we want to ensure that we deal with a non-zero or positive value.

    We could implement such checks in the following way:

    if (q1 / q2 != 0 * m / s)\n  // ...\n

    The above would work (assuming we are dealing with the quantity of speed) but could be suboptimal if the result of q1 / q2 is not expressed in m / s. To eliminate the need for conversion, we need to write:

    if (auto q = q1 / q2; q != q.zero())\n  // ...\n

    but that is a bit inconvenient, and inexperienced users could be unaware of this technique and its reasons.

    For the above reasons, the library provides dedicated interfaces to compare against zero that follow the naming convention of named comparison functions in the C++ Standard Library. The mp-units/compare.h header file exposes the following functions:

    • is_eq_zero
    • is_neq_zero
    • is_lt_zero
    • is_gt_zero
    • is_lteq_zero
    • is_gteq_zero

    Thanks to them, to save typing and not pay for unneeded conversions, our check could be implemented as follows:

    if (is_neq_zero(q1 / q2))\n  // ...\n

    Tip

    Those functions will work with any type T that exposes zero() member function returning something comparable to T. Thanks to that, we can use them not only with quantities but also with std::chrono::duration or any other type that exposes such an interface.

    "},{"location":"users_guide/framework_basics/quantity_arithmetics/#other-maths","title":"Other maths","text":"

    This chapter scopes only on the quantity type's operators. However, there are many named math functions taking quantities as arguments. Those can be found in the mp-units/math.h header file. Among others, we can find there the following:

    • pow(), sqrt(), cbrt(),
    • exp(),
    • abs(),
    • epsilon(),
    • fma(), fmod(),
    • isfinite(), isinf(), isnan(),
    • floor(), ceil(), round(),
    • inverse(),
    • hypot(),
    • sin(), cos(), tan(),
    • asin(), acos(), atan(), atan2().

    In the library, we can also find mp-units/random.h header file with all the pseudo-random number generators working on quantity types.

    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/","title":"Simple and Typed Quantities","text":"

    ISO defines a quantity as:

    Quote

    property of a phenomenon, body, or substance, where the property has a magnitude that can be expressed as a number and a reference

    After that, it says:

    Quote

    A reference can be a measurement unit, a measurement procedure, a reference material, or a combination of such.

    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/#quantity-class-template","title":"quantity class template","text":"

    In the mp-units library, a quantity is represented with the following class template:

    template<Reference auto R,\n         RepresentationOf<get_quantity_spec(R).character> Rep = double>\nclass quantity;\n

    The concept Reference is satisfied by a type that provides all the domain-specific metadata describing a quantity (besides the representation type and its value). Such a type can be either:

    • a unit with an associated quantity type (e.g., si::metre, m / s),
    • a reference type explicitly specifying the quantity type and its unit.

    Important

    All units in the SI system have an associated quantity type.

    A reference type is implicitly created as a result of the following expression:

    constexpr auto ref = isq::length[m];\n

    The above example results in the following type reference<isq::length(), si::metre()> being instantiated.

    As we have two alternative options that satisfy the Reference concept in the mp-units library, we also have two modes of dealing with quantities.

    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/#simple-quantities","title":"Simple quantities","text":"

    The simple mode might be preferred by many developers. It is all about units. Quantities using this mode have shorter type identifiers, resulting in easier-to-understand error messages and better debugging experience.

    Here is a simple example showing how to deal with such quantities:

    C++ modulesHeader files
    #include <print>\nimport mp_units;\n\nusing namespace mp_units;\n\nconstexpr quantity<si::metre / si::second> avg_speed(quantity<si::metre> dist,\n                                                     quantity<si::second> time)\n{\n  return dist / time;\n}\n\nint main()\n{\n  using namespace mp_units::si::unit_symbols;\n\n  const quantity distance = 110 * km;\n  const quantity duration = 2 * h;\n  const quantity speed = avg_speed(distance, duration);\n\n  std::println(\"A car driving {} in {} has an average speed of {:{%N:.4} %U} ({:{%N:.4} %U})\",\n               distance, duration, speed, speed.in(km / h));\n}\n
    #include <mp-units/format.h>\n#include <mp-units/systems/si/si.h>\n#include <print>\n\nusing namespace mp_units;\n\nconstexpr quantity<si::metre / si::second> avg_speed(quantity<si::metre> dist,\n                                                     quantity<si::second> time)\n{\n  return dist / time;\n}\n\nint main()\n{\n  using namespace mp_units::si::unit_symbols;\n\n  const quantity distance = 110 * km;\n  const quantity duration = 2 * h;\n  const quantity speed = avg_speed(distance, duration);\n\n  std::println(\"A car driving {} in {} has an average speed of {:{%N:.4} %U} ({:{%N:.4} %U})\",\n               distance, duration, speed, speed.in(km / h));\n}\n

    The code above prints:

    A car driving 110 km in 2 h has an average speed of 15.28 m/s (55 km/h)\n

    Try it on Compiler Explorer

    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/#user-provided-unit-wrappers","title":"User-provided unit wrappers","text":"

    Sometimes it might be awkward to type some derived units:

    quantity speed = 60 * km / h;\n

    In case such a unit is used a lot in the project, a user can easily provide a nicely named wrapper for it with:

    constexpr auto kmph = km / h;\nquantity speed = 60 * kmph;\n
    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/#easy-to-understand-compilation-error-messages","title":"Easy-to-understand compilation error messages","text":"

    In case a user makes an error in a quantity equation and the result of the calculation will not match the function return type, the compiler will detect such an issue at compile-time.

    For example, in case we will make the following error:

    constexpr quantity<si::metre / si::second> avg_speed(quantity<si::metre> dist,\n                                                     quantity<si::second> time)\n{\n  return dist * time;  // (1)!\n}\n
    1. Quantities multiplied (instead of divided) by accident.

    the following compilation error message will be provided:

    error: no viable conversion from returned value of type\n       'quantity<mp_units::derived_unit<mp_units::si::metre, mp_units::si::second>{{{}}}, [...]>'\n       to function return type\n       'quantity<mp_units::derived_unit<mp_units::si::metre, mp_units::per<mp_units::si::second>>{{{}}}, [...]>'\n   10 |   return dist * time;\n      |          ^~~~~~~~~~~\n
    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/#typed-quantities","title":"Typed quantities","text":"

    Simple mode is all about and just about units. In case we care about a specific quantity type, typed quantities should be preferred. With this mode, for example, we can specify if we deal with width, height, or radius and ensure we will not assign one to another by accident.

    The previous example can be re-typed using typed quantities in the following way:

    C++ modulesHeader files
    #include <print>\nimport mp_units;\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\nconstexpr quantity<isq::speed[m / s]> avg_speed(quantity<isq::length[m]> dist,\n                                                quantity<isq::time[s]> time)\n{\n  return dist / time;\n}\n\nint main()\n{\n  const quantity distance = isq::distance(110 * km);\n  const quantity duration = isq::time(2 * h);\n  const quantity speed = avg_speed(distance, duration);\n\n  std::println(\"A car driving {} in {} has an average speed of {:{%N:.4} %U} ({:{%N:.4} %U})\",\n               distance, duration, speed, speed.in(km / h));\n}\n
    #include <mp-units/format.h>\n#include <mp-units/systems/isq/isq.h>\n#include <mp-units/systems/si/si.h>\n#include <print>\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\nconstexpr quantity<isq::speed[m / s]> avg_speed(quantity<isq::length[m]> dist,\n                                                quantity<isq::time[s]> time)\n{\n  return dist / time;\n}\n\nint main()\n{\n  const quantity distance = isq::distance(110 * km);\n  const quantity duration = isq::time(2 * h);\n  const quantity speed = avg_speed(distance, duration);\n\n  std::println(\"A car driving {} in {} has an average speed of {:{%N:.4} %U} ({:{%N:.4} %U})\",\n               distance, duration, speed, speed.in(km / h));\n}\n
    A car driving 110 km in 2 h has an average speed of 15.28 m/s (55 km/h)\n

    Try it on Compiler Explorer

    In case we will accidentally make the same calculation error as before, this time, we will get a bit longer error message, this time also containing information about the quantity type:

    error: no viable conversion from returned value of type\n       'quantity<reference<get_quantity_spec(metre{}) * struct time{{{}}}, metre{} * second{{}}>{}, [...]>'\n       to function return type\n       'quantity<reference<speed{}, derived_unit<metre, per<second>>{}>{}, [...]>'\n   12 |   return dist * time;\n      |          ^~~~~~~~~~~\n

    As we can see above, the compilation error is longer but still relatively easy to understand.

    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/#additional-type-safety-with-typed-quantities","title":"Additional type safety with typed quantities","text":"

    Based on the previous example, it might seem that typed quantities are not that useful, more to type and provide harder-to-understand error messages. It might be true in some cases, but there are scenarios where they offer additional level of safety.

    Let's see another example:

    C++ modulesHeader files SimpleTyped
    #include <numbers>\nimport mp_units;\n\nusing namespace mp_units;\n\nclass StorageTank {\n  quantity<square(si::metre)> base_;\n  quantity<si::metre> height_;\npublic:\n  constexpr StorageTank(const quantity<square(si::metre)>& base,\n                        const quantity<si::metre>& height) :\n    base_(base), height_(height)\n  {\n  }\n\n  // ...\n};\n\nclass CylindricalStorageTank : public StorageTank {\npublic:\n  constexpr CylindricalStorageTank(const quantity<si::metre>& radius,\n                                   const quantity<si::metre>& height) :\n    StorageTank(std::numbers::pi * pow<2>(radius), height)\n  {\n  }\n};\n\nclass RectangularStorageTank : public StorageTank {\npublic:\n  constexpr RectangularStorageTank(const quantity<si::metre>& length,\n                                   const quantity<si::metre>& width,\n                                   const quantity<si::metre>& height) :\n    StorageTank(length * width, height)\n  {\n  }\n};\n\nint main()\n{\n  using namespace mp_units::si::unit_symbols;\n  auto tank = RectangularStorageTank(1'000 * mm, 500 * mm, 200 * mm);\n  // ...\n}\n
    #include <numbers>\nimport mp_units;\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\n// add a custom quantity type of kind isq::length\ninline constexpr struct horizontal_length\n    : quantity_spec<isq::length> {} horizontal_length;\n\n// add a custom derived quantity type of kind isq::area\n// with a constrained quantity equation\ninline constexpr struct horizontal_area\n    : quantity_spec<isq::area, horizontal_length * isq::width> {} horizontal_area;\n\nclass StorageTank {\n  quantity<horizontal_area[m2]> base_;\n  quantity<isq::height[m]> height_;\npublic:\n  constexpr StorageTank(const quantity<horizontal_area[m2]>& base,\n                        const quantity<isq::height[m]>& height) :\n    base_(base), height_(height)\n  {\n  }\n\n  // ...\n};\n\nclass CylindricalStorageTank : public StorageTank {\npublic:\n  constexpr CylindricalStorageTank(const quantity<isq::radius[m]>& radius,\n                                   const quantity<isq::height[m]>& height) :\n    StorageTank(quantity_cast<horizontal_area>(std::numbers::pi * pow<2>(radius)),\n                height)\n  {\n  }\n};\n\nclass RectangularStorageTank : public StorageTank {\npublic:\n  constexpr RectangularStorageTank(const quantity<horizontal_length[m]>& length,\n                                   const quantity<isq::width[m]>& width,\n                                   const quantity<isq::height[m]>& height) :\n    StorageTank(length * width, height)\n  {\n  }\n};\n\nint main()\n{\n  auto tank = RectangularStorageTank(horizontal_length(1'000 * mm),\n                                     isq::width(500 * mm),\n                                     isq::height(200 * mm));\n  // ...\n}\n
    SimpleTyped
    #include <mp-units/math.h>\n#include <mp-units/systems/si/si.h>\n#include <numbers>\n\nusing namespace mp_units;\n\nclass StorageTank {\n  quantity<square(si::metre)> base_;\n  quantity<si::metre> height_;\npublic:\n  constexpr StorageTank(const quantity<square(si::metre)>& base,\n                        const quantity<si::metre>& height) :\n    base_(base), height_(height)\n  {\n  }\n\n  // ...\n};\n\nclass CylindricalStorageTank : public StorageTank {\npublic:\n  constexpr CylindricalStorageTank(const quantity<si::metre>& radius,\n                                   const quantity<si::metre>& height) :\n    StorageTank(std::numbers::pi * pow<2>(radius), height)\n  {\n  }\n};\n\nclass RectangularStorageTank : public StorageTank {\npublic:\n  constexpr RectangularStorageTank(const quantity<si::metre>& length,\n                                   const quantity<si::metre>& width,\n                                   const quantity<si::metre>& height) :\n    StorageTank(length * width, height)\n  {\n  }\n};\n\nint main()\n{\n  using namespace mp_units::si::unit_symbols;\n  auto tank = RectangularStorageTank(1'000 * mm, 500 * mm, 200 * mm);\n  // ...\n}\n
    #include <mp-units/math.h>\n#include <mp-units/systems/isq/isq.h>\n#include <mp-units/systems/si/si.h>\n#include <numbers>\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\n// add a custom quantity type of kind isq::length\ninline constexpr struct horizontal_length\n    : quantity_spec<isq::length> {} horizontal_length;\n\n// add a custom derived quantity type of kind isq::area\n// with a constrained quantity equation\ninline constexpr struct horizontal_area\n    : quantity_spec<isq::area, horizontal_length * isq::width> {} horizontal_area;\n\nclass StorageTank {\n  quantity<horizontal_area[m2]> base_;\n  quantity<isq::height[m]> height_;\npublic:\n  constexpr StorageTank(const quantity<horizontal_area[m2]>& base,\n                        const quantity<isq::height[m]>& height) :\n    base_(base), height_(height)\n  {\n  }\n\n  // ...\n};\n\nclass CylindricalStorageTank : public StorageTank {\npublic:\n  constexpr CylindricalStorageTank(const quantity<isq::radius[m]>& radius,\n                                   const quantity<isq::height[m]>& height) :\n    StorageTank(quantity_cast<horizontal_area>(std::numbers::pi * pow<2>(radius)),\n                height)\n  {\n  }\n};\n\nclass RectangularStorageTank : public StorageTank {\npublic:\n  constexpr RectangularStorageTank(const quantity<horizontal_length[m]>& length,\n                                   const quantity<isq::width[m]>& width,\n                                   const quantity<isq::height[m]>& height) :\n    StorageTank(length * width, height)\n  {\n  }\n};\n\nint main()\n{\n  auto tank = RectangularStorageTank(horizontal_length(1'000 * mm),\n                                     isq::width(500 * mm),\n                                     isq::height(200 * mm));\n  // ...\n}\n

    In the above example, the highlighted call doesn't look that safe anymore in the case of simple quantities, right? Suppose someone, either by mistake or due to some refactoring, will call the function with an invalid order of arguments. In that case, the program will compile fine but not work as expected.

    Let's see what will happen if we reorder the arguments in the case of typed quantities:

    auto tank = RectangularStorageTank(horizontal_length(1'000 * mm),\n                                   isq::height(200 * mm),\n                                   isq::width(500 * mm));\n

    This time, a compiler provides the following compilation error:

    <source>:53:15: error: no matching constructor for initialization of 'RectangularStorageTank'\n   53 |   auto tank = RectangularStorageTank(horizontal_length(1'000 * mm),\n      |               ^                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n   54 |                                      isq::height(200 * mm),\n      |                                      ~~~~~~~~~~~~~~~~~~~~~~\n   55 |                                      isq::width(500 * mm));\n      |                                      ~~~~~~~~~~~~~~~~~~~~\n<source>:43:13: note: candidate constructor not viable: no known conversion from\n                'quantity<mp_units::reference<mp_units::isq::height{{{{{}}}}},\n                                              mp_units::si::milli_<mp_units::si::metre{{}}>{{{{}}}}>{}, int>' to\n                'const quantity<reference<width{}, metre{}>{}, (default) double>' for 2nd argument\n   43 |   constexpr RectangularStorageTank(const quantity<horizontal_length[m]>& length,\n      |             ^\n   44 |                                    const quantity<isq::width[m]>& width,\n      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n

    What about derived quantities? In the above example, you probably noticed that we also defined a custom horizontal_area quantity of kind isq::area. This quantity has the unique property of being implicitly constructible only from the result of the multiplication of quantities of horizontal_area and isq::width or the ones that implicitly convert to them.

    Based on the above error message, we already know that a quantity of isq::height is not implicitly constructible to the quantity of isq::width. This property is transitively passed to derived quantities using them. If by accident, we will try to create a StorageTank base class in the following way:

    class RectangularStorageTank : public StorageTank {\npublic:\n  constexpr RectangularStorageTank(const quantity<horizontal_length[m]>& length,\n                                   const quantity<isq::width[m]>& width,\n                                   const quantity<isq::height[m]>& height) :\n    StorageTank(length * height, height)\n  {\n  }\n};\n

    we will again get a compilation error message like this one:

    error: no matching constructor for initialization of 'StorageTank'\n   46 |     StorageTank(length * height, height)\n      |     ^           ~~~~~~~~~~~~~~~~~~~~~~~\n<source>:22:13: note: candidate constructor not viable: no known conversion from\n                'quantity<mp_units::reference<mp_units::derived_quantity_spec<horizontal_length, mp_units::isq::height>{{}, {{}}},\n                                              mp_units::derived_unit<mp_units::power<mp_units::si::metre, 2>>{{{}}}>{}, [...]>' to\n                'const quantity<reference<horizontal_area{}, derived_unit<power<metre, 2>>{}>{}, [...]>' for 1st argument\n   22 |   constexpr StorageTank(const quantity<horizontal_area[m2]>& base,\n      |             ^           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n

    Tip

    If you need to use various quantities of the same kind, consider using typed quantities to bring an additional level of safety to your project.

    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/#quantity_cast-to-force-unsafe-conversions","title":"quantity_cast() to force unsafe conversions","text":"

    Did you notice the quantity_cast() usage in the other child class?

    class CylindricalStorageTank : public StorageTank {\npublic:\n  constexpr CylindricalStorageTank(const quantity<isq::radius[m]>& radius,\n                                   const quantity<isq::height[m]>& height) :\n    StorageTank(quantity_cast<horizontal_area>(std::numbers::pi * pow<2>(radius)),\n                height)\n  {\n  }\n};\n

    As isq::radius is not convertible to horizontal_length, the derived quantity of pow<2>(radius) can't be converted to horizontal_area as well. It would be unsafe to allow such a conversion as not all of the circles lie flat on the ground, right?

    In such a case, the user has to explicitly force such an unsafe conversion with the help of a quantity_cast(). This function name is easy to spot in code reviews or while searching the project for problems if something goes sideways. In case of unexpected quantities-related issues, this should be the first function to look for.

    Tip

    Do not overuse quantity_cast(). Use it only when necessary and ensure that the requested conversion is exactly what you need in this case.

    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/#which-mode-should-i-use-in-my-project","title":"Which mode should I use in my project?","text":"

    We have good news for you if you wonder which mode you should choose for your project. Simple and typed quantity modes can be freely mixed with each other. When you use different quantities of the same kind (e.g., radius, wavelength, altitude, ...), you should probably reach for typed quantities to bring additional safety for those cases. Otherwise, just use simple mode for the remaining quantities. The mp-units library will do its best to protect your project based on the information provided.

    Tip

    You can easily mix simple and typed quantities in your project.

    "},{"location":"users_guide/framework_basics/systems_of_quantities/","title":"Systems of Quantities","text":"

    The physical units libraries on the market typically only scope on modeling one or more systems of units. However, this is not the only system kind to model. Another, and maybe even more important, system kind is a system of quantities.

    Info

    Please note that the mp-units is probably the first library on the Open Source market (in any programming language) that models the ISQ with all its definitions provided in ISO 80000. Please provide feedback if something looks odd or could be improved.

    "},{"location":"users_guide/framework_basics/systems_of_quantities/#dimension-is-not-enough-to-describe-a-quantity","title":"Dimension is not enough to describe a quantity","text":"

    Most of the products on the market are aware of physical dimensions. However, a dimension is not enough to describe a quantity. For example, let's see the following implementation:

    class Box {\n  area base_;\n  length height_;\npublic:\n  Box(length l, length w, length h) : base_(l * w), height_(h) {}\n  // ...\n};\n\nBox my_box(2 * m, 3 * m, 1 * m);\n

    How do you like such an interface? It turns out that in most existing strongly-typed libraries this is often the best we can do

    Another typical question many users ask is how to deal with work and torque. Both of those have the same dimension but are different quantities.

    A similar issue is related to figuring out what should be the result of:

    auto res = 1 * Hz + 1 * Bq + 1 * Bd;\n

    where:

    • Hz (hertz) - unit of frequency
    • Bq (becquerel) - unit of activity
    • Bd (baud) - unit of modulation rate

    All of those quantities have the same dimension, namely \\(\\mathsf{T}^{-1}\\), but probably it is not wise to allow adding, subtracting, or comparing them, as they describe vastly different physical properties.

    If the above example seems too abstract, let's consider a fuel consumption (fuel volume divided by distance, e.g., 6.7 l/km) and an area. Again, both have the same dimension \\(\\mathsf{L}^{2}\\), but probably it wouldn't be wise to allow adding, subtracting, or comparing a fuel consumption of a car and the area of a football field. Such an operation does not have any physical sense and should fail to compile.

    Important

    More than one quantity may be defined for the same dimension:

    • quantities of different kinds (e.g. frequency, modulation rate, activity, ...)
    • quantities of the same kind (e.g. length, width, altitude, distance, radius, wavelength, position vector, ...)

    It turns out that the above issues can't be solved correctly without proper modeling of a system of quantities.

    "},{"location":"users_guide/framework_basics/systems_of_quantities/#quantities-of-the-same-kind","title":"Quantities of the same kind","text":"

    ISO 80000-1

    • Quantities may be grouped together into categories of quantities that are mutually comparable
    • Mutually comparable quantities are called quantities of the same kind
    • Two or more quantities cannot be added or subtracted unless they belong to the same category of mutually comparable quantities
    • Quantities of the same kind within a given system of quantities have the same quantity dimension
    • Quantities of the same dimension are not necessarily of the same kind

    The above quotes from ISO 80000 provide answers to all the issues above. Two quantities can't be added, subtracted, or compared unless they belong to the same kind. As frequency, activity, and modulation rate are different kinds, the expression provided above should not compile.

    "},{"location":"users_guide/framework_basics/systems_of_quantities/#system-of-quantities-is-not-only-about-kinds","title":"System of quantities is not only about kinds","text":"

    ISO 80000 specify hundreds of different quantities. There are plenty of different kinds provided and often each kind contains more than one quantity. In fact, it turns out that such quantities form a hierarchy of quantities of the same kind.

    For example, here are all quantities of the kind length provided in the ISO 80000:

    flowchart TD\n    length --- width[width, breadth]\n    length --- height[height, depth, altitude]\n    width --- thickness\n    width --- diameter\n    width --- radius\n    length --- path_length\n    path_length --- distance\n    distance --- radial_distance\n    length --- wavelength\n    length --- position_vector[\"position_vector\\n{vector}\"]\n    length --- displacement[\"displacement\\n{vector}\"]\n    radius --- radius_of_curvature

    Each of the above quantities expresses some kind of length, and each can be measured with si::metre. However, each of them has different properties, usage, and sometimes even requires a different representation type (notice that position_vector and displacement are vector quantities).

    Such a hierarchy helps us in defining arithmetics and conversion rules for various quantities of the same kind.

    "},{"location":"users_guide/framework_basics/systems_of_quantities/#defining-quantities","title":"Defining quantities","text":"

    In the mp-units library all the information about the quantity is provided with the quantity_spec class template. In order to define a specific quantity a user should inherit a strong type from such an instantiation.

    Tip

    Quantity specification definitions benefit from an explicit object parameter added in C++23 to remove the need for CRTP idiom, which significantly simplifies the code. However, as C++23 is far from being mainstream today, a portability macro QUANTITY_SPEC() is provided and used consistently through the library to allow the code to compile with C++20 compilers, thanks to the CRTP usage under the hood.

    For example, here is how the above quantity kind tree can be modeled in the library:

    C++23C++20Portable
    inline constexpr struct length : quantity_spec<dim_length> {} length;\ninline constexpr struct width : quantity_spec<length> {} width;\ninline constexpr auto breadth = width;\ninline constexpr struct height : quantity_spec<length> {} height;\ninline constexpr auto depth = height;\ninline constexpr auto altitude = height;\ninline constexpr struct thickness : quantity_spec<width> {} thickness;\ninline constexpr struct diameter : quantity_spec<width> {} diameter;\ninline constexpr struct radius : quantity_spec<width> {} radius;\ninline constexpr struct radius_of_curvature : quantity_spec<radius> {} radius_of_curvature;\ninline constexpr struct path_length : quantity_spec<length> {} path_length;\ninline constexpr auto arc_length = path_length;\ninline constexpr struct distance : quantity_spec<path_length> {} distance;\ninline constexpr struct radial_distance : quantity_spec<distance> {} radial_distance;\ninline constexpr struct wavelength : quantity_spec<length> {} wavelength;\ninline constexpr struct position_vector : quantity_spec<length, quantity_character::vector> {} position_vector;\ninline constexpr struct displacement : quantity_spec<length, quantity_character::vector> {} displacement;\n
    inline constexpr struct length : quantity_spec<length, dim_length> {} length;\ninline constexpr struct width : quantity_spec<width, length> {} width;\ninline constexpr auto breadth = width;\ninline constexpr struct height : quantity_spec<height, length> {} height;\ninline constexpr auto depth = height;\ninline constexpr auto altitude = height;\ninline constexpr struct thickness : quantity_spec<thickness, width> {} thickness;\ninline constexpr struct diameter : quantity_spec<diameter, width> {} diameter;\ninline constexpr struct radius : quantity_spec<radius, width> {} radius;\ninline constexpr struct radius_of_curvature : quantity_spec<radius_of_curvature, radius> {} radius_of_curvature;\ninline constexpr struct path_length : quantity_spec<path_length, length> {} path_length;\ninline constexpr auto arc_length = path_length;\ninline constexpr struct distance : quantity_spec<distance, path_length> {} distance;\ninline constexpr struct radial_distance : quantity_spec<radial_distance, distance> {} radial_distance;\ninline constexpr struct wavelength : quantity_spec<wavelength, length> {} wavelength;\ninline constexpr struct position_vector : quantity_spec<position_vector, length, quantity_character::vector> {} position_vector;\ninline constexpr struct displacement : quantity_spec<displacement, length, quantity_character::vector> {} displacement;\n
    QUANTITY_SPEC(length, dim_length);\nQUANTITY_SPEC(width, length);\ninline constexpr auto breadth = width;\nQUANTITY_SPEC(height, length);\ninline constexpr auto depth = height;\ninline constexpr auto altitude = height;\nQUANTITY_SPEC(thickness, width);\nQUANTITY_SPEC(diameter, width);\nQUANTITY_SPEC(radius, width);\nQUANTITY_SPEC(radius_of_curvature, radius);\nQUANTITY_SPEC(path_length, length);\ninline constexpr auto arc_length = path_length;\nQUANTITY_SPEC(distance, path_length);\nQUANTITY_SPEC(radial_distance, distance);\nQUANTITY_SPEC(wavelength, length);\nQUANTITY_SPEC(position_vector, length, quantity_character::vector);\nQUANTITY_SPEC(displacement, length, quantity_character::vector);\n

    Note

    More information on how to define a system of quantities can be found in the \"International System of Quantities (ISQ)\" chapter.

    "},{"location":"users_guide/framework_basics/systems_of_quantities/#comparing-adding-and-subtracting-quantities","title":"Comparing, adding, and subtracting quantities","text":"

    ISO 80000 explicitly states that width and height are quantities of the same kind, and as such they:

    • are mutually comparable,
    • can be added and subtracted.

    If we take the above for granted, the only reasonable result of 1 * width + 1 * height is 2 * length, where the result of length is known as a common quantity type. A result of such an equation is always the first common node in a hierarchy tree of the same kind. For example:

    static_assert(common_quantity_spec(isq::width, isq::height) == isq::length);\nstatic_assert(common_quantity_spec(isq::thickness, isq::radius) == isq::width);\nstatic_assert(common_quantity_spec(isq::distance, isq::path_length) == isq::path_length);\n
    "},{"location":"users_guide/framework_basics/systems_of_quantities/#converting-between-quantities","title":"Converting between quantities","text":"

    Based on the same hierarchy of quantities of kind length, we can define quantity conversion rules.

    1. Implicit conversions

      • every width is a length
      • every radius is a width
      static_assert(implicitly_convertible(isq::width, isq::length));\nstatic_assert(implicitly_convertible(isq::radius, isq::width));\nstatic_assert(implicitly_convertible(isq::radius, isq::length));\n
    2. Explicit conversions

      • not every length is a width
      • not every width is a radius
      static_assert(!implicitly_convertible(isq::length, isq::width));\nstatic_assert(!implicitly_convertible(isq::width, isq::radius));\nstatic_assert(!implicitly_convertible(isq::length, isq::radius));\nstatic_assert(explicitly_convertible(isq::length, isq::width));\nstatic_assert(explicitly_convertible(isq::width, isq::radius));\nstatic_assert(explicitly_convertible(isq::length, isq::radius));\n
    3. Explicit casts

      • height is not a width
      • both height and width are quantities of kind length
      static_assert(!implicitly_convertible(isq::height, isq::width));\nstatic_assert(!explicitly_convertible(isq::height, isq::width));\nstatic_assert(castable(isq::height, isq::width));\n
    4. No conversion

      • time has nothing in common with length
      static_assert(!implicitly_convertible(isq::time, isq::length));\nstatic_assert(!explicitly_convertible(isq::time, isq::length));\nstatic_assert(!castable(isq::time, isq::length));\n
    "},{"location":"users_guide/framework_basics/systems_of_quantities/#hierarchies-of-derived-quantities","title":"Hierarchies of derived quantities","text":"

    Derived quantity equations often do not automatically form a hierarchy tree. This is why it is sometimes not obvious what such a tree should look like. Also, ISO explicitly states:

    ISO/IEC Guide 99

    The division of \u2018quantity\u2019 according to \u2018kind of quantity\u2019 is, to some extent, arbitrary.

    The below presents some arbitrary hierarchy of derived quantities of kind energy:

    flowchart TD\n    energy[\"energy\\n(mass * length<sup>2</sup> / time<sup>2</sup>)\"]\n    energy --- mechanical_energy\n    mechanical_energy --- potential_energy\n    potential_energy --- gravitational_potential_energy[\"gravitational_potential_energy\\n(mass * acceleration_of_free_fall * height)\"]\n    potential_energy --- elastic_potential_energy[\"elastic_potential_energy\\n(spring_constant * amount_of_compression<sup>2</sup>)\"]\n    mechanical_energy --- kinetic_energy[\"kinetic_energy\\n(mass * speed<sup>2</sup>)\"]\n    energy --- enthalpy\n    enthalpy --- internal_energy[internal_energy, thermodynamic_energy]\n    internal_energy --- Helmholtz_energy[Helmholtz_energy, Helmholtz_function]\n    enthalpy --- Gibbs_energy[Gibbs_energy, Gibbs_function]\n    energy --- active_energy

    Notice, that even though all of those quantities have the same dimension and can be expressed in the same units, they have different quantity equations that can be used to create them implicitly:

    • energy is the most generic one and thus can be created from base quantities of mass, length, and time. As those are also the roots of quantities of their kinds and all other quantities from their trees are implicitly convertible to them (we agreed on that \"every width is a length\" already), it means that an energy can be implicitly constructed from any quantity of mass, length, and time:

      static_assert(implicitly_convertible(isq::mass * pow<2>(isq::length) / pow<2>(isq::time), isq::energy));\nstatic_assert(implicitly_convertible(isq::mass * pow<2>(isq::height) / pow<2>(isq::time), isq::energy));\n
    • mechanical energy is a more \"specialized\" quantity than energy (not every energy is a mechanical energy). It is why an explicit cast is needed to convert from either energy or the results of its quantity equation:

      static_assert(!implicitly_convertible(isq::energy, isq::mechanical_energy));\nstatic_assert(explicitly_convertible(isq::energy, isq::mechanical_energy));\nstatic_assert(!implicitly_convertible(isq::mass * pow<2>(isq::length) / pow<2>(isq::time),\n                                      isq::mechanical_energy));\nstatic_assert(explicitly_convertible(isq::mass * pow<2>(isq::length) / pow<2>(isq::time),\n                                     isq::mechanical_energy));\n
    • gravitational potential energy is not only even more specialized one but additionally, it is special in a way that it provides its own \"constrained\" quantity equation. Maybe not every mass * pow<2>(length) / pow<2>(time) is a gravitational potential energy, but every mass * acceleration_of_free_fall * height is.

      static_assert(!implicitly_convertible(isq::energy, gravitational_potential_energy));\nstatic_assert(explicitly_convertible(isq::energy, gravitational_potential_energy));\nstatic_assert(!implicitly_convertible(isq::mass * pow<2>(isq::length) / pow<2>(isq::time),\n                                      gravitational_potential_energy));\nstatic_assert(explicitly_convertible(isq::mass * pow<2>(isq::length) / pow<2>(isq::time),\n                                     gravitational_potential_energy));\nstatic_assert(implicitly_convertible(isq::mass * isq::acceleration_of_free_fall * isq::height,\n                                     gravitational_potential_energy));\n
    "},{"location":"users_guide/framework_basics/systems_of_quantities/#modeling-a-quantity-kind","title":"Modeling a quantity kind","text":"

    In the physical units library, we also need an abstraction describing an entire family of quantities of the same kind. Such quantities have not only the same dimension but also can be expressed in the same units.

    To annotate a quantity to represent its kind (and not just a hierarchy tree's root quantity) we introduced a kind_of<> specifier. For example, to express any quantity of length, we need to type kind_of<isq::length>.

    Important

    isq::length and kind_of<isq::length> are two different things.

    Such an entity behaves as any quantity of its kind. This means that it is implicitly convertible to any quantity in a tree.

    static_assert(!implicitly_convertible(isq::length, isq::height));\nstatic_assert(implicitly_convertible(kind_of<isq::length>, isq::height));\n

    Additionally, the result of operations on quantity kinds is also a quantity kind:

    static_assert(same_type<kind_of<isq::length> / kind_of<isq::time>, kind_of<isq::length / isq::time>>);\n

    However, if at least one equation's operand is not a quantity kind, the result becomes a \"strong\" quantity where all the kinds are converted to the hierarchy tree's root quantities:

    static_assert(!same_type<kind_of<isq::length> / isq::time, kind_of<isq::length / isq::time>>);\nstatic_assert(same_type<kind_of<isq::length> / isq::time, isq::length / isq::time>);\n

    Info

    Only a root quantity from the hierarchy tree or the one marked with is_kind specifier in the quantity_spec definition can be put as a template parameter to the kind_of specifier. For example, kind_of<isq::width> will fail to compile. However, we can call get_kind(q) to obtain a kind of any quantity:

    static_assert(get_kind(isq::width) == kind_of<isq::length>);\n
    "},{"location":"users_guide/framework_basics/systems_of_units/","title":"Systems of Units","text":"

    Modeling a system of units is probably the most important feature and a selling point of every physical units library. Thanks to that, the library can protect users from performing invalid operations on quantities and provide automated conversion factors between various compatible units.

    Probably all the libraries in the wild model the SI and many of them provide support for additional units belonging to various other systems (e.g., imperial, cgs, etc).

    "},{"location":"users_guide/framework_basics/systems_of_units/#systems-of-units-are-based-on-systems-of-quantities","title":"Systems of Units are based on Systems of Quantities","text":"

    Systems of quantities specify a set of quantities and equations relating to those quantities. Those equations do not take any unit or a numerical representation into account at all. To create a quantity, we need to add those missing pieces of information. This is where a system of units kicks in.

    The SI is explicitly stated to be based on the ISQ. Among others, it defines 7 base units, one for each base quantity. In the mp-units this is expressed by associating a quantity kind (that we discussed in detail in the previous chapter) with a unit that is used to express it:

    inline constexpr struct metre : named_unit<\"m\", kind_of<isq::length>> {} metre;\n

    Important

    The kind_of<isq::length> above states explicitly that this unit has an associated quantity kind. In other words, si::metre (and scaled units based on it) can be used to express the amount of any quantity of kind length.

    "},{"location":"users_guide/framework_basics/systems_of_units/#units-compose","title":"Units compose","text":"

    One of the most vital points of the SI system is that its units compose. This allows providing thousands of different units for hundreds of various quantities with a tiny set of predefined units and prefixes.

    The same is modeled in the mp-units library, which also allows composing predefined units to create a nearly infinite number of different derived units. For example, one can write:

    quantity<si::metre / si::second> q;\n

    to express a quantity of speed. The resulting quantity type is implicitly inferred from the unit equation by repeating the same operations on the associated quantity kinds.

    "},{"location":"users_guide/framework_basics/systems_of_units/#many-shades-of-the-same-unit","title":"Many shades of the same unit","text":"

    The SI provides the names for 22 common coherent units of 22 derived quantities.

    Each such named derived unit is a result of a specific predefined unit equation. For example, a unit of power quantity is defined in the library as:

    inline constexpr struct watt : named_unit<\"W\", joule / second> {} watt;\n

    However, a power quantity can be expressed in other units as well. For example, the following:

    auto q1 = 42 * W;\nstd::cout << q1 << \"\\n\";\nstd::cout << q1.in(J / s) << \"\\n\";\nstd::cout << q1.in(N * m / s) << \"\\n\";\nstd::cout << q1.in(kg * m2 / s3) << \"\\n\";\n

    prints:

    42 W\n42 J/s\n42 N m/s\n42 kg m\u00b2/s\u00b3\n

    All of the above quantities are equivalent and mean exactly the same.

    "},{"location":"users_guide/framework_basics/systems_of_units/#constraining-a-derived-unit-to-work-only-with-a-specific-derived-quantity","title":"Constraining a derived unit to work only with a specific derived quantity","text":"

    Some derived units are valid only for specific derived quantities. For example, SI specifies both hertz and becquerel derived units with the same unit equation 1 / s. However, it also explicitly states:

    SI Brochure

    The hertz shall only be used for periodic phenomena and the becquerel shall only be used for stochastic processes in activity referred to a radionuclide.

    The above means that the usage of becquerel as a unit of a frequency quantity is an error.

    The library allows constraining such units to work only with quantities of a specific kind in the following way:

    inline constexpr struct hertz : named_unit<\"Hz\", one / second, kind_of<isq::frequency>> {} hertz;\ninline constexpr struct becquerel : named_unit<\"Bq\", one / second, kind_of<isq::activity>> {} becquerel;\n

    With the above, hertz can only be used with frequencies, while becquerel should only be used with quantities of activity. This means that the following equation will not compile:

    auto q = 1 * Hz + 1 * Bq;   // Fails to compile\n

    This is exactly what we wanted to achieve to improve the type-safety of the library.

    "},{"location":"users_guide/framework_basics/systems_of_units/#prefixed-units","title":"Prefixed units","text":"

    Besides named units, the SI specifies also 24 prefixes (all being a power of 10) that can be prepended to all named units to obtain various scaled versions of them.

    Implementation of std::ratio provided by all major compilers is able to express only 16 of them. This is why, in the mp-units, we had to find an alternative way to represent unit magnitude in a more flexible way.

    Each prefix is implemented similarly to the following:

    template<PrefixableUnit auto U> struct quecto_ : prefixed_unit<\"q\", mag_power<10, -30>, U> {};\ntemplate<PrefixableUnit auto U> inline constexpr quecto_<U> quecto;\n

    and then a PrefixableUnit can be prefixed in the following way:

    inline constexpr auto qm = quecto<metre>;\n

    The usage of mag_power not only enables providing support for SI prefixes, but it can also efficiently represent any rational magnitude. For example, IEC 80000 prefixes used in the IT industry can be implemented as:

    template<PrefixableUnit auto U> struct yobi_ : prefixed_unit<\"Yi\", mag_power<2, 80>, U> {};\ntemplate<PrefixableUnit auto U> inline constexpr yobi_<U> yobi;\n
    "},{"location":"users_guide/framework_basics/systems_of_units/#scaled-units","title":"Scaled units","text":"

    In the SI, all units are either base or derived units or prefixed versions of those. However, those are only some of the options possible.

    For example, there is a list of off-system units accepted for use with SI. Those are scaled versions of the SI units with ratios that can't be explicitly expressed with predefined SI prefixes. Those include units like minute, hour, or electronvolt:

    inline constexpr struct minute : named_unit<\"min\", mag<60> * si::second> {} minute;\ninline constexpr struct hour : named_unit<\"h\", mag<60> * minute> {} hour;\ninline constexpr struct electronvolt : named_unit<\"eV\", mag<ratio{1'602'176'634, 1'000'000'000}> * mag_power<10, -19> * si::joule> {} electronvolt;\n

    Also, units of other systems of units are often defined in terms of scaled versions of the SI units. For example, the international yard is defined as:

    inline constexpr struct yard : named_unit<\"yd\", mag<ratio{9'144, 10'000}> * si::metre> {} yard;\n

    For some units, a magnitude might also be irrational. The best example here is a degree which is defined using a floating-point magnitude having a factor of the number \u03c0 (Pi):

    inline constexpr struct mag_pi : magnitude<std::numbers::pi_v<long double>> {} mag_pi;\n
    inline constexpr struct degree : named_unit<{u8\"\u00b0\", \"deg\"}, mag_pi / mag<180> * si::radian> {} degree;\n
    "},{"location":"users_guide/framework_basics/text_output/","title":"Text Output","text":"

    Besides providing dimensional analysis and units conversions, the library also tries hard to print any quantity in the most user-friendly way.

    Note

    The library does not provide a text output for quantity points, as printing just a number and a unit is not enough to adequately describe a quantity point. Often, an additional postfix is required.

    For example, the text output of 42 m may mean many things and can also be confused with an output of a regular quantity. On the other hand, printing 42 m AMSL for altitudes above mean sea level is a much better solution, but the library does not have enough information to print it that way by itself.

    "},{"location":"users_guide/framework_basics/text_output/#derived-unit-symbols-generation","title":"Derived unit symbols generation","text":"

    The library creates symbols for derived ones based on the provided definitions for base units.

    "},{"location":"users_guide/framework_basics/text_output/#unit_symbol_formatting","title":"unit_symbol_formatting","text":"

    unit_symbol_formatting is a data type describing the configuration of the symbol generation algorithm. It contains three orthogonal fields, each with a default value.

    enum class text_encoding : std::int8_t {\n  unicode,  // m\u00b3;  \u00b5s\n  ascii,    // m^3; us\n  default_encoding = unicode\n};\n\nenum class unit_symbol_solidus : std::int8_t {\n  one_denominator,  // m/s;   kg m\u207b\u00b9 s\u207b\u00b9\n  always,           // m/s;   kg/(m s)\n  never,            // m s\u207b\u00b9; kg m\u207b\u00b9 s\u207b\u00b9\n  default_denominator = one_denominator\n};\n\nenum class unit_symbol_separator : std::int8_t {\n  space,          // kg m\u00b2/s\u00b2\n  half_high_dot,  // kg\u22c5m\u00b2/s\u00b2  (valid only for unicode encoding)\n  default_separator = space\n};\n\nstruct unit_symbol_formatting {\n  text_encoding encoding = text_encoding::default_encoding;\n  unit_symbol_solidus solidus = unit_symbol_solidus::default_denominator;\n  unit_symbol_separator separator = unit_symbol_separator::default_separator;\n};\n
    "},{"location":"users_guide/framework_basics/text_output/#unit_symbol","title":"unit_symbol()","text":"

    Returns a std::string_view with the symbol of a unit for the provided configuration:

    template<unit_symbol_formatting fmt = unit_symbol_formatting{}, typename CharT = char, Unit U>\n[[nodiscard]] consteval auto unit_symbol(U);\n

    For example:

    static_assert(unit_symbol<{.solidus = unit_symbol_solidus::never,\n                           .separator = unit_symbol_separator::half_high_dot}>(kg * m / s2) == \"kg\u22c5m\u22c5s\u207b\u00b2\");\n

    Note

    std::string_view is returned only when C++23 is available. Otherwise, an instance of a basic_fixed_string is being returned.

    "},{"location":"users_guide/framework_basics/text_output/#unit_symbol_to","title":"unit_symbol_to()","text":"

    Inserts the generated unit symbol to the output text iterator at runtime based on the provided configuration.

    template<typename CharT = char, std::output_iterator<CharT> Out, Unit U>\nconstexpr Out unit_symbol_to(Out out, U u, unit_symbol_formatting fmt = unit_symbol_formatting{});\n

    For example:

    std::string txt;\nunit_symbol_to(std::back_inserter(txt), kg * m / s2,\n               {.solidus = unit_symbol_solidus::never, .separator = unit_symbol_separator::half_high_dot});\nstd::cout << txt << \"\\n\";\n

    The above prints:

    kg\u22c5m\u22c5s\u207b\u00b2\n
    "},{"location":"users_guide/framework_basics/text_output/#quantity-text-output","title":"Quantity text output","text":""},{"location":"users_guide/framework_basics/text_output/#customization-point","title":"Customization point","text":"

    The SI Brochure says:

    SI Brochure

    The numerical value always precedes the unit and a space is always used to separate the unit from the number. ... The only exceptions to this rule are for the unit symbols for degree, minute and second for plane angle, \u00b0, \u2032 and \u2033, respectively, for which no space is left between the numerical value and the unit symbol.

    To support the above, the library exposes space_before_unit_symbol customization point. By default, its value is true for all the units, so the space between a number and a unit will be present in the output text. To change this behavior, we have to provide a partial specialization for a specific unit:

    template<>\ninline constexpr bool space_before_unit_symbol<non_si::degree> = false;\n

    Note

    The above works only for the default formatting. In case we provide our own format specification (e.g., std::format(\"{:%Q %q}\", q)), the library will always obey this specification for all the units (no matter of what is the actual value of the space_before_unit_symbol customization point) and the separating space will always be present in this case.

    "},{"location":"users_guide/framework_basics/text_output/#output-streams","title":"Output streams","text":"

    Tip

    The output streaming support is opt-in and can be enabled by including the <mp-units/ostream.h> header file.

    The easiest way to print a quantity is to provide its object to the output stream:

    using namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\nusing namespace mp_units::international::unit_symbols;\n\nconst QuantityOf<isq::speed> auto v1 = avg_speed(220. * km, 2 * h);\nconst QuantityOf<isq::speed> auto v2 = avg_speed(140. * mi, 2 * h);\nstd::cout << v1 << '\\n';  // 110 km/h\nstd::cout << v2 << '\\n';  // 70 mi/h\n

    The text output will always print the value of a quantity typically followed by a space and then the symbol of a unit associated with this quantity.

    Important: Don't assume a unit

    Remember that when we deal with a quantity of an \"unknown\" (e.g., auto) type, it is a good practice to always convert the unit to the expected one before passing it to the text output:

    std::cout << v1.in(km / h) << '\\n';       // 110 km/h\nstd::cout << v1.force_in(m / s) << '\\n';  // 30.5556 m/s\n
    "},{"location":"users_guide/framework_basics/text_output/#output-stream-formatting","title":"Output stream formatting","text":"

    Only basic formatting can be applied to output streams. It includes control over width, fill, and alignment of the entire quantity and formatting of a quantity numerical value according to the general C++ rules:

    std::cout << \"|\" << std::setw(10) << 123 * m << \"|\\n\";                       // |     123 m|\nstd::cout << \"|\" << std::setw(10) << std::left << 123 * m << \"|\\n\";          // |123 m     |\nstd::cout << \"|\" << std::setw(10) << std::setfill('*') << 123 * m << \"|\\n\";  // |123 m*****|\n

    Note

    To have more control over the formatting of the quantity that is printed with the output stream just use std::cout << std::format(...).

    "},{"location":"users_guide/framework_basics/text_output/#stdformat","title":"std::format","text":"

    Tip

    The text formatting facility support is opt-in and can be enabled by including the <mp-units/format.h> header file.

    The mp-units library provides custom formatters for std::format facility which allows fine-grained control over what and how it is being printed in the text output.

    "},{"location":"users_guide/framework_basics/text_output/#grammar","title":"Grammar","text":"
    quantity-format-spec  ::=  [fill-and-align] [width] [quantity-specs]\nquantity-specs        ::=  conversion-spec\n                           quantity-specs conversion-spec\n                           quantity-specs literal-char\nliteral-char          ::=  any character other than '{' or '}'\nconversion-spec       ::=  '%' type\ntype                  ::=  [rep-modifier] 'Q'\n                           [unit-modifier] 'q'\nrep-modifier          ::=  [sign] [#] [precision] [L] [rep-type]\nrep-type              ::=  one of\n                           a A b B d e E f F g G o x X\nunit-modifier         ::=  [text-encoding] [unit-symbol-solidus] [unit-symbol-separator]\n                           [text-encoding] [unit-symbol-separator] [unit-symbol-solidus]\n                           [unit-symbol-solidus] [text-encoding] [unit-symbol-separator]\n                           [unit-symbol-solidus] [unit-symbol-separator] [text-encoding]\n                           [unit-symbol-separator] [text-encoding] [unit-symbol-solidus]\n                           [unit-symbol-separator] [unit-symbol-solidus] [text-encoding]\ntext-encoding         ::=  one of\n                           U A\nunit-symbol-solidus   ::=  one of\n                           o a n\nunit-symbol-separator ::=  one of\n                           s d\n

    In the above grammar:

    • fill-and-align, width, sign, #, precision, and L tokens, as well as the individual tokens of rep-type are defined in the format.string.std chapter of the C++ standard specification,
    • tokens Q and q of type are described in the time.format chapter of the C++ standard specification,
    • text-encoding tokens specify the unit text encoding:
      • U (default) uses the Unicode symbols defined by the SI specification (e.g., m\u00b3, \u00b5s)
      • A token forces non-standard ASCII-only output (e.g., m^3, us)
    • unit-symbol-solidus tokens specify how the division of units should look like:
      • o (default) outputs / only when there is only one unit in the denominator, otherwise negative exponents are printed (e.g., m/s, kg m\u207b\u00b9 s\u207b\u00b9)
      • a always uses solidus (e.g., m/s, kg/(m s))
      • n never prints solidus, which means that negative exponents are always used (e.g., m s\u207b\u00b9, kg m\u207b\u00b9 s\u207b\u00b9)
    • unit-symbol-separator tokens specify how multiplied unit symbols should be separated:
      • s (default) uses space as a separator (e.g., kg m\u00b2/s\u00b2)
      • d uses half-high dot (\u22c5) as a separator (e.g., kg\u22c5m\u00b2/s\u00b2)
    "},{"location":"users_guide/framework_basics/text_output/#default-formatting","title":"Default formatting","text":"

    To format quantity values, the formatting facility uses quantity-format-spec. If left empty, the default formatting is applied. The same default formatting is also applied to the output streams. This is why the following code lines produce the same output:

    std::cout << \"Distance: \" << 123 * km << \"\\n\";\nstd::cout << std::format(\"Distance: {}\\n\", 123 * km);\nstd::cout << std::format(\"Distance: {:%Q %q}\\n\", 123 * km);\n

    Note

    For some quantities, the {:%Q %q} format may provide a different output than the default one. It will happen, for example for:

    • units for which space_before_unit_symbol customization point is set to false,
    • quantities of dimension one with a unit one.
    "},{"location":"users_guide/framework_basics/text_output/#controlling-width-fill-and-alignment","title":"Controlling width, fill, and alignment","text":"

    To control width, fill, and alignment, the C++ standard grammar tokens fill-and-align and width are being used, and they treat a quantity value and symbol as a contiguous text:

    std::println(\"|{:0}|\", 123 * m);     // |123 m|\nstd::println(\"|{:10}|\", 123 * m);    // |     123 m|\nstd::println(\"|{:<10}|\", 123 * m);   // |123 m     |\nstd::println(\"|{:>10}|\", 123 * m);   // |     123 m|\nstd::println(\"|{:^10}|\", 123 * m);   // |  123 m   |\nstd::println(\"|{:*<10}|\", 123 * m);  // |123 m*****|\nstd::println(\"|{:*>10}|\", 123 * m);  // |*****123 m|\nstd::println(\"|{:*^10}|\", 123 * m);  // |**123 m***|\n

    Note

    std::println is a C++23 facility. In case we do not have access to C++23, we can obtain the same output with:

    std::cout << std::format(\"<format-string>\\n\", <format-args>);\n
    "},{"location":"users_guide/framework_basics/text_output/#quantity-value-symbol-or-both","title":"Quantity value, symbol, or both?","text":"

    The user can easily decide to either print a whole quantity (value and symbol) or only its parts. Also, a custom style of quantity formatting might be applied:

    std::println(\"{:%Q}\", 123 * km);    // 123\nstd::println(\"{:%q}\", 123 * km);    // km\nstd::println(\"{:%Q%q}\", 123 * km);  // 123km\n
    "},{"location":"users_guide/framework_basics/text_output/#quantity-value-formatting","title":"Quantity value formatting","text":"

    sign token allows us to specify how the value's sign is being printed:

    std::println(\"{0:%Q %q},{0:%+Q %q},{0:%-Q %q},{0:% Q %q}\", 1 * m);   // 1 m,+1 m,1 m, 1 m\nstd::println(\"{0:%Q %q},{0:%+Q %q},{0:%-Q %q},{0:% Q %q}\", -1 * m);  // -1 m,-1 m,-1 m,-1 m\n

    where:

    • + indicates that a sign should be used for both non-negative and negative numbers,
    • - indicates that a sign should be used for negative numbers and negative zero only (this is the default behavior),
    • <space> indicates that a leading space should be used for non-negative numbers other than negative zero, and a minus sign for negative numbers and negative zero.

    precision token is allowed only for floating-point representation types:

    std::println(\"{:%.0Q %q}\", 1.2345 * m);  // 1 m\nstd::println(\"{:%.1Q %q}\", 1.2345 * m);  // 1.2 m\nstd::println(\"{:%.2Q %q}\", 1.2345 * m);  // 1.23 m\n

    rep-type specifies how a value of the representation type is being printed. For integral types:

    std::println(\"{:%bQ %q}\", 42 * m);    // 101010 m\nstd::println(\"{:%BQ %q}\", 42 * m);    // 101010 m\nstd::println(\"{:%dQ %q}\", 42 * m);    // 42 m\nstd::println(\"{:%oQ %q}\", 42 * m);    // 52 m\nstd::println(\"{:%xQ %q}\", 42 * m);    // 2a m\nstd::println(\"{:%XQ %q}\", 42 * m);    // 2A m\n

    The above can be printed in an alternate version thanks to the # token:

    std::println(\"{:%#bQ %q}\", 42 * m);   // 0b101010 m\nstd::println(\"{:%#BQ %q}\", 42 * m);   // 0B101010 m\nstd::println(\"{:%#oQ %q}\", 42 * m);   // 052 m\nstd::println(\"{:%#xQ %q}\", 42 * m);   // 0x2a m\nstd::println(\"{:%#XQ %q}\", 42 * m);   // 0X2A m\n

    For floating-point values, the rep-type token works as follows:

    std::println(\"{:%aQ %q}\",   1.2345678 * m);      // 0x1.3c0ca2a5b1d5dp+0 m\nstd::println(\"{:%.3aQ %q}\", 1.2345678 * m);      // 0x1.3c1p+0 m\nstd::println(\"{:%AQ %q}\",   1.2345678 * m);      // 0X1.3C0CA2A5B1D5DP+0 m\nstd::println(\"{:%.3AQ %q}\", 1.2345678 * m);      // 0X1.3C1P+0 m\nstd::println(\"{:%eQ %q}\",   1.2345678 * m);      // 1.234568e+00 m\nstd::println(\"{:%.3eQ %q}\", 1.2345678 * m);      // 1.235e+00 m\nstd::println(\"{:%EQ %q}\",   1.2345678 * m);      // 1.234568E+00 m\nstd::println(\"{:%.3EQ %q}\", 1.2345678 * m);      // 1.235E+00 m\nstd::println(\"{:%gQ %q}\",   1.2345678 * m);      // 1.23457 m\nstd::println(\"{:%gQ %q}\",   1.2345678e8 * m);    // 1.23457e+08 m\nstd::println(\"{:%.3gQ %q}\", 1.2345678 * m);      // 1.23 m\nstd::println(\"{:%.3gQ %q}\", 1.2345678e8 * m);    // 1.23e+08 m\nstd::println(\"{:%GQ %q}\",   1.2345678 * m);      // 1.23457 m\nstd::println(\"{:%GQ %q}\",   1.2345678e8 * m);    // 1.23457E+08 m\nstd::println(\"{:%.3GQ %q}\", 1.2345678 * m);      // 1.23 m\nstd::println(\"{:%.3GQ %q}\", 1.2345678e8 * m);    // 1.23E+08 m\n
    "},{"location":"users_guide/framework_basics/text_output/#unit-symbol-formatting","title":"Unit symbol formatting","text":"

    Unit symbols of some quantities are specified to use Unicode signs by the SI (e.g., \u03a9 symbol for the resistance quantity). The mp-units library follows this by default. From the engineering point of view, sometimes Unicode text might not be the best solution as terminals of many (especially embedded) devices are ASCII-only. In such a case, the unit symbol can be forced to be printed using ASCII-only characters thanks to the text-encoding token:

    std::println(\"{}\", 10 * si::ohm);             // 10 \u03a9\nstd::println(\"{:%Q %Aq}\", 10 * si::ohm);      // 10 ohm\nstd::println(\"{}\", 125 * us);                 // 125 \u00b5s\nstd::println(\"{:%Q %Aq}\", 125 * us);          // 125 us\nstd::println(\"{}\", 9.8 * (m / s2));           // 9.8 m/s\u00b2\nstd::println(\"{:%Q %Aq}\", 9.8 * (m / s2));    // 9.8 m/s^2\n

    Additionally, both ISQ and SI leave some freedom on how to print unit symbols. This is why two additional tokens were introduced.

    unit-symbol-solidus specifies how the division of units should look like. By default, / will be used only when the denominator contains only one unit. However, with the a or n options, we can force the facility to print the / character always (even when there are more units in the denominator), or never in which case a parenthesis will be added to enclose all denominator units.

    std::println(\"{:%Q %q}\", 1 * m / s);         // 1 m/s\nstd::println(\"{:%Q %q}\", 1 * kg / m / s2);   // 1 kg m\u207b\u00b9 s\u207b\u00b2\nstd::println(\"{:%Q %aq}\", 1 * m / s);        // 1 m/s\nstd::println(\"{:%Q %aq}\", 1 * kg / m / s2);  // 1 kg/(m s\u00b2)\nstd::println(\"{:%Q %nq}\", 1 * m / s);        // 1 m s\u207b\u00b9\nstd::println(\"{:%Q %nq}\", 1 * kg / m / s2);  // 1 kg m\u207b\u00b9 s\u207b\u00b2\n

    Also, there are a few options to separate the units being multiplied:

    ISO 80000-1

    When symbols for quantities are combined in a product of two or more quantities, this combination is indicated in one of the following ways: ab, a b, a \u00b7 b, a \u00d7 b

    NOTE 1 In some fields, e.g., vector algebra, distinction is made between a \u2219 b and a \u00d7 b.

    As of today, the mp-units library only supports a b and a \u00b7 b. Additionally, we decided that the extraneous space in the latter case makes the result too verbose, so we decided to just use the \u00b7 symbol as a separator.

    Note

    Please let us know if you require more formatting options here.

    The unit-symbol-separator token allows us to obtain the following outputs:

    std::println(\"{:%Q %q}\", 1 * kg * m2 / s2);   // 1 kg m\u00b2/s\u00b2\nstd::println(\"{:%Q %dq}\", 1 * kg * m2 / s2);  // 1 kg\u22c5m\u00b2/s\u00b2\n
    "},{"location":"users_guide/framework_basics/the_affine_space/","title":"The Affine Space","text":"

    The affine space has two types of entities:

    • Point - a position specified with coordinate values (e.g., location, address, etc.)
    • Vector - the difference between two points (e.g., shift, offset, displacement, duration, etc.)

    Note

    The Vector described here is specific to the affine space theory and is not the same thing as the quantity of a vector character that we discussed in the \"Scalars, vectors, and tensors\" chapter (although, in some cases, those terms may overlap).

    "},{"location":"users_guide/framework_basics/the_affine_space/#operations-in-the-affine-space","title":"Operations in the affine space","text":"

    Here are the primary operations one can do in the affine space:

    • Vector + Vector -> Vector
    • Vector - Vector -> Vector
    • -Vector -> Vector
    • Vector * Scalar -> Vector
    • Scalar * Vector -> Vector
    • Vector / Scalar -> Vector
    • Point - Point -> Vector
    • Point + Vector -> Point
    • Vector + Point -> Point
    • Point - Vector -> Point

    Important

    It is not possible to:

    • add two Points,
    • subtract a Point from a Vector,
    • multiply nor divide Points with anything else.
    "},{"location":"users_guide/framework_basics/the_affine_space/#points-are-more-common-than-most-of-us-imagine","title":"Points are more common than most of us imagine","text":"

    Point abstractions should be used more often in the C++ software. They are not only about temperature or time. Points are everywhere around us and should become more popular in the products we implement. They can be used to implement:

    • temperature points,
    • timestamps,
    • daily mass readouts from the scale,
    • altitudes of mountain peaks on a map,
    • current speed displayed on a car's speed-o-meter,
    • today's price of instruments on the market,
    • and many more.

    Improving the affine space's Points intuition will allow us to write better and safer software.

    "},{"location":"users_guide/framework_basics/the_affine_space/#vector-is-modeled-by-quantity","title":"Vector is modeled by quantity","text":"

    Up until now, each time we used a quantity in our code, we were modeling some kind of a difference between two things:

    • the distance between two points,
    • duration between two time points,
    • the difference in speed (even if relative to zero).

    As we already know, a quantity type provides all operations required for a Vector type in the affine space.

    "},{"location":"users_guide/framework_basics/the_affine_space/#point-is-modeled-by-quantity_point-and-pointorigin","title":"Point is modeled by quantity_point and PointOrigin","text":"

    In the mp-units library the Point abstraction is modelled by:

    • PointOrigin concept that specifies measurement origin,
    • quantity_point class template that specifies a Point relative to a specific predefined origin.
    "},{"location":"users_guide/framework_basics/the_affine_space/#quantity_point","title":"quantity_point","text":"

    The quantity_point class template specifies an absolute quantity measured from a predefined origin:

    template<Reference auto R,\n         PointOriginFor<get_quantity_spec(R)> auto PO = default_point_origin(R),\n         RepresentationOf<get_quantity_spec(R).character> Rep = double>\nclass quantity_point;\n

    As we can see above, the quantity_point class template exposes one additional parameter compared to quantity. The PO parameter satisfies a PointOriginFor concept and specifies the origin of our measurement scale. By default, it is initialized with a quantity's zeroth point using the following rules:

    • if the measurement unit of a quantity specifies its point origin in its definition (e.g., degree Celsius), then this point is being used,
    • otherwise, an instantiation of zeroth_point_origin<QuantitySpec> is being used which provides a zeroth point for a specific quantity type.

    Tip

    The quantity_point definition can be found in the mp-units/quantity_point.h header file.

    "},{"location":"users_guide/framework_basics/the_affine_space/#implicit-point-origin","title":"Implicit point origin","text":"

    Let's assume that Alice goes for a trip driving a car. She likes taking notes about interesting places that she visits on the road. For every such item, she writes down:

    • its name,
    • a readout from the car's odometer at the location,
    • a current timestamp.

    We can implement this in the following way:

    using std::chrono::system_clock;\n\nstruct trip_log_item {\n  std::string name;\n  quantity_point<isq::distance[km]> odometer;\n  quantity_point<si::second> timestamp;\n};\nusing trip_log = std::vector<trip_log_item>;\n
    trip_log log;\n\nquantity_point timestamp_1{quantity{system_clock::now().time_since_epoch()}};\nlog.emplace_back(\"home\", quantity_point{1356 * km}, timestamp_1);\n\n// some time passes\n\nquantity_point timestamp_2{quantity{system_clock::now().time_since_epoch()}};\nlog.emplace_back(\"castle\", quantity_point{1401 * km}, timestamp_2);\n

    This is an excellent example of where points are helpful. There is no doubt about the correctness of their usage in this scenario:

    • adding two odometer readouts or two timestamps have no physical sense, and that is why we will expect a compile-time error when we try to perform such operations accidentally,
    • subtracting two odometer readouts or timestamps is perfectly valid and results in a quantity storing the interval value between the two points.

    Having such a database, we can print the trip log in the following way:

    for (const auto& item : log) {\n  std::cout << \"POI: \" << item.name << \"\\n\";\n  std::cout << \"- Distance from home: \" << item.odometer - log.front().odometer << \"\\n\";\n  std::cout << \"- Trip duration from start: \" << (item.timestamp - log.front().timestamp).in(non_si::minute) << \"\\n\";\n}\n

    Moreover, if Alice had reset the car's trip odometer before leaving home, we could have rewritten one of the previous lines like that:

    std::cout << \"Distance from home: \" << item.odometer.quantity_from_zero() << \"\\n\";\n

    The above always returns a quantity measured from the \"ultimate\" zeroth point of a scale used for this specific quantity type.

    Tip

    Storing Points is the most efficient representation we can choose in this scenario:

    • to store a value, we read it directly from the instrument, and no additional transformation is needed,
    • to print the absolute value (e.g., odometer), we have the value available right away,
    • to get any relative quantity (e.g., distance from the start, distance from the previous point, etc.), we have to perform a single subtraction operation.

    If we stored Vectors in our database instead, we would have to pay at runtime for additional operations:

    • to store a quantity, we would have to perform the subtraction right away to get the interval between the current value and some reference point,
    • to print the absolute value, we would have to add the quantity to the reference point that we need to store somewhere in the database as well,
    • to get a relative quantity, only the currently stored one is immediate; all other values will require at least one quantity addition operation.

    Now, let's assume that Bob, a friend of Alice, also keeps a log of his trips but he, of course, measures distances from his own home with the odometer in his car. Everything is fine as long as we deal with one trip at a time, but if we start to work with both at once, we may accidentally subtract points from different trips. The library will not prevent us from doing so.

    The points from Alice's and Bob's trips should be considered separate, and to enforce it at compilation time, we need to introduce explicit origins.

    "},{"location":"users_guide/framework_basics/the_affine_space/#absolute-point-origin","title":"Absolute Point origin","text":"

    The absolute point origin specifies the \"zero\" of our measurement's scale. User can specify such an origin by deriving from the absolute_point_origin class template:

    enum class actor { alice, bob };\n\ntemplate<actor A>\nstruct zeroth_odometer_t : absolute_point_origin<zeroth_odometer_t<A>, isq::distance> {};\n\ntemplate<actor A>\ninline constexpr zeroth_odometer_t<A> zeroth_odometer;\n

    Info

    The absolute_point_origin class template uses the CRTP idiom to enforce the uniqueness of such a type. You should pass the type of a derived class as the first argument of the template instantiation.

    Note

    Unfortunately, due to inconsistencies in C++ language rules:

    • we can't define the above in one line of code,
    • provide the same identifier for a class and variable template.

    Odometer is not the only one that can get an explicit point origin in our case. As timestamps are provided by the std::chrono::system_clock, their values are always relative to the epoch of this clock.

    Note

    The mp-units library provides means to specify interoperability with other units libraries. It also has built-in compatibility with std::chrono types, so users do not have to define interoperability traits or point origins for such types by themselves. Those are already provided in the mp-units/systems/si/chrono.h header file.

    Now, we can refactor our database to benefit from the explicit points:

    template<actor A>\nstruct trip_log_item {\n  std::string point_name;\n  quantity_point<si::kilo<si::metre>, zeroth_odometer<A>> odometer;\n  quantity_point<si::second, chrono_point_origin<system_clock>> timestamp;\n};\n\ntemplate<actor A>\nusing trip_log = std::vector<trip_log_item<A>>;\n

    We also need to update the initialization part in our code. In the case of implicit zeroth origins, we could construct quantity_point directly from the value of a quantity. This is no longer the case. As a Point can be represented with a Vector from the origin, to improve the safety of the code we write, a quantity_point class template must be created with one of the following operations:

    quantity_point qp1 = zeroth_odometer<actor::alice> + 1356 * km;\nquantity_point qp2 = 1356 * km + zeroth_odometer<actor::alice>;\nquantity_point qp3 = zeroth_odometer<actor::alice> - 1356 * km;\n

    Although, the qp3 above does not have a physical sense in this specific scenario.

    Note

    It is not allowed to subtract a Point from a Vector thus 1356 * km - zeroth_odometer<actor::alice> is an invalid operation.

    Info

    A rationale for this longer construction syntax can be found in the Why can't I create a quantity by passing a number to a constructor? chapter.

    Similarly to creation of a quantity, if someone does not like the operator-based syntax to create a quantity_point, the same results can be achieved with a two-parameter constructor:

    quantity_point qp4{1356 * km, zeroth_odometer<actor::alice>};\n

    Also, as now our timestamps have a proper point origin provided in a type, we can simplify the previous code by directly converting std::chrono::time_point to our quantity_point type.

    With all the above, we can refactor our initialization part to the following:

    trip_log<actor::alice> alice_log;\n\nalice_log.emplace_back(\"home\", zeroth_odometer<actor::alice> + 1356 * km, system_clock::now());\n\n// some time passes\n\nalice_log.emplace_back(\"castle\", zeroth_odometer<actor::alice> + 1401 * km, system_clock::now());\n
    "},{"location":"users_guide/framework_basics/the_affine_space/#point-arithmetics","title":"Point arithmetics","text":"

    As another example, let's assume we will attend the CppCon conference hosted in Aurora, CO, and we want to estimate the distance we will travel. We have to take a taxi to a local airport, fly to DEN airport with a stopover in FRA, and, in the end, get a cab to the Gaylord Rockies Resort & Convention Center:

    constexpr struct home : absolute_point_origin<home, isq::distance> {} home;\n\nquantity_point<isq::distance[km], home> home_airport = home + 15 * km;\nquantity_point<isq::distance[km], home> fra_airport = home_airport + 829 * km;\nquantity_point<isq::distance[km], home> den_airport = fra_airport + 8115 * km;\nquantity_point<isq::distance[km], home> cppcon_venue = den_airport + 10.1 * mi;\n

    As we can see above, we can easily get a new point by adding a quantity to an origin or another quantity point.

    If we want to find out the distance traveled between two points, we simply subtract them:

    quantity<isq::distance[km]> total = cppcon_venue - home;\nquantity<isq::distance[km]> flight = den_airport - home_airport;\n

    If we would like to find out the total distance traveled by taxi as well, we have to do a bit more calculations:

    quantity<isq::distance[km]> taxi1 = home_airport - home;\nquantity<isq::distance[km]> taxi2 = cppcon_venue - den_airport;\nquantity<isq::distance[km]> taxi = taxi1 + taxi2;\n

    Now, if we print the results:

    std::cout << \"Total distance:  \" << total << \"\\n\";\nstd::cout << \"Flight distance: \" << flight << \"\\n\";\nstd::cout << \"Taxi distance:   \" << taxi << \"\\n\";\n

    we will see the following output:

    Total distance:  8975.25 km\nFlight distance: 8944 km\nTaxi distance:   31.2544 km\n

    Note

    It is not allowed to subtract two point origins defined in terms of absolute_point_origin (e.g., home - home) as those do not contain information about the unit, so we are not able to determine a resulting quantity type.

    "},{"location":"users_guide/framework_basics/the_affine_space/#relative-point-origin","title":"Relative Point origin","text":"

    We often do not have only one ultimate \"zero\" point when we measure things.

    For example, let's assume that we have the following absolute point origin:

    constexpr struct mean_sea_level : absolute_point_origin<mean_sea_level, isq::altitude> {} mean_sea_level;\n

    If we want to model a trip to Mount Everest, measuring all daily hikes from the mean_sea_level might not be efficient. We may know that we are not good climbers, so all our climbs can be represented with an 8-bit integer type, allowing us to save memory in our database of climbs.

    For this purpose, we can define a relative_point_origin in the following way:

    constexpr struct everest_base_camp : relative_point_origin<mean_sea_level + 5364 * m> {} everest_base_camp;\n

    The above can be used as an origin for subsequent Points:

    constexpr quantity_point first_climb_alt = everest_base_camp + isq::altitude(std::uint8_t{42} * m);\nstatic_assert(first_climb_alt.quantity_from(everest_base_camp) == 42 * m);\nstatic_assert(first_climb_alt.quantity_from(mean_sea_level) == 5406 * m);\nstatic_assert(first_climb_alt.quantity_from_zero() == 5406 * m);\n

    As we can see above, the quantity_from() member function returns a relative distance from the provided point origin while the quantity_from_zero() returns the distance from the absolute point origin.

    "},{"location":"users_guide/framework_basics/the_affine_space/#converting-between-different-representations-of-the-same-point","title":"Converting between different representations of the same Point","text":"

    As we might represent the same Point with Vectors from various origins, the mp-units library provides facilities to convert the Point to quantity_point class templates expressed in terms of origins relative to each other in the type system.

    For this purpose, we can use:

    • a converting constructor:

      constexpr quantity_point<isq::altitude[m], mean_sea_level, int> qp = first_climb_alt;\nstatic_assert(qp.quantity_ref_from(qp.point_origin) == 5406 * m);\n
    • a dedicated conversion interface:

      constexpr quantity_point qp = first_climb_alt.point_for(mean_sea_level);\nstatic_assert(qp.quantity_ref_from(qp.point_origin) == 5406 * m);\n

    Note

    It is only allowed to convert between various origins defined in terms of the same absolute_point_origin. Even if it is theoretically possible to express the same Point as a Vector from another absolute_point_origin, the library will not allow such a conversion. A custom user-defined conversion function will be needed to add this functionality.

    Said otherwise, in the mp-units library, there is no way to spell how two distinct absolute_point_origin types relate to each other.

    "},{"location":"users_guide/framework_basics/the_affine_space/#temperature-support","title":"Temperature support","text":"

    Another important example of relative point origins is the support of temperature quantity points. The mp-units library provides a few predefined point origins for this purpose:

    namespace si {\n\ninline constexpr struct absolute_zero : absolute_point_origin<absolute_zero, isq::thermodynamic_temperature> {} absolute_zero;\ninline constexpr struct zeroth_kelvin : decltype(absolute_zero) {} zeroth_kelvin;\n\ninline constexpr struct ice_point : relative_point_origin<quantity_point{273'150 * milli<kelvin>}> {} ice_point;\ninline constexpr struct zeroth_degree_Celsius : decltype(ice_point) {} zeroth_degree_Celsius;\n\n}\n\nnamespace usc {\n\ninline constexpr struct zeroth_degree_Fahrenheit :\n  relative_point_origin<si::zeroth_degree_Celsius - 32 * (mag<ratio{5, 9}> * si::degree_Celsius)> {} zeroth_degree_Fahrenheit;\n\n}\n

    The above is a great example of how point origins can be stacked on top of each other:

    • usc::zeroth_degree_Fahrenheit is defined relative to si::zeroth_degree_Celsius
    • si::zeroth_degree_Celsius is defined relative to si::zeroth_kelvin.

    Note

    Notice that while stacking point origins, we can use not only different representation types but also different units for origins and a Point. In the above example, the relative point origin for degree Celsius is defined in terms of si::kelvin, while the quantity point for it will use si::degree_Celsius as a unit.

    The temperature point origins defined above are provided explicitly in the respective units' definitions:

    namespace si {\n\ninline constexpr struct kelvin :\n    named_unit<\"K\", kind_of<isq::thermodynamic_temperature>, zeroth_kelvin> {} kelvin;\ninline constexpr struct degree_Celsius :\n    named_unit<{u8\"\u00b0C\", \"`C\"}, kelvin, zeroth_degree_Celsius> {} degree_Celsius;\n\n}\n\nnamespace usc {\n\ninline constexpr struct degree_Fahrenheit :\n    named_unit<{u8\"\u00b0F\", \"`F\"}, mag<ratio{5, 9}> * si::degree_Celsius, zeroth_degree_Fahrenheit> {} degree_Fahrenheit;\n\n}\n

    Now let's see how we can benefit from the above definitions. We have quite a few alternatives to choose from here. Depending on our needs or taste we can:

    • be explicit about the unit and origin:

      quantity_point<si::degree_Celsius, si::zeroth_degree_Celsius> q1 = si::zeroth_degree_Celsius + 20.5 * deg_C;\nquantity_point<si::degree_Celsius, si::zeroth_degree_Celsius> q2 = {20.5 * deg_C, si::zeroth_degree_Celsius};\nquantity_point<si::degree_Celsius, si::zeroth_degree_Celsius> q3{20.5 * deg_C};\n
    • specify a unit and use its zeroth point origin implicitly:

      quantity_point<si::degree_Celsius> q4 = si::zeroth_degree_Celsius + 20.5 * deg_C;\nquantity_point<si::degree_Celsius> q5 = {20.5 * deg_C, si::zeroth_degree_Celsius};\nquantity_point<si::degree_Celsius> q6{20.5 * deg_C};\n
    • benefit from CTAD:

      quantity_point q7 = si::zeroth_degree_Celsius + 20.5 * deg_C;\nquantity_point q8 = {20.5 * deg_C, si::zeroth_degree_Celsius};\nquantity_point q9{20.5 * deg_C};\n

    In all of the above cases, we end up with the quantity_point of the same type and value.

    To play a bit more with temperatures, we can implement a simple room AC temperature controller in the following way:

    constexpr struct room_reference_temp : relative_point_origin<quantity_point{21 * deg_C}> {} room_reference_temp;\nusing room_temp = quantity_point<isq::Celsius_temperature[deg_C], room_reference_temp>;\n\nconstexpr auto step_delta = isq::Celsius_temperature(0.5 * deg_C);\nconstexpr int number_of_steps = 6;\n\nroom_temp room_ref{};\nroom_temp room_low = room_ref - number_of_steps * step_delta;\nroom_temp room_high = room_ref + number_of_steps * step_delta;\n\nstd::println(\"Room reference temperature: {} ({}, {})\\n\",\n             room_ref.quantity_from_zero(),\n             room_ref.in(usc::degree_Fahrenheit).quantity_from_zero(),\n             room_ref.in(si::kelvin).quantity_from_zero());\n\nstd::println(\"| {:<14} | {:^18} | {:^18} | {:^18} |\",\n             \"Temperature\", \"Room reference\", \"Ice point\", \"Absolute zero\");\nstd::println(\"|{0:=^16}|{0:=^20}|{0:=^20}|{0:=^20}|\", \"\");\n\nauto print = [&](std::string_view label, auto v) {\n  std::println(\"| {:<14} | {:^18} | {:^18} | {:^18{%N:.2f} %U} |\", label,\n               v - room_reference_temp, (v - si::ice_point).in(deg_C), (v - si::absolute_zero).in(deg_C));\n};\n\nprint(\"Lowest\", room_low);\nprint(\"Default\", room_ref);\nprint(\"Highest\", room_high);\n

    The above prints:

    Room reference temperature: 21 \u00b0C (69.8 \u00b0F, 294.15 K)\n\n| Temperature    |   Room reference   |     Ice point      |   Absolute zero    |\n|================|====================|====================|====================|\n| Lowest         |       -3 \u00b0C        |       18 \u00b0C        |     291.15 \u00b0C      |\n| Default        |        0 \u00b0C        |       21 \u00b0C        |     294.15 \u00b0C      |\n| Highest        |        3 \u00b0C        |       24 \u00b0C        |     297.15 \u00b0C      |\n
    "},{"location":"users_guide/framework_basics/the_affine_space/#no-text-output-for-points","title":"No text output for Points","text":"

    The library does not provide a text output for quantity points, as printing just a number and a unit is not enough to adequately describe a quantity point. Often, an additional prefix or postfix is required.

    For example, the text output of 42 m may mean many things and can also be confused with an output of a regular quantity. On the other hand, printing 42 m AMSL for altitudes above mean sea level is a much better solution, but the library does not have enough information to print it that way by itself.

    "},{"location":"users_guide/framework_basics/the_affine_space/#the-affine-space-is-about-type-safety","title":"The affine space is about type-safety","text":"

    The following operations are not allowed in the affine space:

    • adding two quantity_point objects
      • It is physically impossible to add positions of home and Denver airports.
    • subtracting a quantity_point from a quantity
      • What would it mean to subtract the DEN airport location from the distance to it?
    • multiplying/dividing a quantity_point with a scalar
      • What is the position of 2 * DEN airport location?
    • multiplying/dividing a quantity_point with a quantity
      • What would multiplying the distance with the DEN airport location mean?
    • multiplying/dividing two quantity_point objects
      • What would multiplying home and DEN airport location mean?
    • mixing quantity_points of different quantity kinds
      • It is physically impossible to subtract time from length.
    • mixing quantity_points of inconvertible quantities
      • What does subtracting a distance point to DEN airport from the Mount Everest base camp altitude mean?
    • mixing quantity_points of convertible quantities but with unrelated origins
      • How do we subtract a point on our trip to CppCon measured relatively to our home location from a point measured relative to the center of the Solar System?

    Important: The affine space improves safety

    The usage of quantity_point and affine space types, in general, improves expressiveness and type-safety of the code we write.

    "},{"location":"users_guide/framework_basics/value_conversions/","title":"Value Conversions","text":""},{"location":"users_guide/framework_basics/value_conversions/#value-preserving-conversions","title":"Value-preserving conversions","text":"
    auto q1 = 5 * km;\nstd::cout << q1.in(m) << '\\n';\nquantity<si::metre, int> q2 = q1;\n

    The second line above converts the current quantity to the one expressed in meters and prints its contents. The third line converts the quantity expressed in kilometers into the one measured in meters.

    In case a user would like to perform an opposite transformation:

    auto q1 = 5 * m;\nstd::cout << q1.in(km) << '\\n';\nquantity<si::kilo<si::metre>, int> q2 = q1;\n

    Both conversions will fail to compile.

    There are two ways to make the above work. The first solution is to use a floating-point representation type:

    auto q1 = 5. * m;\nstd::cout << q1.in(km) << '\\n';\nquantity<si::kilo<si::metre>> q2 = q1;\n

    or

    auto q1 = 5 * m;\nstd::cout << value_cast<double>(q1).in(km) << '\\n';\nquantity<si::kilo<si::metre>> q2 = q1;  // double by default\n

    Important

    The mp-units library follows std::chrono::duration logic and treats floating-point types as value-preserving.

    "},{"location":"users_guide/framework_basics/value_conversions/#value-truncating-conversions","title":"Value-truncating conversions","text":"

    The second solution is to force a truncating conversion:

    auto q1 = 5 * m;\nstd::cout << value_cast<km>(q1) << '\\n';\nquantity<si::kilo<si::metre>, int> q2 = q1.force_in(km);\n

    This explicit cast makes it clear that something unsafe is going on. It is easy to spot in code reviews or while chasing a bug in the source code.

    Note

    q.force_in(U) is just a shortcut to run value_cast<U>(q). There is no difference in behavior between those two interfaces. q.force_in(U) was added for consistency with q.in(U) and q.force_numerical_value_in(U).

    Another place where this cast is useful is when a user wants to convert a quantity with a floating-point representation to the one using an integral one. Again, this is a truncating conversion, so an explicit cast is needed:

    quantity<si::metre, int> q3 = value_cast<int>(3.14 * m);\n

    Info

    It is often OK to use an integral as a representation type, but in general, floating-point types provide better precision and are privileged in the library as they are considered to be value-preserving.

    In some cases, a unit and a representation type should be changed simultaneously. Moreover, sometimes, the order of doing those operations matters. In such cases, the library provides the value_cast<U, Rep>(q) which always returns the most precise result:

    C++23C++20Portable
    inline constexpr struct dim_currency : base_dimension<\"$\"> {} dim_currency;\ninline constexpr struct currency : quantity_spec<dim_currency> {} currency;\n\ninline constexpr struct us_dollar : named_unit<\"USD\", kind_of<currency>> {} us_dollar;\ninline constexpr struct scaled_us_dollar : named_unit<\"USD_s\", mag_power<10, -8> * us_dollar> {} scaled_us_dollar;\n\nnamespace unit_symbols {\n\ninline constexpr auto USD = us_dollar;\ninline constexpr auto USD_s = scaled_us_dollar;\n\n}  // namespace unit_symbols\n\nusing Price = quantity_point<currency[us_dollar]>;\nusing Scaled = quantity_point<currency[scaled_us_dollar], zeroth_point_origin<currency>, std::int64_t>;\n\nPrice price{12.95 * USD};\nScaled spx = value_cast<USD_s, std::int64_t>(price);\n
    inline constexpr struct dim_currency : base_dimension<\"$\"> {} dim_currency;\ninline constexpr struct currency : quantity_spec<currency, dim_currency> {} currency;\n\ninline constexpr struct us_dollar : named_unit<\"USD\", kind_of<currency>> {} us_dollar;\ninline constexpr struct scaled_us_dollar : named_unit<\"USD_s\", mag_power<10, -8> * us_dollar> {} scaled_us_dollar;\n\nnamespace unit_symbols {\n\ninline constexpr auto USD = us_dollar;\ninline constexpr auto USD_s = scaled_us_dollar;\n\n}  // namespace unit_symbols\n\nusing Price = quantity_point<currency[us_dollar]>;\nusing Scaled = quantity_point<currency[scaled_us_dollar], zeroth_point_origin<currency>, std::int64_t>;\n\nPrice price{12.95 * USD};\nScaled spx = value_cast<USD_s, std::int64_t>(price);\n
    inline constexpr struct dim_currency : base_dimension<\"$\"> {} dim_currency;\nQUANTITY_SPEC(currency, dim_currency);\n\ninline constexpr struct us_dollar : named_unit<\"USD\", kind_of<currency>> {} us_dollar;\ninline constexpr struct scaled_us_dollar : named_unit<\"USD_s\", mag_power<10, -8> * us_dollar> {} scaled_us_dollar;\n\nnamespace unit_symbols {\n\ninline constexpr auto USD = us_dollar;\ninline constexpr auto USD_s = scaled_us_dollar;\n\n}  // namespace unit_symbols\n\nusing Price = quantity_point<currency[us_dollar]>;\nusing Scaled = quantity_point<currency[scaled_us_dollar], zeroth_point_origin<currency>, std::int64_t>;\n\nPrice price{12.95 * USD};\nScaled spx = value_cast<USD_s, std::int64_t>(price);\n
    "},{"location":"users_guide/use_cases/interoperability_with_other_libraries/","title":"Interoperability with Other Libraries","text":"

    mp-units makes it easy to cooperate with similar entities of other libraries. No matter if we want to provide interoperability with a simple home-grown strongly typed wrapper type (e.g., Meter, Timestamp, ...) or with a feature-rich quantities and units library, we have to provide specializations of:

    • a quantity_like_traits for external quantity-like type,
    • a quantity_point_like_traits for external quantity_point-like type.
    "},{"location":"users_guide/use_cases/interoperability_with_other_libraries/#specifying-a-conversion-kind","title":"Specifying a conversion kind","text":"

    Before we delve into the template specialization details, let's first decide if we want the conversions to happen implicitly or if explicit ones would be a better choice. Or maybe the conversion should be implicit in one direction only (e.g., into mp-units abstractions) while the explicit conversions in the other direction should be preferred?

    There is no one unified answer to the above questions. Everything depends on the use case.

    Typically, the implicit conversions are allowed in cases where:

    • both abstractions mean exactly the same, and interchanging them in the code should not change its logic,
    • there is no significant runtime overhead introduced by such a conversion (e.g., no need for dynamic allocation or copying of huge internal buffers),
    • the target type of the conversion provides the same or better safety to the users,
    • we prefer the simplicity of implicit conversions over safety during the (hopefully short) transition period of refactoring our code base from the usage of one library to the other.

    In all other scenarios, we should probably enforce explicit conversions.

    The kinds of inter-library conversions can be easily configured in partial specializations of conversion traits in the mp-units library. To require an explicit conversion, the return type of the conversion function should be wrapped in convert_explicitly<T>. Otherwise, convert_implicitly<T> should be used.

    "},{"location":"users_guide/use_cases/interoperability_with_other_libraries/#quantities-conversions","title":"Quantities conversions","text":"

    For example, let's assume that some company has its own Meter strong-type wrapper:

    struct Meter {\n  int value;\n};\n

    As every usage of Meter is at least as good and safe as the usage of quantity<si::metre, int>, and as there is no significant runtime performance penalty, we would like to allow the conversion to mp_units::quantity to happen implicitly.

    On the other hand, the quantity type is much safer than the Meter, and that is why we would prefer to see the opposite conversions stated explicitly in our code.

    To enable such interoperability, we must define a partial specialization of the quantity_like_traits<T> type trait. Such specialization should provide:

    • static data member reference that provides the quantity reference (e.g., unit),
    • rep type that specifies the underlying storage type,
    • to_numerical_value(T) static member function returning a quantity's raw value of rep type packed in either convert_explicitly or convert_implicitly wrapper.
    • from_numerical_value(rep) static member function returning T packed in either convert_explicitly or convert_implicitly wrapper.

    For example, for our Meter type, we could provide the following:

    template<>\nstruct mp_units::quantity_like_traits<Meter> {\n  static constexpr auto reference = si::metre;\n  using rep = decltype(Meter::value);\n  static constexpr convert_implicitly<rep> to_numerical_value(Meter m) { return m.value; }\n  static constexpr convert_explicitly<Meter> from_numerical_value(rep v) { return Meter{v}; }\n};\n

    After that, we can check that the QuantityLike concept is satisfied:

    static_assert(mp_units::QuantityLike<Meter>);\n

    and we can write the following:

    void print(Meter m) { std::cout << m.value << \" m\\n\"; }\n\nint main()\n{\n  using namespace mp_units;\n  using namespace mp_units::si::unit_symbols;\n\n  Meter height{42};\n\n  // implicit conversions\n  quantity h1 = height;\n  quantity<isq::height[m], int> h2 = height;\n\n  std::cout << h1 << \"\\n\";\n  std::cout << h2 << \"\\n\";\n\n  // explicit conversions\n  print(Meter(h1));\n  print(Meter(h2));\n}\n

    Note

    No matter if we decide to use implicit or explicit conversions, the mp-units will not allow unsafe operations to happen.

    If we extend the above example with unsafe conversions, the code will not compile, and we will have to fix the issues first before the conversion may be performed:

    UnsafeFixed
    quantity<isq::height[m]> h3 = height;\nquantity<isq::height[mm], int> h4 = height;\nquantity<isq::height[km], int> h5 = height;  // Compile-time error (1)\n\nstd::cout << h3 << \"\\n\";\nstd::cout << h4 << \"\\n\";\nstd::cout << h5 << \"\\n\";\n\nprint(Meter(h3));                            // Compile-time error (2)\nprint(Meter(h4));                            // Compile-time error (3)\nprint(Meter(h5));\n
    1. Truncation of value while converting from meters to kilometers.
    2. Conversion of double to int is not value-preserving.
    3. Truncation of value while converting from millimeters to meters.
    quantity<isq::height[m]> h3 = height;\nquantity<isq::height[mm], int> h4 = height;\nquantity<isq::height[km], int> h5 = quantity{height}.force_in(km);\n\nstd::cout << h3 << \"\\n\";\nstd::cout << h4 << \"\\n\";\nstd::cout << h5 << \"\\n\";\n\nprint(Meter(value_cast<int>(h3)));\nprint(Meter(h4.force_in(m)));\nprint(Meter(h5));\n
    "},{"location":"users_guide/use_cases/interoperability_with_other_libraries/#quantity-points-conversions","title":"Quantity points conversions","text":"

    To play with quantity point conversions, let's assume that we have a Timestamp strong type in our codebase, and we would like to start using mp-units to work with this abstraction.

    struct Timestamp {\n  int seconds;\n};\n

    As we described in The Affine Space chapter, timestamps should be modeled as quantity points rather than regular quantities.

    To allow the conversion between our custom Timestamp type and the quantity_point class template we need to provide the following in the partial specialization of the quantity_point_like_traits<T> type trait:

    • static data member reference that provides the quantity point reference (e.g., unit),
    • static data member point_origin that specifies the absolute point, which is the beginning of our measurement scale for our points,
    • rep type that specifies the underlying storage type,
    • to_quantity(T) static member function returning the quantity being the offset of the point from the origin packed in either convert_explicitly or convert_implicitly wrapper,
    • from_quantity(quantity<reference, rep>) static member function returning T packed in either convert_explicitly or convert_implicitly wrapper.

    For example, for our Timestamp type, we could provide the following:

    template<>\nstruct mp_units::quantity_point_like_traits<Timestamp> {\n  static constexpr auto reference = si::second;\n  static constexpr auto point_origin = default_point_origin(reference);\n  using rep = decltype(Timestamp::seconds);\n\n  static constexpr convert_implicitly<quantity<reference, rep>> to_quantity(Timestamp ts)\n  {\n    return ts.seconds * si::second;\n  }\n\n  static constexpr convert_explicitly<Timestamp> from_quantity(quantity<reference, rep> q)\n  {\n    return Timestamp(q.numerical_value_ref_in(si::second));\n  }\n};\n

    After that, we can check that the QuantityPointLike concept is satisfied:

    static_assert(mp_units::QuantityPointLike<Timestamp>);\n

    and we can write the following:

    void print(Timestamp ts) { std::cout << ts.seconds << \" s\\n\"; }\n\nint main()\n{\n  using namespace mp_units;\n  using namespace mp_units::si::unit_symbols;\n\n  Timestamp ts{42};\n\n  // implicit conversion\n  quantity_point qp = ts;\n\n  std::cout << qp.quantity_from_zero() << \"\\n\";\n\n  // explicit conversion\n  print(Timestamp(qp));\n}\n
    "},{"location":"users_guide/use_cases/interoperability_with_other_libraries/#interoperability-with-the-c-standard-library","title":"Interoperability with the C++ Standard Library","text":"

    In the C++ standard library, we have two types that handle quantities and model the affine space. Those are:

    • std::chrono::duration - specifies quantities of time,
    • std::chrono::time_point - specifies quantity points of time.

    The mp-units library comes with built-in interoperability with those types. It is enough to include the mp-units/systems/si/chrono.h file to benefit from it. This file provides:

    • partial specializations of quantity_like_traits and quantity_point_like_traits that provide support for implicit conversions between std and mp_units types in both directions,
    • chrono_point_origin<Clock> point origin for std clocks,
    • to_chrono_duration and to_chrono_time_point dedicated conversion functions that result in types exactly representing mp-units abstractions.

    Important

    Only a quantity_point that uses chrono_point_origin<Clock> as its origin can be converted to the std::chrono abstractions:

    inline constexpr struct ts_origin : relative_point_origin<chrono_point_origin<system_clock> + 1 * h> {} ts_origin;\ninline constexpr struct my_origin : absolute_point_origin<my_origin, isq::time> {} my_origin;\n\nquantity_point qp1 = sys_seconds{1s};\nauto tp1 = to_chrono_time_point(qp1);  // OK\n\nquantity_point qp2 = chrono_point_origin<system_clock> + 1 * s;\nauto tp2 = to_chrono_time_point(qp2);  // OK\n\nquantity_point qp3 = ts_origin + 1 * s;\nauto tp3 = to_chrono_time_point(qp3);  // OK\n\nquantity_point qp4 = my_origin + 1 * s;\nauto tp4 = to_chrono_time_point(qp4);  // Compile-time Error (1)\n\nquantity_point qp5{1 * s};\nauto tp5 = to_chrono_time_point(qp5);  // Compile-time Error (2)\n
    1. my_origin is not defined in terms of chrono_point_origin<Clock>.
    2. zeroth_point_origin is not defined in terms of chrono_point_origin<Clock>.

    Here is an example of how interoperability described in this chapter can be used in practice:

    using namespace std::chrono;\n\nsys_seconds ts_now = floor<seconds>(system_clock::now());\n\nquantity_point start_time = ts_now;\nquantity speed = 925. * km / h;\nquantity distance = 8111. * km;\nquantity flight_time = distance / speed;\nquantity_point exp_end_time = start_time + flight_time;\n\nsys_seconds ts_end = value_cast<int>(exp_end_time.in(s));\n\nauto curr_time = zoned_time(current_zone(), ts_now);\nauto mst_time = zoned_time(\"America/Denver\", ts_end);\n\nstd::cout << \"Takeoff: \" << curr_time << \"\\n\";\nstd::cout << \"Landing: \" << mst_time << \"\\n\";\n

    The above may print the following output:

    Takeoff: 2023-11-18 13:20:54 UTC\nLanding: 2023-11-18 15:07:01 MST\n
    "},{"location":"users_guide/use_cases/wide_compatibility/","title":"Wide Compatibility","text":"

    The mp-units allows us to implement nice and terse code targeting a specific C++ version and configuration. Such code is easy to write and understand but might not be portable to some older environments.

    However, sometimes, we want to develop code that can be compiled on a wide range of various compilers and configurations. This is why the library also exposes and uses special preprocessor macros that can be used to ensure the wide compatibility of our code.

    Note

    Those macros are used in our short example applications as those are meant to be built on all of the supported compilers. Some still do not support std::format, C++ modules, or C++ versions newer than C++20.

    "},{"location":"users_guide/use_cases/wide_compatibility/#various-compatibility-options","title":"Various compatibility options","text":"

    Depending on your compiler's conformance, you can choose to use any of the below styles to write your code using mp-units:

    C++23C++20C++20 with header filesC++20 with header files + libfmtWide Compatibility
    #include <format>\n#include <iostream>\nimport mp_units;\n\n// ...\n\ninline constexpr struct horizontal_length : quantity_spec<isq::length> {} horizontal_length;\n\n// ...\n\nstd::cout << std::format(...) << \"\\n\";\n
    #include <format>\n#include <iostream>\nimport mp_units;\n\n// ...\n\ninline constexpr struct horizontal_length : quantity_spec<horizontal_length, isq::length> {} horizontal_length;\n\n// ...\n\nstd::cout << std::format(...) << \"\\n\";\n
    #include <mp-units/format.h>\n#include <mp-units/ostream.h>\n#include <mp-units/systems/international/international.h>\n#include <mp-units/systems/isq/isq.h>\n#include <mp-units/systems/si/si.h>\n#include <format>\n#include <iostream>\n\n// ...\n\ninline constexpr struct horizontal_length : quantity_spec<horizontal_length, isq::length> {} horizontal_length;\n\n// ...\n\nstd::cout << std::format(...) << \"\\n\";\n
    #include <mp-units/format.h>\n#include <mp-units/ostream.h>\n#include <mp-units/systems/international/international.h>\n#include <mp-units/systems/isq/isq.h>\n#include <mp-units/systems/si/si.h>\n#include <fmt/format.h>\n#include <iostream>\n\n// ...\n\ninline constexpr struct horizontal_length : quantity_spec<horizontal_length, isq::length> {} horizontal_length;\n\n// ...\n\nstd::cout << fmt::format(...) << \"\\n\";\n
    #include <iostream>\n#ifdef MP_UNITS_MODULES\n#include <mp-units/compat_macros.h>\nimport mp_units;\n#else\n#include <mp-units/format.h>\n#include <mp-units/ostream.h>\n#include <mp-units/systems/international/international.h>\n#include <mp-units/systems/isq/isq.h>\n#include <mp-units/systems/si/si.h>\n#endif\n\n// ...\n\nQUANTITY_SPEC(horizontal_length, isq::length);\n\n// ...\n\nstd::cout << MP_UNITS_STD_FMT::format(...) << \"\\n\";\n

    Tip

    Depending on your preferences, you can either write:

    • terse code directly targeting your specific compiler's abilities,
    • verbose code using preprocessor branches and macros that provide the widest compatibility across various compilers.
    "},{"location":"users_guide/use_cases/wide_compatibility/#compatibility-macros","title":"Compatibility macros","text":"

    This chapter describes only the most essential tools the mp-units users need. All the compatibility macros can be found in the mp-units/compat_macros.h header file.

    Tip

    The mp-units/compat_macros.h header file is implicitly included when we use \"legacy\" headers in our translation units. However, it has to be explicitly included when we use C++20 modules, as those do not propagate preprocessor macros.

    "},{"location":"users_guide/use_cases/wide_compatibility/#QUANTITY_SPEC","title":"QUANTITY_SPEC(name, ...)","text":"

    Quantity specification definitions benefit from an explicit object parameter added in C++23 to remove the need for CRTP idiom, which significantly simplifies the code.

    This macro benefits from the new C++23 feature if available. Otherwise, it uses the CRTP idiom under the hood.

    "},{"location":"users_guide/use_cases/wide_compatibility/#mp_units_std_fmt","title":"MP_UNITS_STD_FMT","text":"

    Some of the supported compilers do not support std::format and related tools. Also, despite using a conformant compiler, some projects still choose to use fmtlib as their primary formatting facility (e.g., to benefit from additional features provided with the library).

    This macro resolves to either the std or fmt namespace, depending on the value of MP_UNITS_USE_FMTLIB CMake option.

    "},{"location":"users_guide/use_cases/working_with_legacy_interfaces/","title":"Working with Legacy interfaces","text":"

    In case we are working with a legacy/unsafe interface, we may need to extract the numerical value of a quantity and pass it to some third-party legacy unsafe interfaces.

    In such situations we can use .numerical_value_in(Unit) member function:

    void legacy_check_speed_limit(int speed_in_km_per_h);\n
    legacy_check_speed_limit((180 * km / (2 * h)).numerical_value_in(km / h));\n

    Such a getter will explicitly enforce the usage of a correct unit required by the underlying interface, which reduces a significant number of safety-related issues.

    The above code will not compile in case value truncation may happen. To solve the issue, we need to either use a value-preserving representation type or force the truncating conversion with .force_numerical_value_in(Unit):

    legacy_check_speed_limit((140 * mi / (2 * h)).force_numerical_value_in(km / h));\n

    The getters mentioned above always return by value as a quantity value conversion may be required to adjust it to the target unit. In case a user needs a reference to the underlying storage .numerical_value_ref_in(Unit) should be used:

    void legacy_set_speed_limit(int* speed_in_km_per_h) { *speed_in_km_per_h = 100; }\n
    quantity<km / h, int> speed_limit;\nlegacy_set_speed_limit(&speed_limit.numerical_value_ref_in(km / h));\n

    This member function again requires a target unit to enforce safety. This overload does not participate in overload resolution if the provided unit has a different scaling factor than the current one.

    "},{"location":"blog/archive/2023/","title":"2023","text":""},{"location":"blog/category/releases/","title":"Releases","text":""},{"location":"blog/category/wg21/","title":"WG21","text":""},{"location":"users_guide/examples/tags_index/","title":"Tags Index","text":"

    Note

    mp-units usage example applications are meant to be built on all of the supported compilers. This is why they benefit from the Wide Compatibility mode.

    Tip

    All usage examples in this chapter are categorized with appropriate tags to simplify navigation and search of relevant code. You can either read all the examples one-by-one in the order provided by the documentation authors or, thanks to the tagging system, jump straight to the example that is the most interesting for you.

    "},{"location":"users_guide/examples/tags_index/#cgs-system","title":"CGS System","text":"
    • avg_speed
    "},{"location":"users_guide/examples/tags_index/#international-system","title":"International System","text":"
    • avg_speed
    • hello_units
    "},{"location":"users_guide/examples/tags_index/#physical-constants","title":"Physical Constants","text":"
    • si_constants
    "},{"location":"users_guide/examples/tags_index/#text-formatting","title":"Text Formatting","text":"
    • avg_speed
    • hello_units
    • si_constants
    "}]} \ No newline at end of file +{"config":{"lang":["en"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"]},"docs":[{"location":"","title":"Welcome to mp-units!","text":"

    mp-units is a compile-time enabled feature-rich Modern C++ modular/header-only library that provides compile-time dimensional analysis and unit/quantity manipulation. Its key strengths include safety, performance, and developer experience.

    The library source code is hosted on GitHub with a permissive MIT license.

    Supported compilers

    This library tries to provide the best user experience possible with the C++ language. To achieve that, it extensively uses C++20 features and the explicit object parameter from C++23.

    Even though the library benefits from C++23 (if available), C++20 is enough to compile and use all of the library's functionality. C++23 features are hidden behind a preprocessor macro providing a backward-compatible way to use it.

    The below table provides the minimum compiler version required to compile the code using the specific feature:

    Feature gcc clang apple-clang MSVC Minimum support 12 16 15 None std::format 13 17 None None C++ modules 14 17 None None C++23 extensions 14 18 None None

    More requirements for C++ modules support can be found in the CMake's documentation.

    C++ modulesHeader files
    #include <iostream>\n#include <print>\nimport mp_units;\n\nusing namespace mp_units;\n\ninline constexpr struct smoot : named_unit<\"smoot\", mag<67> * usc::inch> {} smoot;\n\nint main()\n{\n  constexpr quantity dist = 364.4 * smoot;\n  std::println(\"Harvard Bridge length = {:{%N:.5} %U} ({:{%N:.5} %U}, {:{%N:.5} %U}) \u00b1 1 \u03b5ar\",\n               dist, dist.in(usc::foot), dist.in(si::metre));\n}\n
    #include <mp-units/format.h>\n#include <mp-units/systems/si/si.h>\n#include <mp-units/systems/usc/usc.h>\n#include <print>\n\nusing namespace mp_units;\n\ninline constexpr struct smoot : named_unit<\"smoot\", mag<67> * usc::inch> {} smoot;\n\nint main()\n{\n  constexpr quantity dist = 364.4 * smoot;\n  std::println(\"Harvard Bridge length = {:{%N:.5} %U} ({:{%N:.5} %U}, {:{%N:.5} %U}) \u00b1 1 \u03b5ar\",\n               dist, dist.in(usc::foot), dist.in(si::metre));\n}\n

    Output:

    Harvard Bridge length = 364.4 smoot (2034.6 ft, 620.14 m) \u00b1 1 \u03b5ar\n

    Try it on Compiler Explorer

    What is smoot?

    The smoot (/\u02c8smu\u02d0t/) is a nonstandard unit of length created as part of an MIT fraternity prank. It is named after Oliver R. Smoot, a fraternity pledge to Lambda Chi Alpha, who, in October 1958, lay on the Harvard Bridge (between Boston and Cambridge, Massachusetts) and was used by his fraternity brothers to measure the length of the bridge.

    One smoot equals Oliver Smoot's height at the time of the prank (five feet and seven inches). The bridge's length was measured to be 364.4 smoots plus or minus one ear, with the \"plus or minus\" intended to express the measurement uncertainty.

    Oliver Smoot graduated from MIT with the class of 1962, became a lawyer, and later became chairman of the American National Standards Institute (ANSI) and president of the International Organization for Standardization (ISO).

    More on the smoot unit of length can be found at https://en.wikipedia.org/wiki/Smoot.

    Important: Help needed!

    The mp-units library might be the subject of ISO standardization for C++29. More on this can be found in the following ISO C++ proposals:

    • P1935: A C++ Approach to Physical Units,
    • P2980: A motivation, scope, and plan for a physical quantities and units library,
    • P2981: Improving our safety with a physical quantities and units library,
    • P2982: std::quantity as a numeric type.

    We are actively looking for parties interested in field-trialing the library.

    "},{"location":"release_notes/","title":"Release Notes","text":""},{"location":"release_notes/#mp-units","title":"mp-units","text":""},{"location":"release_notes/#2.2.0","title":"2.2.0 WIP","text":"
    • (!) feat: C++ modules support added by @JohelEGP
    • (!) feat: formatting grammar improved and units formatting support added
    • (!) feat: has_unit_symbol support removed
    • (!) feat: ABI concerns resolved with introduction of u8 strings for symbols
    • feat: implicit point origins support added
    • feat: unit default point origin support added
    • feat: fma, isfinite, isinf, and isnan math function added by @NAThompson
    • feat: quantity_point support added for quantity_cast and value_cast
    • feat: value_cast<Unit, Representation> added
    • feat: interconvertible(QuantitySpec, QuantitySpec) added
    • feat: qp.quantity_from_zero() added
    • feat: underlying_type type trait added
    • feat: do not print space between a number and percent or per_mille
    • feat: ppm parts per million added by @nebkat
    • feat: atan2 2-argument arctangent added by @nebkat
    • feat: fmod floating-point division remainder added by @nebkat
    • feat: std::format support added
    • feat: unit text output support added
    • feat: formatting error messages improved
    • feat: improve types readability by eliminating extraneous () in references, prefixes, and kind_of
    • (!) refactor: zero_Fahrenheit renamed to zeroth_degree_Fahrenheit
    • (!) refactor: SI-related trigonometric functions moved to the si subnamespace
    • (!) refactor: math.h header file broke up to smaller pieces
    • (!) refactor: fixed_string interface refactored
    • (!) refactor: ReferenceOf does not take a dimension anymore
    • (!) refactor: 'o' replaced with '1' as a modifier for unit_symbol_solidus::one_denominator
    • (!) refactor: get_kind() now returns kind_of
    • refactor: math functions constraints refactored
    • refactor: si_quantities.h added to improve compile-times
    • refactor: validate_ascii_string refactored to is_basic_literal_character_set
    • fix: QuantityLike conversions required Q::rep instead of using one provided by quantity_like_traits
    • fix: QuantitySpec[Unit] replaced with make_reference in value_cast
    • fix: ice_point is now defined with the integral offset from absolute_zero
    • fix: performance regression in sudo_cast fixed
    • fix: explicit object parameter support fixed
    • fix: missing version header file added to hacks.h
    • docs: project blog and first posts added
    • docs: project documentation layout refactored
    • docs: \"Interoperability with Other Libraries\" chapter added
    • docs: \"Framework Basics\" chapters updated and cleaned up
    • docs: smoot unit example added to the main page
    • docs: \"Code Example\" chapter renamed to \"Look and Feel\" and reordered in TOC to be after \"Quick Start\"
    • docs: \"Quick Start\" chapter reworked to be simpler and include quantity points
    • docs: \"Quantity points\" chapter extended
    • docs: \"The Affine Space\" chapter updated to reflect the recent design changes
    • docs: \"Working with Legacy interfaces\" chapter added
    • docs: \"Text Output\" chapter updated
    • docs: mkdocs social plugin enabled
    • docs: project logo and custom color scheme added
    • docs: minimum compiler requirements updated
    • docs: Cairo dependency described in the MkDocs section
    • (!) build: Conan and CMake options refactored
    • (!) build: MP_UNITS_AS_SYSTEM_HEADERS support removed
    • build: gsl-lite updated to 0.41.0
    • build: catch2 updated to 3.5.1
    • build: fmt updated to 10.2.1
    • build: gitpod environment updated
    • build: check_cxx_feature_supported added
    • build(conan): generate() now set cache_variables
    • build(conan): can_run check added before running tests
    "},{"location":"release_notes/#2.1.0","title":"2.1.0 December 9, 2023","text":"
    • (!) feat: inverse() support added for dimensions, quantity_spec, units, and references (1 / s will now create quantity and not a Unit)
    • (!) feat: quantity_point does not provide zero() anymore
    • (!) feat: quantity_spec and its kind should not compare equal
    • (!) feat: mutating interface removed from fixed_string
    • (!) feat: common_type with a raw value is not needed anymore as for a long time now raw values are not convertible to the dimensionless quantities
    • (!) feat: symbol_text definition simplified
    • (!) feat: users are now allowed to inherit their own types from absolute point origins
    • (!) feat: interoperability with other libraries redesigned
    • feat: basic_fixed_string(const CharT*, std::integral_constant<std::size_t, N>) constructor added
    • feat: isq::activity added and becquerel definition updated to benefit from it
    • feat: gray and sievert now have correct associated quantity kinds
    • feat: UnitCompatibleWith concept added and applied to in(U) and force_in(U) functions
    • feat: quantities can now be multiplied and divided by units (no parenthesis needed anymore)
    • feat: Magnitude / Unit operator added
    • feat: equality for dimensions now will allow derived classes as well (but not from derived_dimension)
    • feat: zero_Fahrenheit point origin added
    • feat: equivalent point origins handling improved
    • feat(example): unit symbols added to the currency example
    • (!) refactor: unit_symbol<fmt>(U) signature refactored and the resulting text can now also be used at runtime
    • (!) refactor: make_xxx factory functions replaced with two-parameter constructors
    • (!) refactor: unit_symbol changed to consteval
    • refactor: in(U) and force_in(U) now return auto to provide better diagnostics on clang
    • refactor: quantity operators constraints refactored
    • refactor: more type members added to fixed_string definition
    • refactor: unit_symbol_formatting enums now use std::int8_t as a representation type
    • fix: symbols of named dimensionless units with the ratio = 1 were not printed
    • fix: iterator is now properly updated for all cases in unit_symbol
    • fix: Fahrenheit conversion ratio was inverted
    • fix: CommonlyInvocableQuantities was overconstrained for the current library design
    • fix: are_ingredients_convertible now mandates explicit conversion for To dimensionless quantities
    • fix: quantity_point::point_for(PO) constraints fixed
    • fix(example): latitude and longitude fixed to include 0 for N and E respectively
    • ci: clang-17 enabled
    • ci: apple-clang-15 enabled
    • ci: Added C++23 builds to the CI matrix
    • docs: \"Getting Started\" chapters updated
    • docs: \"Basic Concepts\" and \"Interface Introduction\" chapters updated
    • docs: \"Design Overview\" chapter added and \"Concepts\" chapter reworked
    • docs: \"Output stream formatting\" chapter updated
    • docs: \"Default formatting\" chapter updated
    • docs: \"Derived unit symbols generation\" chapter added
    • docs: outdated affine space chapter updated
    • docs: CameCase concept identifiers FAQ added
    • docs: gravitational_potential_energy equation fixed on a graph
    • docs: YouTube video link updated to the C++ on Sea 2023
    • docs: ISO papers reference added to docs and README
    • docs: a representation type in a dimensionless quantity FAQ fixed
    • docs: titles added to some important admonitions
    • docs: \"Terms and Definitions\" slightly updated
    • docs: \"canonical unit\" added to glossary and its documentation in code was updated
    • docs: Design overview graph updated
    "},{"location":"release_notes/#2.0.0","title":"2.0.0 September 24, 2023","text":"
    • units namespace renamed to mp_units (#317)
    • header files in the <mp-units/...> rather then in <units/...> (#317)
    • the downcasting facility is removed (#383, #211, #32)
    • unified and simplified quantity creation (#274)
    • determining the best way to create a quantity (#413)
    • V2 quantity_point (#414)
    • introduction of quantity_spec to store not only dimension but also additional information about quantities (#405)
    • quantity now takes reference object, which aggregates quantity_spec and a unit and a representation type
    • units, prefixes, dimensions, quantity specifications, and references are passed as NTTPs to templates and provide arithmetic operations and comparison
    • expression templates consistently used in all derived types to increase the readability (#351, #166)
    • derived dimensions are now factors of only base dimensions (#281)
    • convertibility of derived quantities (#427)
    • dimensions, quantity specifications, units, and references are now composable, significantly reducing the number of definitions and resulting types
    • heavily simplified unit systems definitions (no need to define unnamed derived units, systems-specific dimensions, aliases for quantities, concepts, UDLs, ... anymore)
    • improved definition of all systems
    • support for ISO 80000 Part 3-6 quantities
    • faster than lightspeed constants (#169)
    • extensions to quantity formatting with fmt
    • quantity_kind removed
    • improved casting of unit with .in(Unit), .force_in(Unit) for quantity and quantity_spec
    • numerical value accessor safety improved with .numerical_value_in(Unit) and .force_numerical_value_in(Unit)
    • quantity can no longer be constructed with a raw value (#434)
    • Implicit construction of quantities from a value (#410)
    • quantity_point can no longer be constructed with just a quantity and an explicit PointOrigin is always needed
    • ceil and floor are dangerous (#432)
    • quecto, ronto, ronna, quetta new SI prefixes support
    • comparison against zero added (#487)
    • documentation rewritten from scratch
    • many smaller changes not possible to address with the previous design (#205, #210, #134)
    "},{"location":"release_notes/#0.8.0","title":"0.8.0 June 14, 2023","text":"
    • (!) refactor: common_quantity, common_quantity_for, common_quantity_point, common_quantity_kind, and common_quantity_point_kind removed
    • (!) refactor: named_derived_unit removed as it was not used
    • (!) refactor: derived_unit renamed to derived_scaled_unit
    • (!) refactor: unit renamed to derived_unit
    • (!) refactor: U::is_named removed from the unit types and replaced with NamedUnit concept
    • (!) refactor: PrefixFamily support removed
    • (!) refactor: mi(naut) renamed to nmi
    • (!) refactor: knot unit helper renamed to kn in FPS
    • (!) refactor: knot text symbol changed from \"knot\" to \"kn\"
    • refactor: quantity op+() and op-() reimplemented in terms of reference rather then quantity types
    • refactor(example): glide_computer now use dimensionless quantities with ranged_representation as rep
    • feat: HEP system support added (thanks @RalphSteinhagen)
    • feat: floor(), ceil(), and round() support added (thanks @hofbi)
    • feat: std::format support for compliant compilers added
    • feat: conversion helpers from mp-units to std::chrono types added
    • feat: math functions can now be safely used with user-defined types
    • feat: conversion from quantity_point to std::chrono::time_point added
    • feat: nautical_mile_per_hour and knot added to si::international system
    • (!) fix: add quantity_point::origin, like std::chrono::time_point::clock
    • fix: enable any prefixes for most of the named units (beside those that use prefixes already)
    • fix: hectare definition fixed to be a prefixed version of are + other units
    • fix: account for different dimensions in quantity_point_cast's constraint
    • fix: output stream operator now properly handles state
    • fix: fmt algorithms were overconstrained with forward_iterator
    • fix: CTAD for aliases fixed
    • fix: derived_ratio calculation
    • fix: fill_t assignment operator fixed
    • fix: improve downcast mode off
    • fix: radioactivity header compilation fixed
    • fix: si::hep::dim_momentum duplicated definition fixed
    • fix: fps can now coexist with international system
    • fix: public headers fixed to be standalone
    • test: standalone public headers tests added
    • (!) build: CMake generator in Conan is no longer obtained from an environment variable
    • (!) build: Required Conan version bumped to 1.48
    • (!) build: Conan 1.48 does not set CMAKE_BUILD_TYPE in the conan_toolchain.cmake anymore
    • build: AppleClang 13 support added (thanks @fdischner)
    • build: most of the conanfile.py refactored to be Conan 2.0 ready
    • build: validate() replaced with configure() to raise errors during conan install in Conan 1.X
    • build: minimum Conan version changed to 1.40
    • build: linear-algebra Conan repo is no needed anymore
    • build: Gitpod support added
    • build: clang-format-15 support added
    • build: export config to local build (#322)
    • build: fix export name of mp-units-system
    • build: fmt updated to 8.0.1
    • build: gsl-lite updated to 0.40.0
    • build: catch2 updated to 2.13.9
    • build: doxygen updated to 1.9.4
    • build: linear_algebra/0.7.0 switched to wg21-linear_algebra/0.7.2
    • ci: VS2022, gcc-11, clang-13, clang-14, and AppleClang 13 support added
    • ci: pre-commit support added (thanks @hofbi)
    • docs: Project documentation updated
    • docs: CITATION.cff file added
    • docs: CONTRIBUTING.md updated
    "},{"location":"release_notes/#0.7.0","title":"0.7.0 May 11, 2021","text":"
    • (!) refactor: ScalableNumber renamed to Representation
    • (!) refactor: output stream operators moved to the units/quantity_io.h header file
    • (!) refactor: Refactored the library file tree
    • (!) refactor: quantity::count() renamed to quantity::number()
    • (!) refactor: data system renamed to isq::iec80000 (quantity names renamed too)
    • (!) refactor: *deduced_unit renamed to *derived_unit
    • (!) refactor: got rid of a noble_derived_unit
    • refactor: quantity (kind) point updated to reflect latest changes to quantity
    • refactor: basic concepts, quantity and quantity_cast refactored
    • refactor: abs() definition refactored to be more explicit about the return type
    • feat: quantity (point) kind support added (thanks @johelegp)
    • feat: quantity references support added (thanks @johelegp)
    • feat: quantity aliases support addded
    • feat: interoperability with std::chrono::duration and other units libraries
    • feat: CTAD for dimensionless quantity added
    • feat: modulation_rate support added (thanks @go2sh)
    • feat: SI prefixes for isq::iec80000 support added (thanks @go2sh)
    • feat: a possibility to disable quantity UDLs support with UNITS_NO_LITERALS preprocessor define
    • feat: a support to define ISQ derived dimensions in terms of different number or order of components
    • perf: preconditions check do not influence the runtime performance of a Release build
    • perf: quantity_cast() generates less assembly instructions
    • perf: temporary string creation removed from quantity::op<<()
    • perf: value initialization for quantity value removed (left with a default initialization)
    • perf: limited the equivalent trait usage
    • perf: limited the C++ Standard Library headers usage
    • perf: rvalue references support added for constructors and getters
    • (!) fix: exp() has sense only for dimensionless quantities
    • (!) fix: dim_torque now properly divides by an angle (instead of multiply) + default unit name change
    • fix: quantity's operators fixed to behave like the underlying types do
    • fix: quantity_cast() fixed to work correctly with representation types not convertible from std::intmax_t
    • fix: ambiguous case for empty type list resolved
    • fix: downcasting facility for non-default-constructible types
    • fix: restore user-warnings within the library implementation
    • fix: the text symbol of foot_pound_force and foot_pound_force_per_second
    • fix: quantity modulo arithmetics fixed
    • (!) build: Conan testing version is now hosted on Artifactory
    • (!) build: Linear Algebra is now hosted on its Artifactory
    • (!) build: BUILD_DOCS CMake option renamed to UNITS_BUILD_DOCS
    • build: doxygen updated to 1.8.20
    • build: catch2 updated to 2.13.4
    • build: fmt updated to 7.1.3
    • build: ms-gsl replaced with gsl-lite/0.38.0
    • build: Conan generator switched to cmake_find_package_multi
    • build: Conan CMakeToolchain support added
    • build: CMake scripts cleanup
    • build: ccache support added
    • ci: CI switched from Travis CI to GitHub Actions
    "},{"location":"release_notes/#0.6.0","title":"0.6.0 September 13, 2020","text":"
    • feat: quantity_point support added (thanks @johelegp)
    • feat: Added angle as SI base dimension (thanks @kwikius)
    • feat: si::angular_velocity support added (thanks @mikeford3)
    • feat: FPS system added (thanks @mikeford3)
    • feat: Added support for mathematical function exp(quantity)
    • feat: Localization support for text output added (thanks @rbrugo)
    • feat: Added STL random number distribution wrappers (thanks @yasamoka)
    • (!) refactor: Refactored and cleaned up the library file tree
    • (!) refactor: q_* UDL renamed to _q_*
    • (!) refactor: UDLs with \"per\" in name renamed from *p* to *_per_*
    • (!) refactor: ratio changed to the NTTP kind
    • (!) refactor: exp and Exp renamed to exponent and Exponent
    • (!) refactor: Scalar concept renamed to ScalableNumber
    • (!) refactor: Dimensionless quantities redesigned to be of a quantity type
    • refactor: math.h function signatures refactored to use a Quantity concept (thanks @kwikius)
    • refactor: [[nodiscard]] added to many functions
    • fix: si::day unit symbol fixed to d (thanks @komputerwiz)
    • fix: si::mole unit symbol fixed to mol (thanks @mikeford3)
    • (!) build: gcc-9 is no longer supported (at least gcc-10 is required)
    • build: Visual Studio 16.7 support added
    • build: linear_algebra updated to 0.7.0/stable
    • build: fmt updated to 7.0.3
    • build: range-v3 updated to 0.11.0
    • build: catch2 updated to 2.13.0
    • build: doxygen updated to 1.8.18
    • build: ms-gsl 3.1.0 dependency added
    • build: Removed the dependency on a git submodule with common CMake scripts
    "},{"location":"release_notes/#0.5.0","title":"0.5.0 May 17, 2020","text":"
    • Major refactoring and rewrite of the library
    • Units are now independent from dimensions
    • Dimensions now depend on units (base or coherent units are provided in a class template)
    • Quantity gets a Dimension template parameter again (as unit does not provide information about its dimension anymore)
    • Spaceship operator support added
    • Added official CGS system support
    • Added official data information system support
    • Repository file tree cleanup
    • ratio refactored to contain Exp template parameter (thanks a lot @oschonrock!)
    • SI fundamental constants added
    • q_ prefix applied to all the UDLs (thanks @kwikius)
    • unknown_unit renamed to unknown_coherent_unit
    • Project documentation greatly extended and switched to Sphinx
    • A few more usage examples added
    • ASCII-only output support added (thanks @yasamoka)
    • Representation values formatting extended (thanks @rbrugo)
    • Output streams formatting support added
    • Linear algebra from std::experimental::math support added
    • Named SI units and their dimensions added (thanks @rbrugo
    • libfmt updated to 6.2.0
    • Added absolute functions and epsilon to math.h (thanks @mikeford3)
    • Added a lot of prefixes to named units and introduced alias_unit (thanks @yasamoka)
    • Linking with Conan targets only when they exists (#98)
    • All physical dimensions and units put into physical namespace
    • CMake improvements
    • Velocity renamed to speed

    Many thanks to GitHub users @oschonrock, @kwikius, and @i-ky for their support in drafting a new library design.

    "},{"location":"release_notes/#0.4.0","title":"0.4.0 Nov 17, 2019","text":"
    • Support for derived dimensions in exp added
    • Added pow() and sqrt() operations on quantities
    • units removed from a std::experimental namespace
    • Downcasting facility refactored so the user does not have to write the boilerplate code anymore
    • From now on base dimensions should inherit from base_dimension class template
    • Added unit symbols definitions to base_dimension and derived units
    • Added support for operator<< on quantity
    • fmt support added
    • Derived unit factory helpers refactored
    • Refactored the way prefixed units are defined
    "},{"location":"release_notes/#0.3.1","title":"0.3.1 Sep 18, 2019","text":"
    • cmcstl2 dependency changed to range-v3 0.9.1
    "},{"location":"release_notes/#0.3.0","title":"0.3.0 Sep 16, 2019","text":"
    • The design as described on CppCon 2019 talk (https://youtu.be/0YW6yxkdhlU)
    • Applied the feedback from the Cologne evening session
    • upcasting_traits renamed to downcasting_traits
    • Dimension template parameter removed from quantity
    • units moved to a std::experimental namespace
    • Leading underscore prefix removed from UDLs
    • Added a few more derived dimensions
    • meter renamed to metre
    • Missing operator* added
    • Predefined dimensions moved to a dedicated directory
    • dimension_ prefix removed from names of derived dimensions
    • cmcstl2 library updated to 2019.09.19
    • base_dimension is a value provided as const& to the exp type
    • integrated with Compiler Explorer
    • gsl-lite dependency removed
    • Fractional dimension exponents support added
    • QuantityOf concept introduced
    • quantity_cast<U, Rep>() support added
    "},{"location":"release_notes/#0.2.0","title":"0.2.0 July 18, 2019","text":"
    • The design as described on C++Now 2019 talk (https://youtu.be/wKchCktZPHU)
    • Added C++20 features supported by gcc-9.1 (std::remove_cvref_t, down with typename, std::type_identity)
    • Compile-time performance optimizations (type_list, common_ratio, ratio, conditional_t)
    "},{"location":"release_notes/#0.1.0","title":"0.1.0 May 18, 2019","text":"
    • Initial library release
    • Begin semantic versioning
    • The last version to work with gcc-8
    "},{"location":"appendix/glossary/","title":"Glossary","text":""},{"location":"appendix/glossary/#iso-definitions","title":"ISO definitions","text":"

    Note

    The ISO terms provided below are only a few of many defined in the ISO/IEC Guide 99.

    quantity
    • Property of a phenomenon, body, or substance, where the property has a magnitude that can be expressed by means of a number and a reference.
    • A reference can be a measurement unit, a measurement procedure, a reference material, or a combination of such.
    • A quantity as defined here is a scalar. However, a vector or a tensor, the components of which are quantities, is also considered to be a quantity.
    • The concept \u2019quantity\u2019 may be generically divided into, e.g. \u2018physical quantity\u2019, \u2018chemical quantity\u2019, and \u2018biological quantity\u2019, or \u2018base quantity\u2019 and \u2018derived quantity\u2019.
    • Examples of quantities are: length, radius, wavelength, energy, electric charge, etc.
    kind of quantity, kind
    • Aspect common to mutually comparable quantities.
    • The division of the concept \u2018quantity\u2019 into several kinds is to some extent arbitrary, for example:
      • the quantities diameter, circumference, and wavelength are generally considered to be quantities of the same kind, namely, of the kind of quantity called length,
      • the quantities heat, kinetic energy, and potential energy are generally considered to be quantities of the same kind, namely of the kind of quantity called energy.
    • Quantities of the same kind within a given system of quantities have the same quantity dimension. However, quantities of the same dimension are not necessarily of the same kind.
      • For example, the quantities moment of force and energy are, by convention, not regarded as being of the same kind, although they have the same dimension. Similarly for heat capacity and entropy, as well as for number of entities, relative permeability, and mass fraction.
    system of quantities
    • Set of quantities together with a set of non-contradictory equations relating those quantities.
    • Examples of systems of quantities are: the International System of Quantities, the Imperial System, etc.
    base quantity
    • Quantity in a conventionally chosen subset of a given system of quantities, where no quantity in the subset can be expressed in terms of the others.
    • Base quantities are referred to as being mutually independent since a base quantity cannot be expressed as a product of powers of the other base quantities.
    • \u2018Number of entities\u2019 can be regarded as a base quantity in any system of quantities.
    derived quantity
    • Quantity, in a system of quantities, defined in terms of the base quantities of that system.
    International System of Quantities, ISQ
    • System of quantities based on the seven base quantities: length, mass, time, electric current, thermodynamic temperature, amount of substance, and luminous intensity.
    • This system of quantities is published in the ISO 80000 and IEC 80000 series Quantities and units.
    • The International System of Units (SI) is based on the ISQ.
    quantity dimension, dimension of a quantity, dimension
    • Expression of the dependence of a quantity on the base quantities of a system of quantities as a product of powers of factors corresponding to the base quantities, omitting any numerical factor.
      • e.g. in the ISQ, the quantity dimension of force is denoted by \\(\\textsf{dim }F = \\mathsf{LMT}^{\u20132}\\).
    • A power of a factor is the factor raised to an exponent. Each factor is the dimension of a base quantity.
    • In deriving the dimension of a quantity, no account is taken of its scalar, vector, or tensor character.
    • In a given system of quantities:
      • quantities of the same kind have the same quantity dimension,
      • quantities of different quantity dimensions are always of different kinds,
      • quantities having the same quantity dimension are not necessarily of the same kind.
    • Symbols representing the dimensions of the base quantities in the ISQ are:

      Base quantity Symbol for dimension length \\(\\mathsf{L}\\) mass \\(\\mathsf{M}\\) time \\(\\mathsf{T}\\) electric current \\(\\mathsf{I}\\) thermodynamic temperature \\(\\mathsf{\u0398}\\) amount of substance \\(\\mathsf{N}\\) luminous intensity \\(\\mathsf{J}\\)

      Thus, the dimension of a quantity \\(Q\\) is denoted by \\(\\textsf{dim }Q = \\mathsf{L}^\u03b1\\mathsf{M}^\u03b2\\mathsf{T}^\u03b3\\mathsf{I}^\u03b4\\mathsf{\u0398}^\u03b5\\mathsf{N}^\u03b6\\mathsf{J}^\u03b7\\) where the exponents, named dimensional exponents, are positive, negative, or zero.

    quantity of dimension one, dimensionless quantity
    • quantity for which all the exponents of the factors corresponding to the base quantities in its quantity dimension are zero.
    • The term \u201cdimensionless quantity\u201d is commonly used and is kept here for historical reasons. It stems from the fact that all exponents are zero in the symbolic representation of the dimension for such quantities. The term \u201cquantity of dimension one\u201d reflects the convention in which the symbolic representation of the dimension for such quantities is the symbol \\(1\\).
    • The measurement units and values of quantities of dimension one are numbers, but such quantities convey more information than a number.
    • Some quantities of dimension one are defined as the ratios of two quantities of the same kind.
    • Numbers of entities are quantities of dimension one.
    measurement unit, unit of measurement, unit
    • Real scalar quantity, defined and adopted by convention, with which any other quantity of the same kind can be compared to express the ratio of the two quantities as a number.
    • Measurement units are designated by conventionally assigned names and symbols.
    • Measurement units of quantities of the same quantity dimension may be designated by the same name and symbol even when the quantities are not of the same kind.
      • For example, joule per kelvin and J/K are respectively the name and symbol of both a measurement unit of heat capacity and a measurement unit of entropy, which are generally not considered to be quantities of the same kind.
    • However, in some cases special measurement unit names are restricted to be used with quantities of specific kind only.
      • For example, the measurement unit \u2018second to the power minus one\u2019 (\\(\\mathsf{1/s}\\)) is called hertz (\\(\\mathsf{Hz}\\)) when used for frequencies and becquerel (\\(\\mathsf{Bq}\\)) when used for activities of radionuclides. As another example, the joule (\\(\\mathsf{J}\\)) is used as a unit of energy, but never as a unit of moment of force, e.g. the newton metre (\\(\\mathsf{N\u00b7m}\\)).
    • Measurement units of quantities of dimension one are numbers. In some cases, these measurement units are given special names, e.g. radian, steradian, and decibel, or are expressed by quotients such as millimole per mole equal to \\(10^{\u22123}\\) and microgram per kilogram equal to \\(10^{\u22129}\\).
    base unit
    • Measurement unit that is adopted by convention for a base quantity.
    • In each coherent system of units, there is only one base unit for each base quantity.
      • e.g. in the SI, the metre is the base unit of length. In the CGS systems, the centimetre is the base unit of length.
    • A base unit may also serve for a derived quantity of the same quantity dimension.
    • For number of entities, the number one, symbol \\(1\\), can be regarded as a base unit in any system of units.
    derived unit
    • Measurement unit for a derived quantity.
    • For example, the metre per second, symbol m/s, and the centimetre per second, symbol cm/s, are derived units of speed in the SI. The kilometre per hour, symbol km/h, is a measurement unit of speed outside the SI but accepted for use with the SI. The knot, equal to one nautical mile per hour, is a measurement unit of speed outside the SI.
    coherent derived unit
    • Derived unit that, for a given system of quantities and for a chosen set of base units, is a product of powers of base units with no other proportionality factor than one.
    • A power of a base unit is the base unit raised to an exponent.
    • Coherence can be determined only with respect to a particular system of quantities and a given set of base units.
      • For example, if the metre, the second, and the mole are base units, the metre per second is the coherent derived unit of velocity when velocity is defined by the quantity equation \\(v = \\mathsf{d}r/\\mathsf{d}t\\), and the mole per cubic metre is the coherent derived unit of amount-of-substance concentration when amount-of-substance concentration is defined by the quantity equation \\(c = n/V\\). The kilometre per hour and the knot, given as examples of derived units, are not coherent derived units in such a system of quantities.
    • A derived unit can be coherent with respect to one system of quantities but not to another.
      • For example, the centimetre per second is the coherent derived unit of speed in a CGS system of units but is not a coherent derived unit in the SI.
    • The coherent derived unit for every derived quantity of dimension one in a given system of units is the number one, symbol \\(1\\). The name and symbol of the measurement unit one are generally not indicated.
    system of units
    • Set of base units and derived units, together with their multiples and submultiples, defined in accordance with given rules, for a given system of quantities.
    coherent system of units
    • System of units, based on a given system of quantities, in which the measurement unit for each derived quantity is a coherent derived unit.
    • A system of units can be coherent only with respect to a system of quantities and the adopted base units.
    • For a coherent system of units, numerical value equations have the same form, including numerical factors, as the corresponding quantity equations.
    off-system measurement unit, off-system unit
    • Measurement unit that does not belong to a given system of units.
    • For example, the electronvolt (about \\(1.602\\;18 \u00d7 10^{\u201319}\\;\\mathsf{J}\\)) is an off-system measurement unit of energy with respect to the SI. Day, hour, minute are off-system measurement units of time with respect to the SI.
    International System of Units, SI
    • System of units, based on the International System of Quantities, their names and symbols, including a series of prefixes and their names and symbols, together with rules for their use, adopted by the General Conference on Weights and Measures (CGPM).
    quantity value, value of a quantity, value
    • Number and reference together expressing magnitude of a quantity.
      • For example, length of a given rod: \\(5.34\\;\\mathsf{m}\\) or \\(534\\;\\mathsf{cm}\\).
    • The number can be complex.
    • A quantity value can be presented in more than one way.
    • In the case of vector or tensor quantities, each component has a quantity value.
      • For example, force acting on a given particle, e.g. in Cartesian components \\((F_x; F_y; F_z) = (\u221231.5; 43.2; 17.0)\\;\\mathsf{N}\\).
    numerical quantity value, numerical value of a quantity, numerical value
    • Number in the expression of a quantity value, other than any number serving as the reference
      • For example, in an amount-of-substance fraction equal to \\(3\\;\\mathsf{mmol/mol}\\), the numerical quantity value is \\(3\\) and the unit is \\(\\mathsf{mmol/mol}\\). The unit \\(\\mathsf{mmol/mol}\\) is numerically equal to \\(0.001\\), but this number \\(0.001\\) is not part of the numerical quantity value, which remains \\(3\\).
    quantity equation
    • Mathematical relation between quantities in a given system of quantities, independent of measure\u00adment units.
    • For example, \\(T = (1/2) mv^2\\) where \\(T\\) is the kinetic energy and \\(v\\) the speed of a specified particle of mass \\(m\\).
    unit equation
    • Mathematical relation between base units, coher\u00adent derived units or other measurement units.
    • For example, \\(\\mathsf{J} := \\mathsf{kg}\\:\\mathsf{m}^2/\\mathsf{s}^2\\), where, \\(\\mathsf{J}\\), \\(\\mathsf{kg}\\), \\(\\mathsf{m}\\), and \\(\\mathsf{s}\\) are the symbols for the joule, kilogram, metre, and second, respectively. (The symbol \\(:=\\) denotes \u201cis by definition equal to\u201d as given in the ISO 80000 and IEC 80000 series.). \\(1\\;\\mathsf{km/h} = (1/3.6)\\;\\mathsf{m/s}\\).
    numerical value equation, numerical quantity value equation
    • Mathematical relation between numerical quantity values, based on a given quantity equation and specified measurement units.
    • For example, in the quantity equation for kinetic energy of a particle, \\(T = (1/2) mv^2\\), if \\(m = 2\\;\\mathsf{kg}\\) and \\(v = 3\\;\\mathsf{m/s}\\), then \\({T} = (1/2)\\:\u00d7\\:2\\:\u00d7\\:3^2\\) is a numerical value equation giving the numerical value \\(9\\) of \\(T\\) in joules.
    "},{"location":"appendix/glossary/#other-definitions","title":"Other definitions","text":"

    Info

    The below terms extend the official ISO glossary and are commonly referred to by the mp-units library.

    base dimension
    • A dimension of a base quantity.
    derived dimension
    • A dimension of a derived quantity.
    • Implemented as an expression template being the result of the dimension equation on base dimensions.
    dimension equation
    • Mathematical relation between dimensions in a given system of quantities, independent of measure\u00adment units.
    quantity kind hierarchy, quantity hierarchy
    • Quantities of the same kind form a hierarchy that determines their:
      • convertibility (e.g. every width is a length, but width should not be convertible to height)
      • common quantity type (e.g. width + height -> length)
    quantity character, character of a quantity, character
    • Scalars, vectors and tensors are mathematical objects that can be used to denote certain physical quantities and their values. They are as such independent of the particular choice of a coordinate system, whereas each scalar component of a vector or a tensor and each component vector and component tensor depend on that choice.
    • A vector is a tensor of the first order and a scalar is a tensor of order zero.
    • For vectors and tensors, the components are quantities that can be expressed as a product of a number and a unit.
    • Vectors and tensors can also be expressed as a numerical value vector or tensor, respectively, multiplied by a unit.
    • Quantities of different characters support different set of operations.
      • For example, a quantity can be multiplied by another one only if any of them has scalar character. Vectors and tensors can't be multiplied or divided, but they support additional operations like dot and cross products, which are not available for scalars.
    • The term \u2019character\u2019 was borrowed from the below quote:

    ISO 80000-1_2009

    In deriving the dimension of a quantity, no account is taken of its scalar, vector, or tensor character.

    quantity specification, quantity_spec
    • An entity storing all the information about a specific quantity:
      • location in a quantity hierarchy
      • quantity equation
      • dimension of a quantity
      • quantity kind
      • quantity character
      • additional constraints (e.g. non-negative)
    • Dimension of a quantity is not enough to specify all the properties of a quantity.
    unit with an associated quantity, associated unit
    • Unit that is used to measure quantities of a specific kind in a given system of units.
    quantity reference, reference
    • According to its definition, quantity can be expressed by means of a number and a reference
    • In the mp-units library, a reference describes all the required meta-information associated with a specific quantity (quantity specification and unit).
    canonical representation of a unit, canonical unit
    • A canonical representation of a unit consists of:
      • a reference unit being the result of extraction of all the intermediate derived units,
      • a magnitude being a product of all the prefixes and magnitudes of extracted scaled units.
    • All units having the same canonical unit are deemed equal.
    • All units having the same reference unit are convertible (their magnitude may differ and is used during conversion).
    reference unit

    See canonical representation of a unit

    absolute quantity point origin, absolute point origin
    • An explicit point on an axis of values of a specific quantity type that serves as an absolute reference point for all quantity points which definitions are (explicitly or implicitly) based on it.
    • For example, mean sea level is commonly used as an absolute reference point to measure altitudes.
    relative quantity point origin, relative point origin
    • An explicit, known at compile-time, point on an axis of values of a specific quantity type serving as a reference for other quantities.
    • For example, an ice point is a quantity point with a value of \\(273.15\\;\\mathsf{K}\\) that is used as the zero point of a degree Celsius scale.
    quantity point origin, point origin
    • Either an absolute point origin or a relative point origin.
    quantity point, absolute quantity
    • An absolute quantity with respect to an origin.
    • For example, timestamp (as opposed to duration), altitude (as opposed to height), absolute temperature (as opposed to temperature difference).
    "},{"location":"appendix/references/","title":"References","text":"ISO80000

    ISO 80000-1:2009(E) \"Quantities and units \u2014 Part 1: General\", International Organization for Standardization.

    Quincey \"Angles in the SI: a detailed proposal for solving the problem, Quincey, Paul (1 October 2021). SIBrochure

    The International System of Units (SI), International Bureau of Weights and Measures (20 May 2019), ISBN 978-92-822-2272-0.

    "},{"location":"blog/","title":"Blog","text":""},{"location":"blog/2023/09/24/whats-new-in-mp-units-20/","title":"What's new in mp-units 2.0?","text":"

    After a year of hard work, we've just released mp-units 2.0.0. It can be obtained from GitHub and Conan.

    The list of the most significant changes introduced by the new version can be found in our Release Notes. We will also describe some of them in this post.

    "},{"location":"blog/2023/09/24/whats-new-in-mp-units-20/#why-20-if-10-was-never-released","title":"Why 2.0 if 1.0 was never released?","text":"

    Version 2 of the mp-units project is a huge change and a new quality for the users. We did not want to pretend that 2.0 is an evolutionary upgrade of the previous version of the project. It feels like a different product.

    We could start a new repo named \"mp-units-v2\" similarly to range-v3 but we decided not to go this path. We kept the same repo and made the scope of the changes and potential breakage explicit with a drastic bump in the project version.

    "},{"location":"blog/2023/09/24/whats-new-in-mp-units-20/#what-has-changed","title":"What has changed?","text":"

    The answer is \"nearly everything\". The whole library and its documentation were rewritten nearly from scratch.

    Here are the significant changes that the users can observe:

    • Repository name

      If you didn't notice, the repository name was changed from \"mpusz/units\" to \"mpusz/mp-units\".

    • Header files content and layout

      Previously, all the header files resided in the include/units directory. Now, they can be found in include/mp-units. The project file tree was significantly changed as well. Many files were moved to different subdirectories or renamed.

    • Namespace

      Previously, all the definitions were provided in the units namespace, and now they are in the mp_units one.

    • Abstractions, interfaces, definitions

      The interfaces of all of the types were refactored. We got unit symbols and a new way to construct a quantity and quantity_point. The readability of the generated types was improved thanks to the introduction of expression templates. Nearly all of the template arguments are now passed by values thanks to class NTTP extensions in C++20. As a result, unit definitions are much easier and terser. Also, the V2 has a powerful ability to model systems of quantities and provides definitions for many ISQ quantities.

    • Conan 2.0

      Also, now we support Conan 2.0, which provides an updated way of handling dependencies.

    "},{"location":"blog/2023/09/24/whats-new-in-mp-units-20/#what-is-gone","title":"What is gone?","text":"

    Some cornerstones of the initial design did not prove in practice and were removed while we moved to version 2.

    "},{"location":"blog/2023/09/24/whats-new-in-mp-units-20/#the-downcasting-facility","title":"The downcasting facility","text":"

    The first and the most important of such features was removing the downcasting facility. This feature is still a powerful metaprogramming technique that allows users to map long class template instantiations to nicely named, short, and easy-to-understand user's strong types.

    Such mapping works perfectly fine for 1-to-1 relationships. However, we often deal with N-to-1 connections in the quantities and units domain. Here are only a few such examples:

    • work and torque have the same dimension \\(L^2MT^{-2}\\),
    • becquerel and hertz have the same definition of \\(s^{-1}\\),
    • litre and cubic decimetre have the same factor.

    In the above examples, multiple entities \"wanted\" to register different names for identical class template instantiations, resulting in compile-time errors. We had to invent some hacks and workarounds to make it work, but we were never satisfied with the outcome.

    Additionally, this facility could easily lead to ODR violations or provide different results depending on which header files were included in the translation units. This was too vulnerable to be considered a good practice here.

    "},{"location":"blog/2023/09/24/whats-new-in-mp-units-20/#no-udls-anymore","title":"No UDLs anymore","text":"

    Over the years, we have learned that UDLs are not a good solution. More information on this subject can be found in the Why don't we use UDLs to create quantities? chapter.

    "},{"location":"blog/2023/09/24/whats-new-in-mp-units-20/#no-construction-of-a-quantity-from-a-raw-value","title":"No construction of a quantity from a raw value","text":"

    To improve safety, we no longer allow the construction of quantities from raw values. In the new design, we always need to explicitly specify a unit to create a quantity:

    quantity q1 = 42 * m;\nquantity<si::metre> = 2 * km;\nquantity q3(42, si::metre);\n

    The previous approach was reported to be error-prone under maintenance. More on this subject can be found in the Why can't I create a quantity by passing a number to a constructor? chapter.

    "},{"location":"blog/2023/09/24/whats-new-in-mp-units-20/#new-look-and-feel","title":"New look and feel","text":"

    Here is a concise example showing you the new look and feel of the library:

    #include <mp-units/format.h>\n#include <mp-units/systems/isq/isq.h>\n#include <mp-units/systems/si/si.h>\n#include <format>\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\nquantity<isq::speed[m / s]> avg_speed(quantity<si::metre> d,\n                                      quantity<si::second> t)\n{ return d / t; }\n\nint main()\n{\n  auto speed = avg_speed(220 * km, 2 * h);\n  std::println(\"{}\", speed);  // 30.5556 m/s\n}\n

    All of the changes we provided, although breaking ones, resulted in much better, easier, and safer abstractions. These offer a new quantity on the market and hopefully will be appreciated by our users.

    Please check our new documentation to learn about the latest version of the project and find out how to benefit from all the new cool stuff we have here.

    "},{"location":"blog/2023/12/09/mp-units-210-released/","title":"mp-units 2.1.0 released!","text":"

    A new product version can be obtained from GitHub and Conan.

    The list of the most significant changes introduced by the new version can be found in our Release Notes. We will also describe the most important of them in this post.

    "},{"location":"blog/2023/12/09/mp-units-210-released/#no-more-parenthesis-while-creating-quantities-with-derived-units","title":"No more parenthesis while creating quantities with derived units","text":"

    The V2 design introduced a way to create a quantity by multiplying a raw value and a unit:

    quantity q1 = 42 * m;\n

    However, this meant that when we wanted to create a quantity having a derived unit, we had to put parenthesis around the unit equation or create a custom value of the named unit:

    quantity q2 = 60 * (km / h);\n\nconstexpr auto kmph = km / h;\nquantity q3 = 60 * kmph;\n\nquantity q4 = 50 * (1 / s);\n

    With the new version, we removed this restriction, and now we can type:

    quantity q5 = 60 * km / h;\nquantity q6 = 50 / s;\n

    As a side effect, we introduced a breaking change. We can't use the following definition of hertz anymore:

    inline constexpr struct hertz : named_unit<\"Hz\", 1 / second, kind_of<isq::frequency>> {} hertz;\n

    and have to type either:

    inline constexpr struct hertz : named_unit<\"Hz\", one / second, kind_of<isq::frequency>> {} hertz;\n

    or

    inline constexpr struct hertz : named_unit<\"Hz\", inverse(second), kind_of<isq::frequency>> {} hertz;\n

    To be consistent, we applied the same change to the dimensions and quantity specifications definitions. Now, to define a frequency we have to type:

    C++23C++20Portable
    inline constexpr struct frequency : quantity_spec<inverse(period_duration)> {} frequency;\n
    inline constexpr struct frequency : quantity_spec<frequency, inverse(period_duration)> {} frequency;\n
    QUANTITY_SPEC(frequency, inverse(period_duration));\n
    "},{"location":"blog/2023/12/09/mp-units-210-released/#make_xxx-factory-functions-replaced-with-two-parameter-constructors","title":"make_xxx factory functions replaced with two-parameter constructors","text":"

    In the initial version of the V2 framework, if someone did not like the multiply syntax to create a quantity we provided the make_quantity() factory function. A similar approach was used for quantity_point creation.

    This version removes those (breaking change) and introduces two parameter constructors:

    quantity q(42, si::metre);\nquantity_point qp(q, mean_sea_level);\n

    The above change encourages a better design and results in a terser code.

    "},{"location":"blog/2023/12/09/mp-units-210-released/#improved-definitions-of-becquerel-gray-and-sievert","title":"Improved definitions of becquerel, gray, and sievert","text":"

    In the initial V2 version, we lacked the definitions of the atomic and nuclear physics quantities, which resulted in simplified and unsafe definitions of becquerel, gray, and sievert units. We still do not model most of the quantities from this domain, but we've added the ones that are necessary for the definition of those units.

    Thanks to the above, the following expressions will not compile:

    quantity q1 = 1 * Hz + 1 * Bq;\nquantity<si::sievert> q2 = 42 * Gy;\n
    "},{"location":"blog/2023/12/09/mp-units-210-released/#compatibility-with-other-libraries-redesigned","title":"Compatibility with other libraries redesigned","text":"

    Another significant improvement in this version was redesigning the way we provide compatibility with other similar libraries. The interfaces of quantity_like_traits and quantity_point_like_traits were changed and extended to provide conversion not only from but also to entities from other libraries (breaking change).

    We've also introduced an innovative approach that allows us to specify if such conversions should happen implicitly or if they need to be forced explicitly.

    More on this subject can be found in the Interoperability with Other Libraries chapter.

    "},{"location":"blog/2023/12/09/mp-units-210-released/#point-origins-can-now-be-derived-from-each-other","title":"Point origins can now be derived from each other","text":"

    Previously, each class derived from absolute_point_origin was considered a unique independent point origin. On the other hand, it was OK to derive multiple classes from the same relative_point_origin, and those were specifying the same point in the domain. We found this confusing and limiting. This is why, in this version, the absolute_point_origin uses a CRTP idiom to be able to detect between points that should be considered different from the ones that should be equivalent.

    If we derive from the same instantiation of absolute_point_origin we end up with an equivalent point origin. This change allows us to provide different names for the same temperature points:

    inline constexpr struct absolute_zero : absolute_point_origin<absolute_zero, isq::thermodynamic_temperature> {} absolute_zero;\ninline constexpr struct zeroth_kelvin : decltype(absolute_zero) {} zeroth_kelvin;\n\ninline constexpr struct ice_point : relative_point_origin<absolute_zero + 273.15 * kelvin> {} ice_point;\ninline constexpr struct zeroth_degree_Celsius : decltype(ice_point) {} zeroth_degree_Celsius;\n

    Please note that this is a breaking change as well.

    "},{"location":"blog/2023/12/09/mp-units-210-released/#unit-symbol-text-can-now-be-properly-used-at-runtime","title":"Unit symbol text can now be properly used at runtime","text":"

    The interface of the previous definition of unit_symbol function allowed the use of the returned buffer only at compile-time. This was too limiting as users often want to use unit symbols at runtime (e.g., print them to the console). The new version redesigned the interface of this function (breaking change) to return a buffer that can be properly used at both compilation and runtime:

    std::string_view unit1 = unit_symbol(m / s);\nstd::cout << unit1 << \"\\n\";     // m/s\nstd::string_view unit2 = unit_symbol<{.solidus = unit_symbol_solidus::never}>(m / s);\nstd::cout << unit2 << \"\\n\";     // m s\u207b\u00b9\n
    "},{"location":"blog/2023/11/12/report-from-the-kona-2023-iso-c-committee-meeting/","title":"Report from the Kona 2023 ISO C++ Committee meeting","text":"

    Several groups in the ISO C++ Committee reviewed the P1935: A C++ Approach to Physical Units proposal in Belfast 2019 and Prague 2020. All those groups expressed interest in the potential standardization of such a library and encouraged further work. The authors also got valuable initial feedback that highly influenced the design of the V2 version of the mp-units library.

    In the following years, we scoped on getting more feedback from the production and design. This resulted in version 2 of the mp-units library that resolved many issues the users and Committee members raised. The features and interfaces of this version are close to being the best we can get with the current version of the C++ language standard.

    We submitted three new proposals related to the standardization of the quantities and units library for the last ISO C++ Committee meeting:

    • P2980: A motivation, scope, and plan for a physical quantities and units library,
    • P2981: Improving our safety with a physical quantities and units library,
    • P2982: std::quantity as a numeric type.

    Those were reviewed and briefly discussed in several groups: Numerics (SG6), Safety & Security (SG23), and Library Evolution Working Group (LEWG). Most of the feedback was positive, and the Committee is interested in spending more time on such proposals.

    The following poll was taken by the LEWG:

    LEWG POLL: Given that our time is limited, more time should be promised for a quantities and units library

    Strongly in Favor In favor Neutral Against Strongly Against 10 13 4 0 0

    Attendance: 22 + 6

    Number of Authors: 4

    Authors\u2019 position: 4x SF

    Outcome: Strong consensus in favor

    Additionally, some concerns were raised about the large scope of the proposal. We were encouraged to return with more details and design rationale in a unified paper. This is what we are working on now for the next Committee meeting that will happen in mid-March 2024 in Tokyo.

    "},{"location":"getting_started/faq/","title":"Frequently Asked Questions","text":""},{"location":"getting_started/faq/#why-do-we-spell-metre-instead-of-meter","title":"Why do we spell metre instead of meter?","text":"

    This is how the BIPM defines it in the SI Brochure (British English spelling by default).

    "},{"location":"getting_started/faq/#why-dont-we-use-udls-to-create-quantities","title":"Why don't we use UDLs to create quantities?","text":"

    Many reasons make UDLs a poor choice for a physical units library:

    1. UDLs work only with literals (compile-time known values). Our observation is that besides the unit tests, there are only a few compile-time known quantity values used in the production code. Please note that for physical constants, we recommend using Faster-than-lightspeed Constants.
    2. Typical implementations of UDLs tend to always use the widest representation type available. In the case of std::chrono::duration, the following is true:

      using namespace std::chrono_literals;\nauto d1 = 42s;\nauto d2 = 42.s;\nstatic_assert(std::is_same_v<decltype(d1)::rep, std::int64_t>);\nstatic_assert(std::is_same_v<decltype(d2)::rep, long double>);\n

    When such UDL is intermixed in arithmetics with any quantity type of a shorter representation type, it will always expand it to the longest one. In other words, such long type spreads until all types use it everywhere.

    1. While increasing the coverage for the library, we learned that many unit symbols conflict with built-in types or numeric extensions. A few of those are: F (farad), J (joule), W (watt), K (kelvin), d (day), l or L (litre), erg, ergps. Usage of the _ prefix would make it work for mp-units, but in case the library is standardized, those naming collisions would be a big issue. This is why we came up with the _q_ prefix that would become q_ after standardization (e.g., 42q_s), which is not that nice anymore.

    2. UDLs with the same identifiers defined in different namespace can't be disambiguated in the C++ language. If both SI and CGS systems define _q_s UDL for a second unit, then it would not be possible to specify which one to use in case both namespaces are \"imported\" with using directives.

    3. Another bad property of UDLs is that they do not compose. A coherent unit of angular momentum would have a UDL specified as _q_kg_m2_per_s. Now imagine that we want to make every possible user happy. How many variations of that unit would we predefine for differently scaled versions of all unit ingredients?

    4. UDLs are also really expensive to define and specify. Typically, for each unit, we need two definitions. One for integral and another one for floating-point representation. Before the V2 framework, the coherent unit of angular momentum was defined as:

      constexpr auto operator\"\" _q_kg_m2_per_s(unsigned long long l)\n{\n  gsl_ExpectsAudit(std::in_range<std::int64_t>(l));\n  return angular_momentum<kilogram_metre_sq_per_second, std::int64_t>(static_cast<std::int64_t>(l));\n}\n\nconstexpr auto operator\"\" _q_kg_m2_per_s(long double l)\n{\n  return angular_momentum<kilogram_metre_sq_per_second, long double>(l);\n}\n
    "},{"location":"getting_started/faq/#why-cant-i-create-a-quantity-by-passing-a-number-to-a-constructor","title":"Why can't I create a quantity by passing a number to a constructor?","text":"

    A quantity class template in the mp-units library has no publicly available constructor taking a raw value. Such support is provided by the std::chrono::duration and was pointed out to us as a red flag safety issue by a few parties already.

    Consider the following structure and a code using it:

    struct X {\n  std::vector<std::chrono::milliseconds> vec;\n  // ...\n};\n
    X x;\nx.vec.emplace_back(42);\n

    Everything works fine for years until, at some point, someone changes the structure to:

    struct X {\n  std::vector<std::chrono::microseconds> vec;\n  // ...\n};\n

    The code continues to compile just fine, but all the calculations are off now. This is why we decided to not follow this path.

    In the mp-units library, both a number and a unit have to always be explicitly provided in order to form a quantity.

    Note

    The same applies to the construction of quantity_point using an explicit point origin. To prevent similar safety issues during maintenance, the initialization always requires providing both a quantity and a PointOrigin that we use as a reference point.

    "},{"location":"getting_started/faq/#why-a-dimensionless-quantity-is-not-just-a-fundamental-arithmetic-type","title":"Why a dimensionless quantity is not just a fundamental arithmetic type?","text":"

    In the initial design of this library, the resulting type of division of two quantities was their common representation type:

    static_assert(std::is_same_v<decltype(10 * km / (5 * km)), int>);\n

    First of all, this was consistent with std::chrono::duration behavior. Additional reasoning behind it was not providing a false impression of a strong quantity type for something that looks and feels like a regular number. Also, all of the mathematic and trigonometric functions were working fine out of the box with such representation types, so we did not have to rewrite sin(), cos(), exp(), and others.

    However, the feedback we got from the production usage was that such an approach is really bad for generic programming. It is hard to handle the result of the two quantities' division (or multiplication) as it might be either a quantity or a fundamental type. If we want to raise such a result to some power, we must use units::pow or std::pow depending on the resulting type. Those are only a few issues related to such an approach.

    Moreover, suppose we divide quantities of the same dimension but with units of significantly different magnitudes. In that case, we may end up with a really small or a huge floating-point value, which may result in losing lots of precision. Returning a dimensionless quantity from such cases allows us to benefit from all the properties of scaled units and is consistent with the rest of the library.

    Note

    More information on the current design can be found in the Dimensionless Quantities chapter.

    "},{"location":"getting_started/faq/#why-do-the-identifiers-for-concepts-in-the-library-use-camelcase","title":"Why do the identifiers for concepts in the library use CamelCase?","text":"

    Initially, C++20 was meant to use CamelCase for all the concept identifiers. All the concepts from the std::ranges library were merged with such names into the standard document draft. Frustratingly, CamelCase concepts got dropped from the C++ standard at the last moment before releasing C++20. Now, we are facing the predictable consequences of running out of names.

    As long as some concepts in the library could be easily named with a standard_case there are some that are hard to distinguish from the corresponding type names, such as Quantity, QuantityPoint, QuantitySpec, or Reference. This is why we decided to use CamelCase consistently for all the concept identifiers to make it clear when we are talking about a type or concept identifier.

    However, we are aware that this might be a temporary solution. In case the library gets standardized, we can expect the ISO C++ Committee to bikeshed/rename all of the concept identifiers to a standard_case, even if it will result in a harder to understand code.

    Note

    In case you have a good idea of how to rename existing concepts to the standard_case, please let us know in the associated GitHub Issue.

    "},{"location":"getting_started/faq/#why-unicode-quantity-symbols-are-used-by-default-instead-of-ascii-only-characters","title":"Why Unicode quantity symbols are used by default instead of ASCII-only characters?","text":"

    Both C++ and ISO 80000 are standardized by the ISO. ISO 80000 and the SI standards specify Unicode symbols as the official unit names for some quantities (e.g. \u03a9 symbol for the resistance quantity). As the mp-units library will be proposed for standardization as a part of the C++ Standard Library we have to obey the rules and be consistent with ISO specifications.

    Note

    We do understand engineering reality and the constraints of some environments. This is why the library has the option of ASCII-only Quantity Symbols.

    "},{"location":"getting_started/faq/#why-dont-we-have-cmake-options-to-disable-the-building-of-tests-and-examples","title":"Why don't we have CMake options to disable the building of tests and examples?","text":"

    Over time, many people provided PRs proposing adding options to build tests and examples conditionally. Here are a few examples:

    • Add CMake options for disabling docs, examples and tests
    • build: add options to disable part of the build
    • CMake Refactoring and Option Cleanup

    We admit this is a common practice in the industry, but we also believe this is a bad pattern.

    First, the only need for such options comes when a user wants to use add_subdirectory() in CMake to handle dependencies. Such an approach does not scale and should be discouraged. There is little use for such a practice in times when we have dedicated package managers like Conan.

    The second thing is that our observation is that many people are fixed on disabling \"unneeded\" subdirectories from compilation, but they do not see or address the biggest issue, which is polluting user's build environment with our development-specific settings. Propagating our restrictive compilation flags to user's project is not the best idea as it might cause a lot of harm if this project stops to compile because of that.

    Last but not least, not having those options is on purpose. Top level CMakeLists.txt file should only be used by mp-units developers and contributors as an entry point for the project's development. We want to ensure that everyone will build ALL the code correctly before pushing a commit. Having such options would allow unintended issues to leak to PRs and CI.

    This is why our projects have two entry points:

    • ./CMakeLists.txt is to be used by projects developers to build ALL the project code with really restrictive compilation flags,
    • ./src/CMakeLists.txt contains only a pure library definition and should be used by the customers that prefer to use CMake's add_subdirectory() to handle the dependencies.

    Note

    For more details on this please refer to the CMake + Conan: 3 Years Later - Mateusz Pusz lecture that Mateusz Pusz provided at the C++Now 2021 conference.

    "},{"location":"getting_started/installation_and_usage/","title":"Installation And Usage","text":"

    This chapter provides all the necessary information to obtain and build the code using mp-units. It also describes how to build or distribute the library and generate its documentation.

    "},{"location":"getting_started/installation_and_usage/#cpp-compiler-support","title":"C++ compiler support","text":"

    Info

    mp-units library tries to provide the best user experience possible with the C++ language. To achieve that, it extensively uses C++20 features and the explicit object parameter from C++23.

    Even though the library benefits from C++23 (if available), C++20 is enough to compile and use all of the library's functionality. C++23 features are hidden behind a preprocessor macro providing a backward-compatible way to use it.

    The below table provides the minimum compiler version required to compile the code using the specific feature:

    Feature gcc clang apple-clang MSVC Minimum support 12 16 15 None std::format 13 17 None None C++ modules 14 17 None None C++23 extensions 14 18 None None

    More requirements for C++ modules support can be found in the CMake's documentation.

    "},{"location":"getting_started/installation_and_usage/#modules","title":"Modules","text":"

    The mp-units library provides the following C++ modules.

    flowchart TD\n    mp_units --- mp_units.systems --- mp_units.core
    C++ Module CMake Target Contents mp_units.core mp-units::core Core library framework and systems-independent utilities mp_units.systems mp-units::systems All the systems of quantities and units mp_units mp-units::mp-units Core + Systems

    Note

    C++ modules are provided within the package only when cxx_modules Conan option is set to True.

    "},{"location":"getting_started/installation_and_usage/#repository-structure-and-dependencies","title":"Repository structure and dependencies","text":"

    This repository contains three independent CMake-based projects:

    • ./src

      • header-only project containing whole mp-units library
      • ./src/CMakeList.txt file is intended as an entry point for library users
      • in case this library becomes part of the C++ standard, it will have no external dependencies but until then, it depends on the following:

        • gsl-lite to verify runtime contracts with the gsl_Expects macro,
        • {fmt} to provide text formatting of quantities (if std::format is not supported yet on a specific compiler).
    • .

      • project used as an entry point for library development and CI/CD
      • it wraps ./src project together with usage examples and tests
      • additionally to the dependencies of ./src project, it uses:

        • Catch2 library as a unit tests framework,
        • linear algebra library based on proposal P1385 used in some examples and tests.
    • ./test_package

      • CMake library installation and Conan package verification.

    Important: Library users should not use the top-level CMake file

    Top level CMakeLists.txt file should only be used by mp-units developers and contributors as an entry point for the project's development. We want to ensure that everyone will build ALL the code correctly before pushing a commit. Having such options would allow unintended issues to leak to PRs and CI.

    This is why our projects have two entry points:

    • ./CMakeLists.txt is to be used by projects developers to build ALL the project code with really restrictive compilation flags,
    • ./src/CMakeLists.txt contains only a pure library definition and should be used by the customers that prefer to use CMake's add_subdirectory() to handle the dependencies.

    To learn more about the rationale, please check our FAQ.

    "},{"location":"getting_started/installation_and_usage/#obtaining-dependencies","title":"Obtaining dependencies","text":"

    This library assumes that most of the dependencies will be provided by the Conan Package Manager. If you want to obtain required dependencies by other means, some modifications to the library's CMake files might be needed. The rest of the dependencies responsible for documentation generation are provided by python3-pip.

    "},{"location":"getting_started/installation_and_usage/#conan-quick-intro","title":"Conan quick intro","text":"

    In case you are not familiar with Conan, to install it (or upgrade) just do:

    pip3 install -U conan\n

    After that, you might need to add a custom profile file for your development environment in ~/.conan2/profiles directory. An example profile can look as follows:

    ~/.conan2/profiles/gcc12
    [settings]\narch=x86_64\nbuild_type=Release\ncompiler=gcc\ncompiler.cppstd=20\ncompiler.libcxx=libstdc++11\ncompiler.version=12\nos=Linux\n\n[conf]\ntools.build:compiler_executables={\"c\": \"gcc-12\", \"cpp\": \"g++-12\"}\n

    Setting the language version

    Please note that the mp-units library requires at least C++20 to be set in a Conan profile or forced via the Conan command line. If we do the former, we will not need to provide -s compiler.cppstd=20 every time we run a Conan command line (as provided in the command line instructions below).

    Using Ninja as a CMake generator for Conan

    It is highly recommended to set Ninja as a CMake generator for Conan. To do so, we should create a ~/.conan2/global.conf file that will set tools.cmake.cmaketoolchain:generator to one of the Ninja generators. For example:

    ~/.conan2/global.conf
    tools.cmake.cmaketoolchain:generator=\"Ninja Multi-Config\"\n

    Separate build folders for different configurations

    ~/.conan2/global.conf file may also set tools.cmake.cmake_layout:build_folder_vars which makes working with several compilers or build configurations easier. For example, the below line will force Conan to generate separate CMake presets and folders for each compiler and C++ standard version:

    ~/.conan2/global.conf
    tools.cmake.cmake_layout:build_folder_vars=[\"settings.compiler\", \"settings.compiler.version\", \"settings.compiler.cppstd\"]\n

    In such a case, we will need to use a configuration-specific preset name in the Conan instructions provided below rather than just conan-default and conan-release (e.g. conan-gcc-13-23 and conan-gcc-13-23-release)

    "},{"location":"getting_started/installation_and_usage/#build-options","title":"Build options","text":""},{"location":"getting_started/installation_and_usage/#conan-options","title":"Conan options","text":"cxx_modules

    2.2.0 \u00b7 True/False (Default: False)

    Configures CMake to add C++ modules to the list of default targets.

    use_fmtlib

    2.2.0 \u00b7 True/False (Default: False)

    Forces usage of {fmt} library instead of the C++20 Standard Library features.

    "},{"location":"getting_started/installation_and_usage/#conan-configuration-properties","title":"Conan configuration properties","text":"user.mp-units.build:all

    2.2.0 \u00b7 True/False (Default: False)

    Enables compilation of all the source code, including tests and examples. To support this, it requires some additional Conan build dependencies described in Repository Structure and Dependencies. It also runs unit tests during Conan build (unless tools.build:skip_test configuration property is set to True).

    user.mp-units.build:skip_la

    2.2.0 \u00b7 True/False (Default: False)

    If user.mp-units.build:all is enabled, among others, Conan installs the external wg21-linear_algebra dependency and enables the compilation of linear algebra-based tests and usage examples. Such behavior can be disabled with this option.

    "},{"location":"getting_started/installation_and_usage/#cmake-options","title":"CMake options","text":"MP_UNITS_BUILD_CXX_MODULES

    2.2.0 \u00b7 ON/OFF (Default: OFF)

    Adds C++ modules to the list of default targets.

    MP_UNITS_USE_FMTLIB

    2.2.0 \u00b7 ON/OFF (Default: OFF)

    Forces usage of {fmt} library instead of the C++20 Standard Library features.

    MP_UNITS_BUILD_LA

    2.0.0 \u00b7 ON/OFF (Default: ON)

    Enables building code depending on the linear algebra library.

    MP_UNITS_IWYU

    2.0.0 \u00b7 ON/OFF (Default: OFF)

    Enables include-what-you-use when compiling with a clang compiler. Additionally turns on MP_UNITS_AS_SYSTEM_HEADERS.

    "},{"location":"getting_started/installation_and_usage/#cmake-with-presets-support","title":"CMake with presets support","text":"

    It is recommended to use at least CMake 3.23 to build this project as this version introduced support for CMake Presets schema version 4, used now by Conan to generate presets files. All build instructions below assume that you have such support. If not, your CMake invocations have to be replaced with something like:

    mkdir build && cd build\ncmake .. -G \"Ninja Multi-Config\" -DCMAKE_TOOLCHAIN_FILE=<path_to_generators_dir>/conan_toolchain.cmake\ncmake --build . --config Release\n

    Tip

    In case you can't use CMake 3.23 but you have access to CMake 3.20 or later, you can append -c tools.cmake.cmaketoolchain.presets:max_schema_version=2 to the conan install command which will force Conan to use an older version of the CMake Presets schema.

    "},{"location":"getting_started/installation_and_usage/#installation-and-reuse","title":"Installation and reuse","text":"

    There are many different ways of installing/reusing mp-units in your project. Below we mention only a few of many options possible.

    Important: Prefer using Conan if possible

    The easiest and most recommended way to obtain mp-units is with the Conan package manager. See Conan + CMake (release) for a detailed instruction.

    "},{"location":"getting_started/installation_and_usage/#copy","title":"Copy","text":"

    As mp-units is a C++ header-only library you can simply copy all needed src/*/include subdirectories to your source tree.

    Note

    In such a case, you are on your own to ensure all the dependencies are installed and their header files can be located during the build. Please also note that some compiler-specific flags are needed to make the code compile without issues.

    "},{"location":"getting_started/installation_and_usage/#copy-cmake","title":"Copy + CMake","text":"

    If you copy the whole mp-units repository to your project's file tree, you can reuse CMake targets defined by the library. To do so, you should use CMakeLists.txt file from the ./src directory:

    add_subdirectory(<path_to_units_folder>/src)\n# ...\ntarget_link_libraries(<your_target> <PUBLIC|PRIVATE|INTERFACE> mp-units::mp-units)\n

    Note

    You are still on your own to make sure all the dependencies are installed and their header and CMake configuration files can be located during the build.

    "},{"location":"getting_started/installation_and_usage/#conan-cmake-release","title":"Conan + CMake (release)","text":"

    Tip

    If you are new to the Conan package manager, it is highly recommended to read Obtaining Dependencies and refer to Consuming packages chapter of the official Conan documentation for more information.

    mp-units releases are hosted on Conan-Center. The following steps may be performed to obtain an official library release:

    1. Create Conan configuration file (either conanfile.txt or conanfile.py) in your project's top-level directory and add mp-units as a dependency of your project. For example, the simplest file may look as follows:

      conanfile.txt
      [requires]\nmp-units/2.1.0\n\n[options]\nmp-units:cxx_modules=True\n\n[layout]\ncmake_layout\n\n[generators]\nCMakeToolchain\nCMakeDeps\n
    2. Import mp-units and its dependencies definitions to your project's build procedure with find_package:

      find_package(mp-units REQUIRED)\n
    3. Link your CMake targets with mp-units:

      target_link_libraries(<your_target> <PUBLIC|PRIVATE|INTERFACE> mp-units::mp-units)\n
    4. Download, build, and install Conan dependencies before running the CMake configuration step:

      conan install . -pr <your_conan_profile> -s compiler.cppstd=20 -b=missing\ncmake --preset conan-default\ncmake --build --preset conan-release\n
    "},{"location":"getting_started/installation_and_usage/#conan-cmake-live-at-head","title":"Conan + CMake (Live At Head)","text":"

    This chapter describes the procedure to Live At Head, which means using the latest stable version of mp-units all the time.

    Note

    Please note that even though the Conan packages that you will be using are generated ONLY for builds that are considered stable (passed our CI tests), some minor regressions may happen (our CI and C++20 build environment is not perfect yet). Also, please expect that the library interface might, and probably will, change occasionally. Even though we do our best, such changes might not be reflected in the project's documentation right away.

    The procedure is similar to the one described in Conan + CMake (release) with the following differences:

    1. Before starting the previous procedure, add mp-units remote to your Conan configuration:

      conan remote add conan-mpusz https://mpusz.jfrog.io/artifactory/api/conan/conan-oss\n
    2. In your Conan configuration file, provide the package identifier of the mpusz/testing stream:

      conanfile.txt
      [requires]\nmp-units/2.2.0@mpusz/testing\n\n[options]\nmp-units:cxx_modules=True\n\n[layout]\ncmake_layout\n\n[generators]\nCMakeToolchain\nCMakeDeps\n

      Tip

      The identifiers of the latest packages can always be found in the project's README file or on the project's Artifactory.

    3. Force Conan to check for updated recipes with -u:

      conan install . -pr <your_conan_profile> -s compiler.cppstd=20 -b=missing -u\n
    "},{"location":"getting_started/installation_and_usage/#install","title":"Install","text":"

    In case you don't want to use Conan in your project and just want to install the mp-units library on your file system and use find_package(mp-units) from another repository to find it; it is enough to perform the following steps:

    conan install . -pr <your_conan_profile> -s compiler.cppstd=20 -b=missing\nmv CMakeUserPresets.json src\ncd src\ncmake --preset conan-default -DCMAKE_INSTALL_PREFIX=<your_installation_path>\ncmake --build --preset conan-release --target install\n
    "},{"location":"getting_started/installation_and_usage/#contributing-or-just-building-all-the-tests-and-examples","title":"Contributing (or just building all the tests and examples)","text":"

    In case you would like to build all the mp-units source code (with unit tests and examples), you should:

    1. Use the CMakeLists.txt from the top-level directory.
    2. Run Conan with user.mp-units.build:all = True.
    git clone https://github.com/mpusz/mp-units.git && cd units\nconan build . -pr <your_conan_profile> -s compiler.cppstd=23 -o cxx_modules=True -c user.mp-units.build:all=True -b missing\n

    The above will download and install all of the dependencies needed for the development of the library, build all of the source code, and run unit tests.

    If you prefer to build the project via CMake rather than Conan, then you should replace the conan build with conan install command and then follow with a regular CMake build:

    cmake --preset conan-default\ncmake --build --preset conan-release\ncmake --build --preset conan-release --target all_verify_interface_header_sets\ncmake --build --preset conan-release --target test\n
    "},{"location":"getting_started/installation_and_usage/#building-documentation","title":"Building documentation","text":"

    Starting from mp-units 2.0 we are using Material for MkDocs to build our documentation. The easiest way to install all the required dependencies is with pip:

    pip install -U mkdocs-material mkdocs-rss-plugin\n

    Additionally, a Cairo Graphics library is required by Material for MkDocs. Please follow the official MkDocs documentation to install it.

    After that, you can either:

    • easily start a live server to preview the documentation as you write

      mkdocs serve\n
    • build the documentation

      mkdocs build\n
    "},{"location":"getting_started/installation_and_usage/#packaging","title":"Packaging","text":"

    To test CMake installation and Conan packaging or create a Conan package run:

    conan create . --user <username> --channel <channel> -pr <your_conan_profile> -s compiler.cppstd=20 -o cxx_modules=True -c user.mp-units.build:all=True -b missing\n

    The above will create a Conan package and run tests provided in ./test_package directory.

    "},{"location":"getting_started/installation_and_usage/#uploading-mp-units-package-to-the-conan-server","title":"Uploading mp-units package to the Conan server","text":"
    conan upload -r <remote-name> --all mp-units/2.1.0@<user>/<channel>\n
    "},{"location":"getting_started/introduction/","title":"Introduction","text":"

    mp-units is a Modern C++ library that provides compile-time dimensional analysis and unit/quantity manipulation. The initial versions of the library were inspired by the std::chrono::duration but with each release, the interfaces diverged from the original to provide a better user experience.

    Info

    A brief introduction to the library's interfaces and the rationale for changes in version 2.0 of mp-units were provided in detail by Mateusz Pusz in the \"The Power of C++ Templates With mp-units: Lessons Learned & a New Library Design\" talk at the C++ on Sea 2023 conference.

    "},{"location":"getting_started/introduction/#open-source","title":"Open Source","text":"

    mp-units is Free and Open Source, with a permissive MIT license. Check out the source code and issue tracking (for questions and support, reporting bugs, suggesting feature requests and improvements) at https://github.com/mpusz/mp-units.

    "},{"location":"getting_started/introduction/#with-the-users-experience-in-mind","title":"With the User's Experience in Mind","text":"

    Most of the critical design decisions in the library are dictated by the requirement of providing the best user experience possible. Other C++ physical units libraries are \"famous\" for their enormous and hard-to-understand error messages (one line of the error log often does not fit on one slide). The ultimate goal of mp-units is to improve this and make compile-time errors and debugging as easy and user-friendly as possible.

    To achieve this goal, several techniques are applied:

    • usage of C++20 concepts that improve compile-times and the readability of error messages when compared to the traditional template metaprogramming with SFINAE,
    • usage of strong types for framework entities (instead of type aliases),
    • usage of expression templates to improve the readability of generated types,
    • limiting the number of template arguments to the bare minimum.

    Important: It is all about errors

    In many generic C++ libraries, compile-time errors do not happen often. It is hard to break std::string or std::vector in a way that won't compile with a huge error log. Physical quantities and units libraries are different. Generation of compile-time errors is the main reason to use such a library.

    "},{"location":"getting_started/introduction/#key-features","title":"Key Features","text":"Feature Description Safety - The affine space strong types (quantity and quantity_point)- Compile-time checked conversions of quantities and units- Unique support for many quantities of the same kind- Type-safe equations on scalar, vector, and tensor quantities and their units- Value-preserving conversions Performance - All the compile-time logic implemented as immediate (consteval) functions- As fast or even faster than working with fundamental types- No space size overhead needed to implement high-level abstractions Great User Experience - Optimized for readable compilation errors and great debugging experience- Efficient and composable way to specify a unit of choice- Value-based dimension, unit, and quantity equations Feature Rich - Systems of Quantities- Systems of Units- Scalar, vector, and tensor quantities- The affine space- Different models of the universe (e.g. natural units systems)- Strong dimensionless quantities- Strong angular system- Supports any unit's magnitude (huge, small, floating-point)- Faster-than-lightspeed constants- Highly adjustable text-output formatting Easy to Extend - Each entity can be defined with a single line of code- User can easily extend the systems with custom dimensions, quantities, and units Low Standardization Cost - Small number of predefined entities thanks to their composability- No external dependencies (assuming full C++20 support)- No macros in the user interface (besides portability and standard-compliance issues)- Possibility to be standardized as a freestanding part of the C++ Standard Library"},{"location":"getting_started/look_and_feel/","title":"Look and Feel","text":"

    Here is a small example of operations possible on scalar quantities:

    C++ modulesHeader files
    import mp_units;\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\n// simple numeric operations\nstatic_assert(10 * km / 2 == 5 * km);\n\n// conversions to common units\nstatic_assert(1 * h == 3600 * s);\nstatic_assert(1 * km + 1 * m == 1001 * m);\n\n// derived quantities\nstatic_assert(1 * km / (1 * s) == 1000 * m / s);\nstatic_assert(2 * km / h * (2 * h) == 4 * km);\nstatic_assert(2 * km / (2 * km / h) == 1 * h);\n\nstatic_assert(2 * m * (3 * m) == 6 * m2);\n\nstatic_assert(10 * km / (5 * km) == 2 * one);\n\nstatic_assert(1000 / (1 * s) == 1 * kHz);\n
    #include <mp-units/systems/si/si.h>\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\n// simple numeric operations\nstatic_assert(10 * km / 2 == 5 * km);\n\n// conversions to common units\nstatic_assert(1 * h == 3600 * s);\nstatic_assert(1 * km + 1 * m == 1001 * m);\n\n// derived quantities\nstatic_assert(1 * km / (1 * s) == 1000 * m / s);\nstatic_assert(2 * km / h * (2 * h) == 4 * km);\nstatic_assert(2 * km / (2 * km / h) == 1 * h);\n\nstatic_assert(2 * m * (3 * m) == 6 * m2);\n\nstatic_assert(10 * km / (5 * km) == 2 * one);\n\nstatic_assert(1000 / (1 * s) == 1 * kHz);\n

    Try it on Compiler Explorer

    This library requires some C++20 features (concepts and constraints, classes as NTTP, ...). Thanks to them, a user gets a powerful but still easy-to-use interface where all unit conversions and dimensional analysis can be performed without sacrificing accuracy. Please see the below example for a quick preview of basic library features:

    C++ modulesHeader files
    #include <format>\n#include <iomanip>\n#include <iostream>\n#include <print>\nimport mp_units;\n\nusing namespace mp_units;\n\nconstexpr QuantityOf<isq::speed> auto avg_speed(QuantityOf<isq::length> auto d,\n                                                QuantityOf<isq::time> auto t)\n{\n  return d / t;\n}\n\nint main()\n{\n  using namespace mp_units::si::unit_symbols;\n  using namespace mp_units::international::unit_symbols;\n\n  constexpr quantity v1 = 110 * km / h;\n  constexpr quantity v2 = 70 * mph;\n  constexpr quantity v3 = avg_speed(220. * isq::distance[km], 2 * h);\n  constexpr quantity v4 = avg_speed(isq::distance(140. * mi), 2 * h);\n  constexpr quantity v5 = v3.in(m / s);\n  constexpr quantity v6 = value_cast<m / s>(v4);\n  constexpr quantity v7 = value_cast<int>(v6);\n\n  std::cout << v1 << '\\n';                                        // 110 km/h\n  std::cout << std::setw(10) << std::setfill('*') << v2 << '\\n';  // ***70 mi/h\n  std::cout << std::format(\"{:*^10}\\n\", v3);                      // *110 km/h*\n  std::println(\"{:%N in %U}\", v4);                                // 70 in mi/h\n  std::println(\"{:{%N:.2f}%?%U}\", v5);                            // 30.56 m/s\n  std::println(\"{:{%N:.2f}%?{%U:n}}\", v6);                        // 31.29 m s\u207b\u00b9\n  std::println(\"{:%N}\", v7);                                      // 31\n}\n
    #include <mp-units/format.h>\n#include <mp-units/ostream.h>\n#include <mp-units/systems/international/international.h>\n#include <mp-units/systems/isq/isq.h>\n#include <mp-units/systems/si/si.h>\n#include <format>\n#include <iomanip>\n#include <iostream>\n#include <print>\n\nusing namespace mp_units;\n\nconstexpr QuantityOf<isq::speed> auto avg_speed(QuantityOf<isq::length> auto d,\n                                                QuantityOf<isq::time> auto t)\n{\n  return d / t;\n}\n\nint main()\n{\n  using namespace mp_units::si::unit_symbols;\n  using namespace mp_units::international::unit_symbols;\n\n  constexpr quantity v1 = 110 * km / h;\n  constexpr quantity v2 = 70 * mph;\n  constexpr quantity v3 = avg_speed(220. * isq::distance[km], 2 * h);\n  constexpr quantity v4 = avg_speed(isq::distance(140. * mi), 2 * h);\n  constexpr quantity v5 = v3.in(m / s);\n  constexpr quantity v6 = value_cast<m / s>(v4);\n  constexpr quantity v7 = value_cast<int>(v6);\n\n  std::cout << v1 << '\\n';                                        // 110 km/h\n  std::cout << std::setw(10) << std::setfill('*') << v2 << '\\n';  // ***70 mi/h\n  std::cout << std::format(\"{:*^10}\\n\", v3);                      // *110 km/h*\n  std::println(\"{:%N in %U}\", v4);                                // 70 in mi/h\n  std::println(\"{:{%N:.2f}%?%U}\", v5);                            // 30.56 m/s\n  std::println(\"{:{%N:.2f}%?{%U:n}}\", v6);                        // 31.29 m s\u207b\u00b9\n  std::println(\"{:%N}\", v7);                                      // 31\n}\n

    Try it on Compiler Explorer

    Note

    More code examples can be found in the Examples chapter.

    "},{"location":"getting_started/quick_start/","title":"Quick Start","text":"

    This chapter provides a quick introduction to get you started with mp-units. Much more details can be found in our User's Guide.

    "},{"location":"getting_started/quick_start/#quantities","title":"Quantities","text":"

    A quantity is a concrete amount of a unit representing a quantity type of a specified dimension with a specific representation. It is represented in the library with a quantity class template.

    The SI Brochure says:

    SI Brochure

    The value of the quantity is the product of the number and the unit. The space between the number and the unit is regarded as a multiplication sign (just as a space between units implies multiplication).

    Following the above, the value of a quantity in the mp-units library is created by multiplying a number with a predefined unit:

    C++ modulesHeader files
    import mp_units;\n\nusing namespace mp_units;\n\nquantity q = 42 * si::metre / si::second;\n
    #include <mp-units/systems/si/si.h>\n\nusing namespace mp_units;\n\nquantity q = 42 * si::metre / si::second;\n

    Info

    In case someone doesn't like the multiply syntax or there is an ambiguity between operator* provided by this and other libraries, a quantity can also be created with a two-parameter constructor:

    C++ modulesHeader files
    import mp_units;\n\nusing namespace mp_units;\n\nquantity q{42, si::metre / si::second};\n
    #include <mp-units/systems/si/si.h>\n\nusing namespace mp_units;\n\nquantity q{42, si::metre / si::second};\n

    The above creates an instance of quantity<derived_unit<si::metre, per<si::second>>{}, int>. The same can be obtained using optional unit symbols:

    C++ modulesHeader files
    import mp_units;\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\nquantity q = 42 * m / s;\n
    #include <mp-units/systems/si/si.h>\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\nquantity q = 42 * m / s;\n

    Important

    Unit symbols introduce a lot of short identifiers into the current scope, which may cause naming collisions with unrelated but already existing identifiers in the code base. This is why unit symbols are opt-in.

    A user has several options here to choose from depending on the required scenario and possible naming conflicts:

    • explicitly \"import\" all of them from a dedicated unit_symbols namespace with a using-directive:

      using namespace mp_units;\nusing namespace mp_units::si::unit_symbols;  // imports all the SI symbols at once\n\nquantity q = 42 * m / s;\n
    • selectively select only the required and not-conflicting ones with using-declarations:

      using namespace mp_units;\nusing si::unit_symbols::m;\nusing si::unit_symbols::s;\n\nquantity q = 42 * m / s;\n
    • specify a custom not conflicting unit identifier for a unit:

      using namespace mp_units;\nconstexpr Unit auto mps = si::metre / si::second;\n\nquantity q = 42 * mps;\n

    Quantities of the same kind can be added, subtracted, and compared to each other:

    C++ modulesHeader files
    import mp_units;\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\nstatic_assert(1 * km + 50 * m == 1050 * m);\n
    #include <mp-units/systems/si/si.h>\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\nstatic_assert(1 * km + 50 * m == 1050 * m);\n

    Various quantities can be multiplied or divided by each other:

    static_assert(140 * km / (2 * h) == 70 * km / h);\n

    Note

    In case you wonder why this library does not use UDLs to create quantities, please check our FAQ.

    "},{"location":"getting_started/quick_start/#quantity-points","title":"Quantity points","text":"

    The quantity point specifies an absolute quantity with respect to an origin. If no origin is provided explicitly, an implicit one will be provided by the library.

    Together with quantities, they model The Affine Space.

    Quantity points should be used in all places where adding two values is meaningless (e.g., temperature points, timestamps, altitudes, readouts from the car's odometer, etc.).

    The set of operations that can be done on quantity points is limited compared to quantities. This introduces an additional type-safety.

    C++ modulesHeader files
    #include <print>\nimport mp_units;\n\nint main()\n{\n  using namespace mp_units;\n  using namespace mp_units::si::unit_symbols;\n  using namespace mp_units::usc::unit_symbols;\n\n  quantity_point temp{20. * deg_C};\n  std::println(\"Temperature: {} ({})\",\n               temp.quantity_from_zero(),\n               temp.in(deg_F).quantity_from_zero());\n}\n
    #include <mp-units/format.h>\n#include <mp-units/systems/si/si.h>\n#include <mp-units/systems/usc/usc.h>\n#include <print>\n\nint main()\n{\n  using namespace mp_units;\n  using namespace mp_units::si::unit_symbols;\n  using namespace mp_units::usc::unit_symbols;\n\n  quantity_point temp{20. * deg_C};\n  std::println(\"Temperature: {} ({})\",\n               temp.quantity_from_zero(),\n               temp.in(deg_F).quantity_from_zero());\n}\n

    The above outputs:

    Temperature: 20 \u00b0C (68 \u00b0F)\n

    Info

    Check The Affine Space chapter to learn more about quantity points.

    "},{"location":"users_guide/terms_and_definitions/","title":"Terms and Definitions","text":"

    The mp-units project consistently uses the official metrology vocabulary defined by the ISO and BIPM. You can find essential project-related definitions in our documentation's \"Glossary\" chapter. Even more, terms are provided in the official metrology vocabulary of the ISO and BIPM.

    Tip

    Please familiarize yourself with terms from \"Glossary\" to better understand the documentation and improve domain-related communication and discussions.

    "},{"location":"users_guide/examples/avg_speed/","title":"avg_speed","text":"

    Try it on Compiler Explorer

    Let's continue the previous example. This time, our purpose will not be to showcase as many library features as possible, but we will scope on different interfaces one can provide with the mp-units. We will also describe some advantages and disadvantages of presented solutions.

    First, we either import a module or include all the necessary header files and import all the identifiers from the mp_units namespace:

    avg_speed.cpp
    #include <exception>\n#include <iostream>\n#ifdef MP_UNITS_MODULES\nimport mp_units;\n#else\n#include <mp-units/ostream.h>\n#include <mp-units/systems/cgs/cgs.h>\n#include <mp-units/systems/international/international.h>\n#include <mp-units/systems/isq/space_and_time.h>\n#include <mp-units/systems/si/unit_symbols.h>\n#endif\n\nnamespace {\n\nusing namespace mp_units;\n

    Next, we define two functions calculating average speed based on quantities of fixed units and integral and floating-point representation types, respectively, and a third function that we introduced in the previous example:

    avg_speed.cpp
    constexpr quantity<si::metre / si::second, int> fixed_int_si_avg_speed(quantity<si::metre, int> d,\n                                                                       quantity<si::second, int> t)\n{\n  return d / t;\n}\n\nconstexpr quantity<si::metre / si::second> fixed_double_si_avg_speed(quantity<si::metre> d, quantity<si::second> t)\n{\n  return d / t;\n}\n\nconstexpr QuantityOf<isq::speed> auto avg_speed(QuantityOf<isq::length> auto d, QuantityOf<isq::time> auto t)\n{\n  return d / t;\n}\n

    We also added a simple utility to print our results:

    avg_speed.cpp
    template<QuantityOf<isq::length> D, QuantityOf<isq::time> T, QuantityOf<isq::speed> V>\nvoid print_result(D distance, T duration, V speed)\n{\n  const auto result_in_kmph = speed.force_in(si::kilo<si::metre> / non_si::hour);\n  std::cout << \"Average speed of a car that makes \" << distance << \" in \" << duration << \" is \" << result_in_kmph\n            << \".\\n\";\n}\n

    Now, let's analyze how those three utility functions behave with different sets of arguments. First, we are going to use quantities of SI units and integral representation:

    avg_speed.cpp
    void example()\n{\n  using namespace mp_units::si::unit_symbols;\n\n  // SI (int)\n  {\n    constexpr auto distance = 220 * km;\n    constexpr auto duration = 2 * h;\n\n    std::cout << \"SI units with 'int' as representation\\n\";\n\n    print_result(distance, duration, fixed_int_si_avg_speed(distance, duration));\n    print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));\n    print_result(distance, duration, avg_speed(distance, duration));\n  }\n

    The above provides the following output:

    SI units with 'int' as representation\nAverage speed of a car that makes 220 km in 2 h is 108 km/h.\nAverage speed of a car that makes 220 km in 2 h is 110 km/h.\nAverage speed of a car that makes 220 km in 2 h is 110 km/h.\n

    Please note that in the first two cases, we must convert length from km to m and time from h to s. The converted values are used to calculate speed in m/s which is then again converted to the one in km/h. Those conversions not only impact the application's runtime performance but may also affect the precision of the final result. Such truncation can be easily observed in the first case where we deal with integral representation types (the resulting speed is 108 km/h).

    The second scenario is really similar to the previous one, but this time, function arguments have floating-point representation types:

    avg_speed.cpp
      // SI (double)\n  {\n    constexpr auto distance = 220. * km;\n    constexpr auto duration = 2. * h;\n\n    std::cout << \"\\nSI units with 'double' as representation\\n\";\n\n    // conversion from a floating-point to an integral type is a truncating one so an explicit cast is needed\n    print_result(distance, duration, fixed_int_si_avg_speed(value_cast<int>(distance), value_cast<int>(duration)));\n    print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));\n    print_result(distance, duration, avg_speed(distance, duration));\n  }\n

    Conversion from floating-point to integral representation types is considered value-truncating and that is why now, in the first case, we need an explicit call to value_cast<int>.

    In the text output, we can observe that, again, the resulting value gets truncated during conversions in the first cast:

    SI units with 'double' as representation\nAverage speed of a car that makes 220 km in 2 h is 108 km/h.\nAverage speed of a car that makes 220 km in 2 h is 110 km/h.\nAverage speed of a car that makes 220 km in 2 h is 110 km/h.\n

    Next, let's do the same for integral and floating-point representations, but this time using international mile:

    avg_speed.cpp
      // International mile (int)\n  {\n    using namespace mp_units::international::unit_symbols;\n\n    constexpr auto distance = 140 * mi;\n    constexpr auto duration = 2 * h;\n\n    std::cout << \"\\nInternational mile with 'int' as representation\\n\";\n\n    // it is not possible to make a lossless conversion of miles to meters on an integral type\n    // (explicit cast needed)\n    print_result(distance, duration, fixed_int_si_avg_speed(distance.force_in(m), duration));\n    print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));\n    print_result(distance, duration, avg_speed(distance, duration));\n  }\n\n  // International mile (double)\n  {\n    using namespace mp_units::international::unit_symbols;\n\n    constexpr auto distance = 140. * mi;\n    constexpr auto duration = 2. * h;\n\n    std::cout << \"\\nInternational mile with 'double' as representation\\n\";\n\n    // conversion from a floating-point to an integral type is a truncating one so an explicit cast is needed\n    // also it is not possible to make a lossless conversion of miles to meters on an integral type\n    // (explicit cast needed)\n    print_result(distance, duration, fixed_int_si_avg_speed(value_cast<m, int>(distance), value_cast<int>(duration)));\n    print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));\n    print_result(distance, duration, avg_speed(distance, duration));\n  }\n

    One important difference here is the fact that as it is not possible to make a lossless conversion of miles to meters on a quantity using an integral representation type, so this time, we need a value_cast<m, int> to force it.

    If we check the text output of the above, we will see the following:

    International mile with 'int' as representation\nAverage speed of a car that makes 140 mi in 2 h is 111 km/h.\nAverage speed of a car that makes 140 mi in 2 h is 112.654 km/h.\nAverage speed of a car that makes 140 mi in 2 h is 112 km/h.\n\nInternational mile with 'double' as representation\nAverage speed of a car that makes 140 mi in 2 h is 111 km/h.\nAverage speed of a car that makes 140 mi in 2 h is 112.654 km/h.\nAverage speed of a car that makes 140 mi in 2 h is 112.654 km/h.\n

    Please note how the first and third results get truncated using integral representation types.

    In the end, we repeat the scenario for CGS units:

    avg_speed.cpp
      {\n    constexpr auto distance = 22'000'000 * cgs::centimetre;\n    constexpr auto duration = 7200 * cgs::second;\n\n    std::cout << \"\\nCGS units with 'int' as representation\\n\";\n\n    // it is not possible to make a lossless conversion of centimeters to meters on an integral type\n    // (explicit cast needed)\n    print_result(distance, duration, fixed_int_si_avg_speed(distance.force_in(m), duration));\n    print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));\n    print_result(distance, duration, avg_speed(distance, duration));\n  }\n\n  // CGS (double)\n  {\n    constexpr auto distance = 22'000'000. * cgs::centimetre;\n    constexpr auto duration = 7200. * cgs::second;\n\n    std::cout << \"\\nCGS units with 'double' as representation\\n\";\n\n    // conversion from a floating-point to an integral type is a truncating one so an explicit cast is needed\n    // it is not possible to make a lossless conversion of centimeters to meters on an integral type\n    // (explicit cast needed)\n    print_result(distance, duration, fixed_int_si_avg_speed(value_cast<m, int>(distance), value_cast<int>(duration)));\n\n    print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));\n    print_result(distance, duration, avg_speed(distance, duration));\n  }\n}\n\n}  // namespace\n

    Again, we observe value_cast being used in the same places and consistent truncation errors in the text output:

    CGS units with 'int' as representation\nAverage speed of a car that makes 22000000 cm in 7200 s is 108 km/h.\nAverage speed of a car that makes 22000000 cm in 7200 s is 110 km/h.\nAverage speed of a car that makes 22000000 cm in 7200 s is 109 km/h.\n\nCGS units with 'double' as representation\nAverage speed of a car that makes 2.2e+07 cm in 7200 s is 108 km/h.\nAverage speed of a car that makes 2.2e+07 cm in 7200 s is 110 km/h.\nAverage speed of a car that makes 2.2e+07 cm in 7200 s is 110 km/h.\n

    The example file ends with a simple main() function:

    avg_speed.cpp
    int main()\n{\n  try {\n    example();\n  } catch (const std::exception& ex) {\n    std::cerr << \"Unhandled std exception caught: \" << ex.what() << '\\n';\n  } catch (...) {\n    std::cerr << \"Unhandled unknown exception caught\\n\";\n  }\n}\n
    ","tags":["CGS System","International System","Text Formatting"]},{"location":"users_guide/examples/hello_units/","title":"hello_units","text":"

    Try it on Compiler Explorer

    This is a really simple example showcasing the features of the mp-units library.

    First, we either import the mp_units module or include the headers for:

    • an International System of Quantities (ISQ)
    • an International System of units (SI)
    • units derived from the International Yard and Pound
    • text formatting and stream output support
    hello_units.cpp
    #include <mp-units/compat_macros.h>\n#include <iomanip>\n#include <iostream>\n#ifdef MP_UNITS_MODULES\nimport mp_units;\n#else\n#include <mp-units/format.h>\n#include <mp-units/ostream.h>\n#include <mp-units/systems/international/international.h>\n#include <mp-units/systems/isq/isq.h>\n#include <mp-units/systems/si/si.h>\n#endif\n

    Also, to shorten the definitions, we \"import\" all the symbols from the mp_units namespace.

    hello_units.cpp
    using namespace mp_units;\n

    Next, we define a simple function that calculates the average speed based on the provided arguments of length and time:

    hello_units.cpp
    constexpr QuantityOf<isq::speed> auto avg_speed(QuantityOf<isq::length> auto d, QuantityOf<isq::time> auto t)\n{\n  return d / t;\n}\n

    The above function template takes any quantities implicitly convertible to isq::length and isq::time, respectively. Those quantities can use any compatible unit and a representation type. The function returns a result of a straightforward equation and ensures that its quantity type is implicitly convertible to isq::speed.

    Tip

    Besides verifying the type returned from the function, constraining a generic return 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.

    hello_units.cpp
    int main()\n{\n  using namespace mp_units::si::unit_symbols;\n  using namespace mp_units::international::unit_symbols;\n

    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.

    hello_units.cpp
      constexpr quantity v1 = 110 * km / h;\n  constexpr quantity v2 = 70 * mph;\n  constexpr quantity v3 = avg_speed(220. * km, 2 * h);\n  constexpr quantity v4 = avg_speed(isq::distance(140. * mi), 2 * isq::duration[h]);\n  constexpr quantity v5 = v3.in(m / s);\n  constexpr quantity v6 = value_cast<m / s>(v4);\n  constexpr quantity v7 = value_cast<int>(v6);\n
    • Lines 21 & 22 create a quantity of kind isq::length / isq::time with the numbers and units provided. Such quantities can be converted or assigned to any other quantity with a matching kind.
    • Line 23 calls our function template with quantities of kind isq::length and isq::time and number and units provided.
    • Line 24 explicitly provides quantity types of the quantities passed to a function template. This time, those will not be quantity kinds anymore and will have more restrictive conversion rules.
    • Line 25 changes the unit of a quantity v3 to m / s in a value-preserving way (floating-point representations are considered to be value-preserving).
    • Line 26 does a similar operation, but this time, it would also succeed for value-truncating cases (if that was the case).
    • Line 27 does a value-truncating conversion of changing the underlying representation type from double to int.
    hello_units.cpp
      std::cout << v1 << '\\n';                                             // 110 km/h\n  std::cout << std::setw(10) << std::setfill('*') << v2 << '\\n';       // ***70 mi/h\n  std::cout << MP_UNITS_STD_FMT::format(\"{:*^10}\\n\", v3);              // *110 km/h*\n  std::cout << MP_UNITS_STD_FMT::format(\"{:%N in %U}\\n\", v4);          // 70 in mi/h\n  std::cout << MP_UNITS_STD_FMT::format(\"{:{%N:.2f}%?%U}\\n\", v5);      // 30.56 in m/s\n  std::cout << MP_UNITS_STD_FMT::format(\"{:{%N:.2f}%?{%U:n}}\\n\", v6);  // 31.29 in m s\u207b\u00b9\n  std::cout << MP_UNITS_STD_FMT::format(\"{:%N}\\n\", v7);                // 31\n}\n

    The above presents various ways to print a quantity. Both stream insertion operations and std::format facilities are supported.

    Tip

    MP_UNITS_STD_FMT is used for compatibility reasons. If a specific compiler does not support std::format or a user prefers to use the {fmt} library, this macro will resolve to fmt namespace. Otherwise, the std namespace will be used.

    ","tags":["International System","Text Formatting"]},{"location":"users_guide/examples/si_constants/","title":"si_constants","text":"

    Try it on Compiler Explorer

    The next example presents all the seven defining constants of the SI system. We can observe how Faster-than-lightspeed Constants work in practice.

    si_constants.cpp
    #include <mp-units/compat_macros.h>\n#include <iostream>\n#ifdef MP_UNITS_MODULES\nimport mp_units;\n#else\n#include <mp-units/format.h>\n#include <mp-units/systems/si/si.h>\n#endif\n\ntemplate<class T>\n  requires mp_units::is_scalar<T>\ninline constexpr bool mp_units::is_vector<T> = true;\n

    As always, we start with the inclusion of all the needed header files. After that, for the simplicity of this example, we hack the character of quantities to be able to express vector quantities with simple scalar types.

    si_constants.cpp
    int main()\n{\n  using namespace mp_units;\n  using namespace mp_units::si;\n  using namespace mp_units::si::unit_symbols;\n\n  std::cout << \"The seven defining constants of the SI and the seven corresponding units they define:\\n\";\n  std::cout << MP_UNITS_STD_FMT::format(\"- hyperfine transition frequency of Cs: {} = {:{%N:.0} %U}\\n\",\n                                        1. * si2019::hyperfine_structure_transition_frequency_of_cs,\n                                        (1. * si2019::hyperfine_structure_transition_frequency_of_cs).in(Hz));\n  std::cout << MP_UNITS_STD_FMT::format(\"- speed of light in vacuum:             {} = {:{%N:.0} %U}\\n\",\n                                        1. * si2019::speed_of_light_in_vacuum,\n                                        (1. * si2019::speed_of_light_in_vacuum).in(m / s));\n  std::cout << MP_UNITS_STD_FMT::format(\"- Planck constant:                      {} = {:{%N:.8e} %U}\\n\",\n                                        1. * si2019::planck_constant, (1. * si2019::planck_constant).in(J * s));\n  std::cout << MP_UNITS_STD_FMT::format(\"- elementary charge:                    {} = {:{%N:.9e} %U}\\n\",\n                                        1. * si2019::elementary_charge, (1. * si2019::elementary_charge).in(C));\n  std::cout << MP_UNITS_STD_FMT::format(\"- Boltzmann constant:                   {} = {:{%N:.6e} %U}\\n\",\n                                        1. * si2019::boltzmann_constant, (1. * si2019::boltzmann_constant).in(J / K));\n  std::cout << MP_UNITS_STD_FMT::format(\"- Avogadro constant:                    {} = {:{%N:.8e} %U}\\n\",\n                                        1. * si2019::avogadro_constant, (1. * si2019::avogadro_constant).in(one / mol));\n  std::cout << MP_UNITS_STD_FMT::format(\"- luminous efficacy:                    {} = {}\\n\",\n                                        1. * si2019::luminous_efficacy, (1. * si2019::luminous_efficacy).in(lm / W));\n}\n

    The main part of the example prints all of the SI-defining constants. While analyzing the output of this program (provided below), we can easily notice that a direct printing of the quantity provides just a value 1 with a proper constant symbol. This is the main power of the Faster-than-lightspeed Constants feature. Only after we explicitly convert the unit of a quantity to proper SI units we get an actual numeric value of the constant.

    The seven defining constants of the SI and the seven corresponding units they define:\n- hyperfine transition frequency of Cs: 1 \u0394\u03bd_Cs = 9192631770 Hz\n- speed of light in vacuum:             1 c = 299792458 m/s\n- Planck constant:                      1 h = 6.62607015e-34 J s\n- elementary charge:                    1 e = 1.602176634e-19 C\n- Boltzmann constant:                   1 k = 1.380649e-23 J/K\n- Avogadro constant:                    1 N_A = 6.02214076e+23 1/mol\n- luminous efficacy:                    1 K_cd = 683 lm/W\n
    ","tags":["Physical Constants","Text Formatting"]},{"location":"users_guide/examples/tags_index/","title":"Tags Index","text":"

    Note

    mp-units usage example applications are meant to be built on all of the supported compilers. This is why they benefit from the Wide Compatibility mode.

    Tip

    All usage examples in this chapter are categorized with appropriate tags to simplify navigation and search of relevant code. You can either read all the examples one-by-one in the order provided by the documentation authors or, thanks to the tagging system, jump straight to the example that is the most interesting for you.

    "},{"location":"users_guide/examples/tags_index/#cgs-system","title":"CGS System","text":"
    • avg_speed
    "},{"location":"users_guide/examples/tags_index/#international-system","title":"International System","text":"
    • avg_speed
    • hello_units
    "},{"location":"users_guide/examples/tags_index/#physical-constants","title":"Physical Constants","text":"
    • si_constants
    "},{"location":"users_guide/examples/tags_index/#text-formatting","title":"Text Formatting","text":"
    • avg_speed
    • hello_units
    • si_constants
    "},{"location":"users_guide/framework_basics/character_of_a_quantity/","title":"Character of a Quantity","text":"

    Warning

    This chapter's features are experimental and subject to change or removal. Please share your feedback if something seems wrong or could be improved.

    "},{"location":"users_guide/framework_basics/character_of_a_quantity/#scalars-vectors-and-tensors","title":"Scalars, vectors, and tensors","text":"

    ISO 80000-2

    Scalars, vectors and tensors are mathematical objects that can be used to denote certain physical quantities and their values. They are as such independent of the particular choice of a coordinate system, whereas each scalar component of a vector or a tensor and each component vector and component tensor depend on that choice.

    Such distinction is important because each quantity character represents different properties and allows different operations to be done on its quantities.

    For example, imagine a physical units library that allows the creation of a \\(speed\\) quantity from both \\(length / time\\) and \\(length * time\\). It wouldn't be too safe to use such a product, right?

    Now we have to realize that both of the above operations (multiplication and division) are not even mathematically defined for linear algebra types such as vectors or tensors. On the other hand, two vectors can be passed as arguments to dot and cross-product operations. The result of the first one is a scalar. The second one results in a vector that is perpendicular to both vectors passed as arguments. Again, it wouldn't be safe to allow replacing those two operations with each other or expect the same results from both cases. This simply can't work.

    "},{"location":"users_guide/framework_basics/character_of_a_quantity/#isq-defines-quantities-of-all-characters","title":"ISQ defines quantities of all characters","text":"

    While defining quantities ISO 80000 explicitly mentions when a specific quantity has a vector or tensor character. Here are some examples:

    Quantity Character Quantity Equation \\(duration\\) scalar {base quantity} \\(mass\\) scalar {base quantity} \\(length\\) scalar {base quantity} \\(path\\; length\\) scalar {base quantity} \\(radius\\) scalar {base quantity} \\(position\\; vector\\) vector {base quantity} \\(velocity\\) vector \\(position\\; vector / duration\\) \\(acceleration\\) vector \\(velocity / duration\\) \\(force\\) vector \\(mass * acceleration\\) \\(power\\) scalar \\(force \\cdot velocity\\) \\(moment\\; of\\; force\\) vector \\(position\\; vector \\times force\\) \\(torque\\) scalar \\(moment\\; of\\; force \\cdot \\{unit\\; vector\\}\\) \\(surface\\; tension\\) scalar \\(\\lvert force \\rvert / length\\) \\(angular\\; displacement\\) scalar \\(path\\; length / radius\\) \\(angular\\; velocity\\) vector \\(angular\\; displacement / duration * \\{unit\\; vector\\}\\) \\(momentum\\) vector \\(mass * velocity\\) \\(angular\\; momentum\\) vector \\(position\\; vector \\times momentum\\) \\(moment\\; of\\; inertia\\) tensor \\(angular\\; momentum \\otimes angular\\; velocity\\)

    In the above equations:

    • \\(a * b\\) - regular multiplication where one of the arguments has to be scalar
    • \\(a / b\\) - regular division where the divisor has to be scalar
    • \\(a \\cdot b\\) - dot product of two vectors
    • \\(a \\times b\\) - cross product of two vectors
    • \\(\\lvert a \\rvert\\) - magnitude of a vector
    • \\(\\{unit\\; vector\\}\\) - a special vector with the magnitude of \\(1\\)
    • \\(a \\otimes b\\) - tensor product of two vectors or tensors

    Note

    As of now, all of the C++ physical units libraries on the market besides mp-units do not support the operations mentioned above. They expose only multiplication and division operators, which do not work for linear algebra-based representation types. If a user of those libraries would like to create the quantities provided in the above table properly, this would result in a compile-time error stating that multiplication and division of two linear algebra vectors is impossible.

    "},{"location":"users_guide/framework_basics/character_of_a_quantity/#characters-dont-apply-to-dimensions-and-units","title":"Characters don't apply to dimensions and units","text":"

    ISO 80000 explicitly states that dimensions are orthogonal to quantity characters:

    ISO 80000-1:2009

    In deriving the dimension of a quantity, no account is taken of its scalar, vector, or tensor character.

    Also, it explicitly states that:

    ISO 80000-2

    All units are scalars.

    "},{"location":"users_guide/framework_basics/character_of_a_quantity/#defining-vector-and-tensor-quantities","title":"Defining vector and tensor quantities","text":"

    To specify that a specific quantity has a vector or tensor character a value of quantity_character enumeration can be appended to the quantity_spec describing such a quantity type:

    C++23C++20Portable
    inline constexpr struct position_vector : quantity_spec<length, quantity_character::vector> {} position_vector;\ninline constexpr struct displacement : quantity_spec<length, quantity_character::vector> {} displacement;\n
    inline constexpr struct position_vector : quantity_spec<position_vector, length, quantity_character::vector> {} position_vector;\ninline constexpr struct displacement : quantity_spec<displacement, length, quantity_character::vector> {} displacement;\n
    QUANTITY_SPEC(position_vector, length, quantity_character::vector);\nQUANTITY_SPEC(displacement, length, quantity_character::vector);\n

    With the above, all the quantities derived from position_vector or displacement will have a correct character determined according to the kind of operations included in the quantity equation defining a derived quantity.

    For example, velocity in the below definition will be defined as a vector quantity (no explicit character override is needed):

    C++23C++20Portable
    inline constexpr struct velocity : quantity_spec<speed, position_vector / duration> {} velocity;\n
    inline constexpr struct velocity : quantity_spec<velocity, speed, position_vector / duration> {} velocity;\n
    QUANTITY_SPEC(velocity, speed, position_vector / duration);\n
    "},{"location":"users_guide/framework_basics/character_of_a_quantity/#representation-types-for-vector-and-tensor-quantities","title":"Representation types for vector and tensor quantities","text":"

    As we remember, the quantity class template is defined as follows:

    template<Reference auto R,\n         RepresentationOf<get_quantity_spec(R).character> Rep = double>\nclass quantity;\n

    The second template parameter is constrained with a RepresentationOf concept that checks if the provided representation type satisfies the requirements for the character associated with this quantity type.

    Note

    The current version of the C++ Standard Library does not provide any types that could be used as a representation type for vector and tensor quantities. This is why users are on their own here .

    To provide examples and implement unit tests, our library uses the types proposed in the P1385 and available as a Conan package in the Conan Center. However, thanks to the provided customization points, any linear algebra library types can be used as a vector or tensor quantity representation type.

    To enable the usage of a user-defined type as a representation type for vector or tensor quantities, we need to provide a partial specialization of is_vector or is_tensor customization points.

    For example, here is how it can be done for the P1385 types:

    #include <matrix>\n\nusing la_vector = STD_LA::fixed_size_column_vector<double, 3>;\n\ntemplate<>\ninline constexpr bool mp_units::is_vector<la_vector> = true;\n

    With the above, we can use la_vector as a representation type for our quantity:

    Quantity auto q = la_vector{1, 2, 3} * isq::velocity[m / s];\n

    In case there is an ambiguity of operator* between mp-units and a linear algebra library, we can either:

    • use two-parameter constructor

      Quantity auto q = quantity{la_vector{1, 2, 3}, isq::velocity[m / s]};\n
    • provide a dedicated overload of operator* that will resolve the ambiguity and wrap the above

      template<Reference R>\nQuantity auto operator*(la_vector rep, R)\n{\n  return quantity{rep, R{}};\n}\n

    Note

    The following does not work:

    Quantity auto q1 = la_vector{1, 2, 3} * m / s;\nQuantity auto q2 = isq::velocity(la_vector{1, 2, 3} * m / s);\nquantity<isq::velocity[m/s]> q3{la_vector{1, 2, 3} * m / s};\n

    In all the cases above, the SI unit m / s has an associated scalar quantity of isq::length / isq::time. la_vector is not a correct representation type for a scalar quantity so the construction fails.

    "},{"location":"users_guide/framework_basics/character_of_a_quantity/#hacking-the-character","title":"Hacking the character","text":"

    Sometimes we want to use a vector quantity, but we don't care about its direction. For example, the standard gravity acceleration constant always points down, so we might not care about this in a particular scenario. In such a case, we may want to \"hack\" the library to allow scalar types to be used as a representation type for scalar quantities.

    For example, we can do the following:

    template<class T>\n  requires mp_units::is_scalar<T>\ninline constexpr bool mp_units::is_vector<T> = true;\n

    which says that every type that can be used as a scalar representation is also allowed for vector quantities.

    Doing the above is actually not such a big \"hack\" as the ISO 80000 explicitly allows it:

    ISO 80000-2

    A vector is a tensor of the first order and a scalar is a tensor of order zero.

    Despite it being allowed by ISO 80000, for type-safety reasons, we do not allow such a behavior by default, and a user has to opt into such scenarios explicitly.

    "},{"location":"users_guide/framework_basics/concepts/","title":"Concepts","text":"

    This chapter enumerates all the user-facing concepts in the mp-units library.

    "},{"location":"users_guide/framework_basics/concepts/#Dimension","title":"Dimension<T>","text":"

    Dimension concept matches a dimension of either a base or derived quantity:

    • Base dimensions are explicitly defined by the user by inheriting from the instantiation of a base_dimension class template. It should be instantiated with a unique symbol identifier describing this dimension in a specific system of quantities.
    • Derived dimensions are implicitly created by the library's framework based on the quantity equation provided in the quantity specification.
    "},{"location":"users_guide/framework_basics/concepts/#DimensionOf","title":"DimensionOf<T, V>","text":"

    DimensionOf concept is satisfied when both arguments satisfy a Dimension concept and when they compare equal.

    "},{"location":"users_guide/framework_basics/concepts/#QuantitySpec","title":"QuantitySpec<T>","text":"

    QuantitySpec concept matches all the quantity specifications including:

    • Base quantities defined by a user by inheriting from the quantity_spec class template instantiated with a base dimension argument.
    • Derived named quantities defined by a user by inheriting from the quantity_spec class template instantiated with a result of a quantity equation passed as an argument.
    • Other named quantities forming a hierarchy of quantities of the same kind defined by a user by inheriting from the quantity_spec class template instantiated with another \"parent\" quantity specification passed as an argument.
    • Quantity kinds describing a family of mutually comparable quantities.
    • Intermediate derived quantity specifications being a result of a quantity equations on other specifications.
    "},{"location":"users_guide/framework_basics/concepts/#QuantitySpecOf","title":"QuantitySpecOf<T, V>","text":"

    QuantitySpecOf concept is satisfied when both arguments satisfy a QuantitySpec concept and when T is implicitly convertible to V.

    More details

    Additionally:

    • T should not be a nested quantity specification of V
    • either T is quantity kind or V should not be a nested quantity specification of T

    Those additional conditions are required to make the following work:

    static_assert(ReferenceOf<si::radian, isq::angular_measure>);\nstatic_assert(!ReferenceOf<si::radian, dimensionless>);\nstatic_assert(!ReferenceOf<isq::angular_measure[si::radian], dimensionless>);\nstatic_assert(ReferenceOf<one, isq::angular_measure>);\nstatic_assert(!ReferenceOf<dimensionless[one], isq::angular_measure>);\n
    "},{"location":"users_guide/framework_basics/concepts/#Unit","title":"Unit<T>","text":"

    Unit concept matches all the units in the library including:

    • Base units defined by a user by inheriting from the named_unit class template instantiated with a unique symbol identifier describing this unit in a specific system of units.
    • Named scaled units defined by a user by inheriting from the named_unit class template instantiated with a unique symbol identifier and a product of multiplying another unit with some magnitude.
    • Prefixed units defined by a user by inheriting from the prefixed_unit class template instantiated with a prefix symbol, a magnitude, and a unit to be prefixed.
    • Derived named units defined by a user by inheriting from the named_unit class template instantiated with a unique symbol identifier and a result of unit equation passed as an argument.
    • Derived unnamed units being a result of a unit equations on other units.

    Note

    In the mp-units library, physical constants are also implemented as units.

    "},{"location":"users_guide/framework_basics/concepts/#AssociatedUnit","title":"AssociatedUnit<T>","text":"

    AssociatedUnit concept describes a unit with an associated quantity and is satisfied by:

    • All units derived from a named_unit class template instantiated with a unique symbol identifier and a QuantitySpec of a quantity kind.
    • All units being a result of unit equations on other associated units.
    Examples

    All units in the SI have associated quantities. For example, si::second is specified to measure isq::time.

    Natural units typically do not have an associated quantity. For example, if we assume c = 1, a natural::second unit can be used to measure both time and length. In such case, speed would have a unit of one.

    "},{"location":"users_guide/framework_basics/concepts/#PrefixableUnit","title":"PrefixableUnit<T>","text":"

    PrefixableUnit concept is satisfied by all units derived from a named_unit class template for which a customization point unit_can_be_prefixed<T{}> was not explicitly set to false. Such units can be passed as an argument to a prefixed_unit class template.

    Examples

    All units in the SI can be prefixed with SI-defined prefixes.

    Some off-system units like non_si::day can't be prefixed. To enforce that, the following has to be provided:

    template<> inline constexpr bool unit_can_be_prefixed<non_si::day> = false;\n
    "},{"location":"users_guide/framework_basics/concepts/#UnitOf","title":"UnitOf<T, V>","text":"

    UnitOf concept is satisfied for all units T matching an AssociatedUnit concept with an associated quantity type implicitly convertible to V.

    More details

    Additionally, the kind of V and the kind of quantity type associated with T must be the same, or the quantity type associated with T may not be derived from the kind of V.

    This condition is required to make dimensionless[si::radian] invalid as si::radian should be only used for isq::angular_measure, which is a nested quantity kind within the dimensionless quantities tree.

    "},{"location":"users_guide/framework_basics/concepts/#UnitCompatibleWith","title":"UnitCompatibleWith<T, V1, V2>","text":"

    UnitCompatibleWith concept is satisfied for all units T when:

    • V1 is a Unit,
    • V2 is a QuantitySpec,
    • T and V1 are defined in terms of the same reference unit,
    • if T is an AssociatedUnit it should satisfy UnitOf<V2>.
    "},{"location":"users_guide/framework_basics/concepts/#Reference","title":"Reference<T>","text":"

    Reference concept is satisfied by all quantity reference types. Such types provide all the meta-information required to create a Quantity. A Reference can either be:

    • An AssociatedUnit.
    • The instantiation of a reference class template with a QuantitySpec passed as the first template argument and a Unit passed as the second one.
    "},{"location":"users_guide/framework_basics/concepts/#ReferenceOf","title":"ReferenceOf<T, V>","text":"

    ReferenceOf concept is satisfied by references T which have a quantity specification that satisfies QuantitySpecOf<V> concept. |

    "},{"location":"users_guide/framework_basics/concepts/#Representation","title":"Representation<T>","text":"

    Representation concept constraints a type of a number that stores the value of a quantity.

    "},{"location":"users_guide/framework_basics/concepts/#RepresentationOf","title":"RepresentationOf<T, Ch>","text":"

    RepresentationOf concept is satisfied by all Representation types that are of a specified quantity character Ch.

    A user can declare a custom representation type to be of a specific character by providing the specialization with true for one or more of the following variable templates:

    • is_scalar<T>
    • is_vector<T>
    • is_tensor<T>
    Tip

    If we want to use scalar types to also express vector quantities (e.g., ignoring the \"direction\" of the vector) the following definition can be provided to enable such a behavior:

    template<class T>\n  requires mp_units::is_scalar<T>\ninline constexpr bool mp_units::is_vector<T> = true;\n
    "},{"location":"users_guide/framework_basics/concepts/#Quantity","title":"Quantity<T>","text":"

    Quantity concept matches every quantity in the library and is satisfied by all types being or deriving from an instantiation of a quantity class template.

    "},{"location":"users_guide/framework_basics/concepts/#QuantityOf","title":"QuantityOf<T, V>","text":"

    QuantityOf concept is satisfied by all the quantities for which a QuantitySpecOf<V> is true.

    "},{"location":"users_guide/framework_basics/concepts/#PointOrigin","title":"PointOrigin<T>","text":"

    PointOrigin concept matches all quantity point origins in the library. It is satisfied by either:

    • All types derived from an absolute_point_origin class template.
    • All types derived from a relative_point_origin class template.
    "},{"location":"users_guide/framework_basics/concepts/#PointOriginFor","title":"PointOriginFor<T, V>","text":"

    PointOriginFor concept is satisfied by all PointOrigin types that have quantity type implicitly convertible from quantity specification V, which means that V must satisfy QuantitySpecOf<T::quantity_spec>.

    Examples

    si::ice_point can serve as a point origin for points of isq::Celsius_temperature because this quantity type implicitly converts to isq::thermodynamic_temperature.

    However, if we define mean_sea_level in the following way:

    inline constexpr struct mean_sea_level : absolute_point_origin<isq::altitude> {} mean_sea_level;\n

    then it can't be used as a point origin for points of isq::length or isq::width as none of them is implicitly convertible to isq::altitude:

    • not every length is an altitude,
    • width is not compatible with altitude.
    "},{"location":"users_guide/framework_basics/concepts/#QuantityPoint","title":"QuantityPoint<T>","text":"

    QuantityPoint concept is satisfied by all types being either a specialization or derived from quantity_point class template.

    "},{"location":"users_guide/framework_basics/concepts/#QuantityPointOf","title":"QuantityPointOf<T, V>","text":"

    QuantityPointOf concept is satisfied by all the quantity points T that match the following value V:

    V Condition QuantitySpec The quantity point quantity specification satisfies QuantitySpecOf<V> concept. PointOrigin The point and V have the same absolute point origin."},{"location":"users_guide/framework_basics/concepts/#QuantityLike","title":"QuantityLike<T>","text":"

    QuantityLike concept provides interoperability with other libraries and is satisfied by a type T for which an instantiation of quantity_like_traits type trait yields a valid type that provides:

    • Static data member reference that matches the Reference concept,
    • rep type that matches RepresentationOf concept with the character provided in reference.
    • to_numerical_value(T) static member function returning a raw value of the quantity packed in either convert_explicitly or convert_implicitly wrapper that enables implicit conversion in the latter case.
    • from_numerical_value(rep) static member function returning T packed in either convert_explicitly or convert_implicitly wrapper that enables implicit conversion in the latter case.
    Examples

    This is how support for std::chrono::seconds can be provided:

    template<>\nstruct mp_units::quantity_like_traits<std::chrono::seconds> {\n  static constexpr auto reference = si::second;\n  using rep = std::chrono::seconds::rep;\n\n  [[nodiscard]] static constexpr convert_implicitly<rep> to_numerical_value(const std::chrono::seconds& q)\n  {\n    return q.count();\n  }\n\n  [[nodiscard]] static constexpr convert_implicitly<std::chrono::seconds> from_numerical_value(const rep& v)\n  {\n    return std::chrono::seconds(v);\n  }\n};\n\nquantity q = 42s;\nstd::chrono::seconds dur = 42 * s;\n
    "},{"location":"users_guide/framework_basics/concepts/#QuantityPointLike","title":"QuantityPointLike<T>","text":"

    QuantityPointLike concept provides interoperability with other libraries and is satisfied by a type T for which an instantiation of quantity_point_like_traits type trait yields a valid type that provides:

    • Static data member reference that matches the Reference concept.
    • Static data member point_origin that matches the PointOrigin concept.
    • rep type that matches RepresentationOf concept with the character provided in reference.
    • to_quantity(T) static member function returning the quantity being the offset of the point from the origin packed in either convert_explicitly or convert_implicitly wrapper that enables implicit conversion in the latter case.
    • from_quantity(quantity<reference, rep>) static member function returning T packed in either convert_explicitly or convert_implicitly wrapper that enables implicit conversion in the latter case.
    Examples

    This is how support for a std::chrono::time_point of std::chrono::seconds can be provided:

    template<typename C>\nstruct mp_units::quantity_point_like_traits<std::chrono::time_point<C, std::chrono::seconds>> {\n  using T = std::chrono::time_point<C, std::chrono::seconds>;\n  static constexpr auto reference = si::second;\n  static constexpr struct point_origin : absolute_point_origin<isq::time> {} point_origin{};\n  using rep = std::chrono::seconds::rep;\n\n  [[nodiscard]] static constexpr convert_implicitly<quantity<reference, rep>> to_quantity(const T& qp)\n  {\n    return quantity{qp.time_since_epoch()};\n  }\n\n  [[nodiscard]] static constexpr convert_implicitly<T> from_quantity(const quantity<reference, rep>& q)\n  {\n    return T(q);\n  }\n};\n\nquantity_point qp = time_point_cast<std::chrono::seconds>(std::chrono::system_clock::now());\nstd::chrono::sys_seconds q = qp + 42 * s;\n
    "},{"location":"users_guide/framework_basics/design_overview/","title":"Design Overview","text":"

    The most important entities in the mp-units library are:

    • quantity,
    • quantity point,
    • unit,
    • dimension,
    • quantity specification
    • quantity reference,
    • quantity representation.

    The graph provided below presents how those and a few other entities depend on each other:

    flowchart TD\n    Unit --- Reference\n    Dimension --- QuantitySpec[\"Quantity specification\"]\n    quantity_character[\"Quantity character\"] --- QuantitySpec\n    QuantitySpec --- Reference[\"Quantity reference\"]\n    Reference --- Quantity\n    quantity_character -.- Representation\n    Representation --- Quantity\n    Quantity --- QuantityPoint[\"Quantity point\"]\n    PointOrigin[\"Point origin\"] --- QuantityPoint\n\n    click Dimension \"#dimension\"\n    click quantity_character \"#quantity-character\"\n    click QuantitySpec \"#quantity-specification\"\n    click Unit \"#unit\"\n    click Reference \"#quantity-reference\"\n    click Representation \"#quantity-representation\"\n    click Quantity \"#quantity\"\n    click PointOrigin \"#point-origin\"\n    click QuantityPoint \"#quantity-point\"
    "},{"location":"users_guide/framework_basics/design_overview/#dimension","title":"Dimension","text":"

    Dimension specifies the dependence of a quantity on the base quantities of a particular system of quantities. It is represented as a product of powers of factors corresponding to the base quantities, omitting any numerical factor.

    In the mp-units library, we use the terms:

    • base dimension to refer to the dimension of a base quantity,
    • derived dimension to refer to the dimension of a derived quantity.

    For example:

    • length (\\(\\mathsf{L}\\)), mass (\\(\\mathsf{M}\\)), time (\\(\\mathsf{T}\\)), electric current (\\(\\mathsf{I}\\)), thermodynamic temperature (\\(\\mathsf{\u0398}\\)), amount of substance (\\(\\mathsf{N}\\)), and luminous intensity (\\(\\mathsf{J}\\)) are the base dimensions of the ISQ.
    • A derived dimension of force in the ISQ is denoted by \\(\\textsf{dim }F = \\mathsf{LMT}^{\u20132}\\).
    • The implementation of IEC 80000 in this library provides iec80000::dim_traffic_intensity base dimension to extend ISQ with strong information technology quantities.

    Base dimensions can be defined by the user in the following way:

    inline constexpr struct dim_length : base_dimension<\"L\"> {} dim_length;\ninline constexpr struct dim_time : base_dimension<\"T\"> {} dim_time;\n

    Derived dimensions are implicitly created by the library's framework based on the quantity equation provided in the quantity specification:

    C++23C++20Portable
    inline constexpr struct length : quantity_spec<dim_length> {} length;\ninline constexpr struct time : quantity_spec<dim_time> {} time;\ninline constexpr struct speed : quantity_spec<length / time> {} speed;\n\nstatic_assert(speed.dimension == dim_length / dim_time);\n
    inline constexpr struct length : quantity_spec<length, dim_length> {} length;\ninline constexpr struct time : quantity_spec<time, dim_time> {} time;\ninline constexpr struct speed : quantity_spec<speed, length / time> {} speed;\n\nstatic_assert(speed.dimension == dim_length / dim_time);\n
    QUANTITY_SPEC(length, dim_length);\nQUANTITY_SPEC(time, dim_time);\nQUANTITY_SPEC(speed, length / time);\n\nstatic_assert(speed.dimension == dim_length / dim_time);\n

    Important

    Users should not explicitly define any derived dimensions. Those should always be implicitly created by the framework.

    The multiplication/division on quantity specifications also multiplies/divides their dimensions:

    static_assert((length / time).dimension == dim_length / dim_time);\n

    The dimension equation of isq::dim_length / isq::dim_time results in the derived_dimension<isq::dim_length, per<isq::dim_time>> type.

    "},{"location":"users_guide/framework_basics/design_overview/#quantity-character","title":"Quantity character","text":"

    ISO 80000 explicitly states that quantities (even of the same kind) may have different characters:

    • scalar,
    • vector,
    • tensor.

    The quantity character in the mp-units library is implemented with the quantity_character enumeration:

    enum class quantity_character { scalar, vector, tensor };\n

    Info

    You can read more on quantity characters in the \"Character of a Quantity\" chapter.

    "},{"location":"users_guide/framework_basics/design_overview/#quantity-specification","title":"Quantity specification","text":"

    Dimension is not enough to describe a quantity. This is why the ISO 80000 provides hundreds of named quantity types. It turns out that there are many more quantity types in the ISQ than the named units in the SI.

    This is why the mp-units library introduces a quantity specification entity that stores:

    • dimension,
    • quantity type/name,
    • quantity character,
    • the quantity equation being the recipe to create this quantity (only for derived quantities that specify such a recipe).

    Note

    We know that it might be sometimes confusing to talk about quantities, quantity types/names, and quantity specifications. However, it might be important to notice here that even the ISO 80000 admits that:

    It is customary to use the same term, \"quantity\", to refer to both general quantities, such as length, mass, etc., and their instances, such as given lengths, given masses, etc. Accordingly, we are used to saying both that length is a quantity and that a given length is a quantity by maintaining the specification \u2013 \"general quantity, \\(Q\\)\" or \"individual quantity, \\(Q_\\textsf{a}\\)\" \u2013 implicit and exploiting the linguistic context to remove the ambiguity.

    In the mp-units library, we have a:

    • quantity - implemented as a quantity class template,
    • quantity specification - implemented with a quantity_spec class template that among others identifies a specific quantity type/name.

    For example:

    • isq::length, isq::mass, isq::time, isq::electric_current, isq::thermodynamic_temperature, isq::amount_of_substance, and isq::luminous_intensity are the specifications of base quantities in the ISQ.
    • isq::width, isq::height, isq::radius, and isq::position_vector are only a few of many quantities of a kind length specified in the ISQ.
    • isq::area, isq::speed, isq::moment_of_force are only a few of many derived quantities provided in the ISQ.

    Quantity specification can be defined by the user in one of the following ways:

    C++23C++20Portable
    inline constexpr struct length : quantity_spec<dim_length> {} length;\ninline constexpr struct height : quantity_spec<length> {} height;\ninline constexpr struct speed : quantity_spec<length / time> {} speed;\n
    inline constexpr struct length : quantity_spec<length, dim_length> {} length;\ninline constexpr struct height : quantity_spec<height, length> {} height;\ninline constexpr struct speed : quantity_spec<speed, length / time> {} speed;\n
    QUANTITY_SPEC(length, dim_length);\nQUANTITY_SPEC(height, length);\nQUANTITY_SPEC(speed, length / time);\n

    The quantity equation of isq::length / isq::time results in the derived_quantity_spec<isq::length, per<isq::time>> type.

    "},{"location":"users_guide/framework_basics/design_overview/#unit","title":"Unit","text":"

    A unit is a concrete amount of a quantity that allows us to measure the values of quantities of the same kind and represent the result as a number being the ratio of the two quantities.

    For example:

    • si::second, si::metre, si::kilogram, si::ampere, si::kelvin, si::mole, and si::candela are the base units of the SI.
    • si::kilo<si::metre> is a prefixed unit of length.
    • si::radian, si::newton, and si::watt are examples of named derived units within the SI.
    • non_si::minute is an example of a scaled unit of time.
    • si::si2019::speed_of_light_in_vacuum is a physical constant standardized by the SI in 2019.

    Note

    In the mp-units library, physical constants are also implemented as units.

    A unit can be defined by the user in one of the following ways:

    template<PrefixableUnit auto U> struct kilo_ : prefixed_unit<\"k\", mag_power<10, 3>, U> {};\ntemplate<PrefixableUnit auto U> inline constexpr kilo_<U> kilo;\n\ninline constexpr struct second : named_unit<\"s\", kind_of<isq::time>> {} second;\ninline constexpr struct minute : named_unit<\"min\", mag<60> * second> {} minute;\ninline constexpr struct gram : named_unit<\"g\", kind_of<isq::mass>> {} gram;\ninline constexpr struct kilogram : decltype(kilo<gram>) {} kilogram;\ninline constexpr struct newton : named_unit<\"N\", kilogram * metre / square(second)> {} newton;\n\ninline constexpr struct speed_of_light_in_vacuum : named_unit<\"c\", mag<299'792'458> * metre / second> {} speed_of_light_in_vacuum;\n

    The unit equation of si::metre / si::second results in the derived_unit<si::metre, per<si::second>> type.

    "},{"location":"users_guide/framework_basics/design_overview/#quantity-reference","title":"Quantity reference","text":"

    ISO defines a quantity as:

    Quote

    property of a phenomenon, body, or substance, where the property has a magnitude that can be expressed as a number and a reference

    After that, it says:

    Quote

    A reference can be a measurement unit, a measurement procedure, a reference material, or a combination of such.

    In the mp-units library, a quantity reference provides all the domain-specific metadata for the quantity besides its numerical value:

    • all the data stored in the quantity specification,
    • unit.

    Together with the value of a representation type, it forms a quantity.

    In the library, we have two different ways to provide a reference:

    • every unit with the associated quantity kind is a valid reference,
    • providing a unit to an indexing operator of a quantity specification explicitly instantiates a reference class template with this quantity spec and a unit passed as arguments.

    Note

    All the units of the SI have associated quantity kinds and may serve as a reference.

    For example:

    • si::metre is defined in the SI as a unit of isq::length and thus can be used as a reference to instantiate a quantity of length (e.g., 42 * m).
    • The expression isq::height[m] results with reference<isq::height, si::metre>, which can be used to instantiate a quantity of isq::height with a unit of si::metre (e.g., 42 * isq::height[m]).
    "},{"location":"users_guide/framework_basics/design_overview/#quantity-representation","title":"Quantity representation","text":"

    Quantity representation defines the type used to store the numerical value of a quantity. Such a type should be of a specific quantity character provided in the quantity specification.

    Note

    By default, all floating-point and integral (besides bool) types are treated as scalars.

    "},{"location":"users_guide/framework_basics/design_overview/#quantity","title":"Quantity","text":"

    ISO defines a quantity as:

    Quote

    property of a phenomenon, body, or substance, where the property has a magnitude that can be expressed as a number and a reference

    This is why a quantity class template is defined in the library as:

    template<Reference auto R,\n         RepresentationOf<get_quantity_spec(R).character> Rep = double>\nclass quantity;\n

    Its value can be easily created by multiplying/dividing the numerical value and a reference.

    For example:

    • All of 42 * m, 42 * si::metre, 42 * isq::height[m], and isq::height(42 * m) create a quantity.
    • A quantity type can also be specified explicitly (e.g., quantity<si::metre, int>, quantity<isq::height[m]>).
    "},{"location":"users_guide/framework_basics/design_overview/#point-origin","title":"Point origin","text":"

    In the affine space theory, the point origin specifies where the \"zero\" of our measurement's scale is.

    In the mp-units library, we have two types of point origins:

    • absolute - defines an absolute \"zero\" for our point,
    • relative - defines an origin that has some \"offset\" relative to an absolute point.

    For example:

    • the absolute point origin can be defined in the following way:
    inline constexpr struct absolute_zero : absolute_point_origin<isq::thermodynamic_temperature> {} absolute_zero;\n
    • the relative point origin can be defined in the following way:
    inline constexpr struct ice_point : relative_point_origin<absolute_zero + 273'150 * milli<kelvin>> {} ice_point;\n
    "},{"location":"users_guide/framework_basics/design_overview/#quantity-point","title":"Quantity point","text":"

    Quantity point implements a point in the affine space theory.

    In the mp-units library, the quantity point is implemented as:

    template<Reference auto R,\n         PointOriginFor<get_quantity_spec(R)> auto PO,\n         RepresentationOf<get_quantity_spec(R).character> Rep = double>\nclass quantity_point;\n

    Its value can be easily created by adding/subtracting the quantity with a point origin.

    For example:

    • The following specifies a quantity point defined in terms of an ice_point provided in the previous example:
    constexpr auto room_reference_temperature = ice_point + isq::Celsius_temperature(21 * deg_C);\n
    "},{"location":"users_guide/framework_basics/dimensionless_quantities/","title":"Dimensionless Quantities","text":"

    The quantities we discussed so far always had some specific type and physical dimension. However, this is not always the case. While performing various computations, we sometimes end up with so-called \"dimensionless\" quantities, which ISO defines as quantities of dimension one:

    ISO/IEC Guide 99

    • Quantity for which all the exponents of the factors corresponding to the base quantities in its quantity dimension are zero.
    • The measurement units and values of quantities of dimension one are numbers, but such quantities convey more information than a number.
    • Some quantities of dimension one are defined as the ratios of two quantities of the same kind.
    • Numbers of entities are quantities of dimension one.
    "},{"location":"users_guide/framework_basics/dimensionless_quantities/#dividing-two-quantities-of-the-same-kind","title":"Dividing two quantities of the same kind","text":"

    Dividing two quantities of the same kind always results in a quantity of dimension one. However, depending on what type of quantities we divide or what their units are, we may end up with slightly different results.

    Note

    In mp-units, dividing two quantities of the same dimension always results in a quantity with the dimension being dimension_one. This is often different for other physical units libraries, which may return a raw representation type for such cases. A raw value is also always returned from the division of two std::chrono::duration objects.

    To read more about the reasoning for this design decision, please check our FAQ.

    "},{"location":"users_guide/framework_basics/dimensionless_quantities/#dividing-quantities-of-the-same-type","title":"Dividing quantities of the same type","text":"

    First, let's analyze what happens if we divide two quantities of the same type:

    constexpr QuantityOf<dimensionless> auto q = isq::height(200 * m) / isq::height(50 * m);\n

    In such a case, we end up with a dimensionless quantity that has the following properties:

    static_assert(q.quantity_spec == dimensionless);\nstatic_assert(q.dimension == dimension_one);\nstatic_assert(q.unit == one);\n

    In case we would like to print its value, we would see a raw value of 4 in the output with no unit being printed.

    "},{"location":"users_guide/framework_basics/dimensionless_quantities/#dividing-quantities-of-different-types","title":"Dividing quantities of different types","text":"

    Now let's see what happens if we divide quantities of the same dimension and unit but which have different quantity types:

    constexpr QuantityOf<dimensionless> auto q = isq::work(200 * J) / isq::heat(50 * J);\n

    Again we end up with dimension_one and one, but this time:

    static_assert(q.quantity_spec == isq::work / isq::heat);\n

    As shown above, the result is not of a dimensionless type anymore. Instead, we get a quantity type derived from the performed quantity equation. According to the ISQ, work divided by heat is the recipe for the thermodynamic efficiency quantity, thus:

    static_assert(implicitly_convertible(q.quantity_spec, isq::efficiency_thermodynamics));\n

    Note

    The quantity of isq::efficiency_thermodynamics is of a kind dimensionless, so it is implicitly convertible to dimensionless and satisfies the QuantityOf<dimensionless> concept.

    "},{"location":"users_guide/framework_basics/dimensionless_quantities/#dividing-quantities-of-different-units","title":"Dividing quantities of different units","text":"

    Now, let's see what happens when we divide two quantities of the same type but different units:

    constexpr QuantityOf<dimensionless> auto q = isq::height(4 * km) / isq::height(2 * m);\n

    This time, we still get a quantity of the dimensionless type with a dimension_one as its dimension. However, the resulting unit is not one anymore:

    static_assert(q.unit == mag_power<10, 3> * one);\n

    In case we would print the text output of this quantity, we would not see a raw value of 2000, but 2 km/m.

    First, it may look surprising, but this is consistent with dividing quantities of different dimensions. For example, if we divide 4 * km / 2 * s, we do not expect km to be \"expanded\" to m before the division, right? We would expect the result of 2 km/s, which is exactly what we get when we divide quantities of the same kind.

    This is a compelling feature that allows us to express huge or tiny ratios without the need for big and expensive representation types. With this, we can easily define things like a Hubble's constant that uses a unit that is proportional to the ratio of kilometers per megaparsecs, which are both units of length:

    inline constexpr struct hubble_constant :\n    named_unit<{u8\"H\u2080\", \"H_0\"}, mag<ratio{701, 10}> * si::kilo<si::metre> / si::second / si::mega<parsec>> {} hubble_constant;\n
    "},{"location":"users_guide/framework_basics/dimensionless_quantities/#counts-of-things","title":"Counts of things","text":"

    Another important use case for dimensionless quantities is to provide strong types for counts of things. For example:

    • ISO-80000-3 provides a rotation quantity defined as the number of revolutions,
    • IEC-80000-6 provides a number of turns in a winding quantity,
    • IEC-80000-13 provides a Hamming distance quantity defined as the number of digit positions in which the corresponding digits of two words of the same length are different.

    Thanks to assigning strong names to such quantities, later on, they can be explicitly used as arguments in the quantity equations of other quantities deriving from them.

    "},{"location":"users_guide/framework_basics/dimensionless_quantities/#predefined-units-of-the-dimensionless-quantity","title":"Predefined units of the dimensionless quantity","text":"

    As we observed above, the most common unit for dimensionless quantities is one. It has the ratio of 1 and does not output any textual symbol.

    Important: one is an identity

    A unit one is special in the entire type system of units as it is considered to be an identity operand in the unit expression templates. This means that, for example:

    static_assert(one * one == one);\nstatic_assert(one * si::metre == si::metre);\nstatic_assert(si::metre / si::metre == one);\n

    The same is also true for dimension_one and dimensionless in the domains of dimensions and quantity specifications.

    Besides the unit one, there are a few other scaled units predefined in the library for usage with dimensionless quantities:

    inline constexpr struct percent : named_unit<\"%\", mag<ratio{1, 100}> * one> {} percent;\ninline constexpr struct per_mille : named_unit<{u8\"\u2030\", \"%o\"}, mag<ratio(1, 1000)> * one> {} per_mille;\ninline constexpr struct parts_per_million : named_unit<\"ppm\", mag<ratio(1, 1'000'000)> * one> {} parts_per_million;\ninline constexpr auto ppm = parts_per_million;\n
    "},{"location":"users_guide/framework_basics/dimensionless_quantities/#angular-quantities","title":"Angular quantities","text":"

    Special, often controversial, examples of dimensionless quantities are an angular measure and solid angular measure quantities that are defined in the ISQ to be the result of a division of \\(arc\\; length / radius\\) and \\(area / radius^2\\) respectively. Moreover, ISQ also explicitly states that both can be expressed in the unit one. This means that both angular measure and solid angular measure should be of a kind dimensionless.

    On the other hand, ISQ also specifies that a unit radian can be used for angular measure, and a unit steradian can be used for solid angular measure. Those should not be mixed or used to express other types of dimensionless quantities. This means that both angular measure and solid angular measure should also be quantity kinds by themselves.

    Note

    Many people claim that angle being a dimensionless quantity is a bad idea. There are proposals submitted to make an angle a base quantity and rad to become a base unit. More on this topic can be found in the \"Strong Angular System\" chapter.

    "},{"location":"users_guide/framework_basics/dimensionless_quantities/#nested-quantity-kinds","title":"Nested quantity kinds","text":"

    Angular quantities are not the only ones with such a \"strange\" behavior. Another but a similar case is a storage capacity quantity specified in IEC-80000-13 that again allows expressing it in both one and bit units.

    Those cases make dimensionless quantities an exceptional tree in the library. This is the only quantity hierarchy that contains more than one quantity kind in its tree:

    flowchart TD\n    dimensionless[\"dimensionless\\n[one]\"]\n    dimensionless --- rotation\n    dimensionless --- efficiency\n    dimensionless --- angular_measure[\"angular_measure\\n[rad]\"]\n    angular_measure --- rotational_displacement\n    angular_measure --- phase_angle\n    dimensionless --- solid_angular_measure[\"solid_angular_measure\\n[sr]\"]\n    dimensionless --- drag_factor\n    dimensionless --- storage_capacity[\"storage_capacity\\n[bit]\"] --- equivalent_binary_storage_capacity\n    dimensionless --- ...

    To provide such support in the library, we provided an is_kind specifier that can be appended to the quantity specification:

    C++23C++20Portable
    inline constexpr struct angular_measure : quantity_spec<dimensionless, arc_length / radius, is_kind> {} angular_measure;\ninline constexpr struct solid_angular_measure : quantity_spec<dimensionless, area / pow<2>(radius), is_kind> {} solid_angular_measure;\ninline constexpr struct storage_capacity : quantity_spec<dimensionless, is_kind> {} storage_capacity;\n
    inline constexpr struct angular_measure : quantity_spec<angular_measure, dimensionless, arc_length / radius, is_kind> {} angular_measure;\ninline constexpr struct solid_angular_measure : quantity_spec<solid_angular_measure, dimensionless, area / pow<2>(radius), is_kind> {} solid_angular_measure;\ninline constexpr struct storage_capacity : quantity_spec<storage_capacity, dimensionless, is_kind> {} storage_capacity;\n
    QUANTITY_SPEC(angular_measure, dimensionless, arc_length / radius, is_kind);\nQUANTITY_SPEC(solid_angular_measure, dimensionless, area / pow<2>(radius), is_kind);\nQUANTITY_SPEC(storage_capacity, dimensionless, is_kind);\n

    With the above, we can constrain radian, steradian, and bit to be allowed for usage with specific quantity kinds only:

    inline constexpr struct radian : named_unit<\"rad\", metre / metre, kind_of<isq::angular_measure>> {} radian;\ninline constexpr struct steradian : named_unit<\"sr\", square(metre) / square(metre), kind_of<isq::solid_angular_measure>> {} steradian;\ninline constexpr struct bit : named_unit<\"bit\", one, kind_of<storage_capacity>> {} bit;\n

    but still allow the usage of one and its scaled versions for such quantities.

    "},{"location":"users_guide/framework_basics/faster_than_lightspeed_constants/","title":"Faster-than-lightspeed Constants","text":"

    In most libraries, physical constants are implemented as constant (possibly constexpr) quantity values. Such an approach has some disadvantages, often affecting the run time performance and causing a loss of precision.

    "},{"location":"users_guide/framework_basics/faster_than_lightspeed_constants/#simplifying-constants-in-an-equation","title":"Simplifying constants in an equation","text":"

    When dealing with equations involving physical constants, they often occur more than once in an expression. Such a constant may appear both in a numerator and denominator of a quantity equation. As we know from fundamental physics, we can simplify such an expression by striking a constant out of the equation. Supporting such behavior allows a faster runtime performance and often a better precision of the resulting value.

    "},{"location":"users_guide/framework_basics/faster_than_lightspeed_constants/#physical-constants-as-units","title":"Physical constants as units","text":"

    The mp-units library allows and encourages the implementation of physical constants as regular units. With that, the constant's value is handled at compile-time, and under favorable circumstances, it can be simplified in the same way as all other repeated units do. If it is not simplified, the value is stored in a type, and the expensive multiplication or division operations can be delayed in time until a user selects a specific unit to represent/print the data.

    Such a feature often also allows using simpler or faster representation types in the equation. For example, instead of always having to multiply a small integral value with a big floating-point constant number, we can just use the integral type all the way. Only in case a constant will not simplify in the equation, and the user will require a specific unit, such a multiplication will be lazily invoked, and the representation type will need to be expanded to facilitate that. With that, addition, subtractions, multiplications, and divisions will always be the fastest - compiled away or done in out-of-order execution.

    To benefit from all of the above, in the mp-units library, SI defining and other constants are implemented as units in the following way:

    namespace si {\n\nnamespace si2019 {\n\ninline constexpr struct speed_of_light_in_vacuum :\n  named_unit<\"c\", mag<299'792'458> * metre / second> {} speed_of_light_in_vacuum;\n\n}  // namespace si2019\n\ninline constexpr struct magnetic_constant :\n  named_unit<{u8\"\u03bc\u2080\", \"u_0\"}, mag<4> * mag_pi * mag_power<10, -7> * henry / metre> {} magnetic_constant;\n\n}  // namespace mp_units::si\n
    "},{"location":"users_guide/framework_basics/faster_than_lightspeed_constants/#usage-examples","title":"Usage examples","text":"

    With the above definitions, we can calculate vacuum permittivity as:

    constexpr auto permeability_of_vacuum = 1. * si::magnetic_constant;\nconstexpr auto speed_of_light_in_vacuum = 1 * si::si2019::speed_of_light_in_vacuum;\n\nQuantityOf<isq::permittivity_of_vacuum> auto q = 1 / (permeability_of_vacuum * pow<2>(speed_of_light_in_vacuum));\n\nstd::println(\"permittivity of vacuum = {} = {:{%N:.3e} %U}\", q, q.in(F / m));\n

    The above first prints the following:

    permittivity of vacuum = 1  \u03bc\u2080\u207b\u00b9 c\u207b\u00b2 = 8.854e-12 F/m\n

    As we can clearly see, all the calculations above were just about multiplying and dividing the number 1 with the rest of the information provided as a compile-time type. Only when a user wants a specific SI unit as a result, the unit ratios are lazily resolved.

    Another similar example can be an equation for total energy:

    QuantityOf<isq::mechanical_energy> auto total_energy(QuantityOf<isq::momentum> auto p,\n                                                     QuantityOf<isq::mass> auto m,\n                                                     QuantityOf<isq::speed> auto c)\n{\n  return isq::mechanical_energy(sqrt(pow<2>(p * c) + pow<2>(m * pow<2>(c))));\n}\n
    constexpr auto GeV = si::giga<si::electronvolt>;\nconstexpr QuantityOf<isq::speed> auto c = 1. * si::si2019::speed_of_light_in_vacuum;\nconstexpr auto c2 = pow<2>(c);\n\nconst auto p1 = isq::momentum(4. * GeV / c);\nconst QuantityOf<isq::mass> auto m1 = 3. * GeV / c2;\nconst auto E = total_energy(p1, m1, c);\n\nstd::cout << \"in `GeV` and `c`:\\n\"\n          << \"p = \" << p1 << \"\\n\"\n          << \"m = \" << m1 << \"\\n\"\n          << \"E = \" << E << \"\\n\";\n\nconst auto p2 = p1.in(GeV / (m / s));\nconst auto m2 = m1.in(GeV / pow<2>(m / s));\nconst auto E2 = total_energy(p2, m2, c).in(GeV);\n\nstd::cout << \"\\nin `GeV`:\\n\"\n          << \"p = \" << p2 << \"\\n\"\n          << \"m = \" << m2 << \"\\n\"\n          << \"E = \" << E2 << \"\\n\";\n\nconst auto p3 = p1.in(kg * m / s);\nconst auto m3 = m1.in(kg);\nconst auto E3 = total_energy(p3, m3, c).in(J);\n\nstd::cout << \"\\nin SI base units:\\n\"\n          << \"p = \" << p3 << \"\\n\"\n          << \"m = \" << m3 << \"\\n\"\n          << \"E = \" << E3 << \"\\n\";\n

    The above prints the following:

    in `GeV` and `c`:\np = 4 GeV/c\nm = 3 GeV/c\u00b2\nE = 5 GeV\n\nin `GeV`:\np = 1.33426e-08 GeV s/m\nm = 3.33795e-17 GeV s\u00b2/m\u00b2\nE = 5 GeV\n\nin SI base units:\np = 2.13771e-18 kg m/s\nm = 5.34799e-27 kg\nE = 8.01088e-10 J\n
    "},{"location":"users_guide/framework_basics/generic_interfaces/","title":"Generic Interfaces","text":"

    Using a concrete unit in the interface often makes a lot of sense. It is especially useful if we store the data internally in the object. In such a case, we have to select a specific unit anyway.

    For example, let's consider a simple storage tank:

    class StorageTank {\n  quantity<horizontal_area[m2]> base_;\n  quantity<isq::height[m]> height_;\n  quantity<isq::mass_density[kg / m3]> density_ = air_density;\npublic:\n  constexpr StorageTank(const quantity<horizontal_area[m2]>& base, const quantity<isq::height[m]>& height) :\n      base_(base), height_(height)\n  {\n  }\n\n  // ...\n};\n

    As the quantities provided in the function's interface are then stored in the class, there is probably no sense in using generic interfaces here.

    "},{"location":"users_guide/framework_basics/generic_interfaces/#the-issues-with-unit-specific-interfaces","title":"The issues with unit-specific interfaces","text":"

    However, in many cases, using a specific unit in the interface is counterproductive. Let's consider the following function:

    quantity<km / h> avg_speed(quantity<km> distance, quantity<h> duration)\n{\n  return distance / duration;\n}\n

    Everything seems fine for now. It also works great if we call it with:

    quantity<km / h> s1 = avg_speed(220 * km, 2 * h);\n

    However, if the user starts doing the following:

    quantity<mi / h> s2 = avg_speed(140 * mi, 2 * h);\nquantity<m / s> s3 = avg_speed(20 * m, 2 * s);\n

    some issues start to be clearly visible:

    1. The arguments must be converted to units mandated by the function's parameters at each call. This involves potentially expensive multiplication/division operations at runtime.
    2. After the function returns the speed in a unit of km/h, another potentially expensive multiplication/division operations must be performed to convert the resulting quantity into a unit being the derived unit of the initial function's arguments.
    3. Besides the obvious runtime cost, some unit conversions may result in a value truncation, which means that the result will not be exactly equal to a direct division of the function's arguments.
    4. We have to use a floating-point representation type (the quantity class template by default uses double as a representation type) which is considered value-preserving. Trying to use an integral type in this scenario will work only for s1, while s2 and s3 will fail to compile. Failing to compile is a good thing here as the library tries to prevent the user from doing a clearly wrong thing. To make the code compile, the user needs to use dedicated value_cast or force_in like this:

      quantity<isq::speed[mi / h]> s2 = avg_speed(value_cast<km>(140 * mi), 2 * h);\nquantity<isq::speed[m / s]> s3 = avg_speed((20 * m).force_in(km), (2 * s).force_in(h));\n

      but the above will obviously provide an incorrect behavior (e.g., division by 0 in the evaluation of s3).

    "},{"location":"users_guide/framework_basics/generic_interfaces/#a-naive-solution","title":"A naive solution","text":"

    A naive solution here would be to implement the function as an unconstrained function template:

    auto avg_speed(auto distance, auto duration)\n{\n  return distance / duration;\n}\n

    Beware, this is not a good solution. The above code is too generic. Such a function template accepts everything:

    • quantities of other types
      • the compiler will not prevent accidental reordering of the function's arguments,
      • quantities of different types can be passed as well,
    • plain double arguments,
    • std::vector and std::lock_guard will be accepted as well (of course, this will fail in the instantiation of a function's body later in the compilation process).

    Note

    The usage of auto instead of a function parameter type is a C++20 feature. It makes such a code a function template where the type of such a parameter will be deduced during the template instantiation process from the argument type passed by the user.

    "},{"location":"users_guide/framework_basics/generic_interfaces/#constraining-function-template-arguments-with-concepts","title":"Constraining function template arguments with concepts","text":"

    Much better generic code can be implemented using basic concepts provided with the library:

    Original template notationThe shorthand notationTerse notation
    template<typename Distance, typename Duration>\n  requires QuantityOf<Distance, isq::length> && QuantityOf<Duration, isq::time>\nauto avg_speed(Distance distance, Duration duration)\n{\n  return isq::speed(distance / duration);\n}\n
    template<QuantityOf<isq::length> Distance, QuantityOf<isq::time> Duration>\nauto avg_speed(Distance distance, Duration duration)\n{\n  return isq::speed(distance / duration);\n}\n
    auto avg_speed(QuantityOf<isq::length> auto distance,\n               QuantityOf<isq::time> auto duration)\n{\n  return isq::speed(distance / duration);\n}\n

    This explicitly states that the arguments passed by the user must not only satisfy a Quantity concept, but also their quantity specification must be implicitly convertible to isq::length and isq::time accordingly. This no longer leaves room for error while still allowing the compiler to generate the most efficient code.

    Tip

    Please note that now it is safe just to use integral types all the way which again improves the runtime performance as the multiplication/division operations are often faster on the integral rather than floating-point types.

    "},{"location":"users_guide/framework_basics/generic_interfaces/#constraining-function-template-return-type","title":"Constraining function template return type","text":"

    The above function template resolves all of the issues described before. However, we can do even better here by additionally constraining the return type:

    QuantityOf<isq::speed> auto avg_speed(QuantityOf<isq::length> auto distance,\n                                      QuantityOf<isq::time> auto duration)\n{\n  return isq::speed(distance / duration);\n}\n

    Doing so has two important benefits:

    1. It informs the users of our interface about what to expect to be the result of a function invocation. It is superior to just returning auto, which does not provide any hint about the thing being returned there.
    2. Such a concept constrains the type returned from the function. This means that it works as a unit test to verify if our function actually performs what it is supposed to do. If there is an error in quantity equations, we will learn about it right away.
    "},{"location":"users_guide/framework_basics/generic_interfaces/#constraining-a-variable-on-the-stack","title":"Constraining a variable on the stack","text":"

    If we know precisely what the function does in its internals and if we know the exact argument types passed to such a function, we often know the exact type that will be returned from its invocation.

    However, if we care about performance, we should often use the generic interfaces described in this chapter. A side effect is that we sometimes are unsure about the return type. Even if we know it today, it might change a week from now due to some code refactoring.

    In such cases, we can again use auto to denote the type:

    auto s1 = avg_speed(220 * km, 2 * h);\nauto s2 = avg_speed(140 * mi, 2 * h);\nauto s3 = avg_speed(20 * m, 2 * s);\n

    or benefit from CTAD:

    quantity s1 = avg_speed(220 * km, 2 * h);\nquantity s2 = avg_speed(140 * mi, 2 * h);\nquantity s3 = avg_speed(20 * m, 2 * s);\n

    In both cases, it is probably OK to do so as the avg_speed function name explicitly provides the information on what to expect as a result.

    In other scenarios where the returned quantity type is not so obvious, it is again helpful to constrain the type with a concept like so:

    QuantityOf<isq::speed> auto s1 = avg_speed(220 * km, 2 * h);\nQuantityOf<isq::speed> auto s2 = avg_speed(140 * mi, 2 * h);\nQuantityOf<isq::speed> auto s3 = avg_speed(20 * m, 2 * s);\n

    The above explicitly provides additional information about the quantity we are dealing with in the code, and it serves as a unit test checking if the \"thing\" returned from a function is actually what we expected here.

    Note

    The QuantityOf and QuantityPointOf concepts are probably the most useful, but there are a few more to play with. A list of all the concepts can be found in the Basic Concepts chapter.

    "},{"location":"users_guide/framework_basics/interface_introduction/","title":"Interface Introduction","text":""},{"location":"users_guide/framework_basics/interface_introduction/#new-style-of-definitions","title":"New style of definitions","text":"

    The mp-units library decided to use a rather unusual pattern to define entities. Here is how we define metre and second SI base units:

    inline constexpr struct metre : named_unit<\"m\", kind_of<isq::length>> {} metre;\ninline constexpr struct second : named_unit<\"s\", kind_of<isq::time>> {} second;\n

    Please note that the above reuses the same identifier for a type and its value. The rationale behind this is that:

    • Users always work with values and never have to spell such a type name.
    • The types appear in the compilation errors and during debugging.

    Important

    To improve compiler errors' readability and make it easier to correlate them with a user's written code, a new idiom in the library is to use the same identifier for a type and its instance.

    "},{"location":"users_guide/framework_basics/interface_introduction/#strong-types-instead-of-aliases","title":"Strong types instead of aliases","text":"

    Let's look again at the above units definitions. Another important point to notice is that all the types describing entities in the library are short, nicely named identifiers that derive from longer, more verbose class template instantiations. This is really important to improve the user experience while debugging the program or analyzing the compilation error.

    Note

    Such a practice is rare in the industry. Some popular C++ physical units libraries generate enormously long error messages where even only the first line failed to fit on a slide with a tiny font.

    "},{"location":"users_guide/framework_basics/interface_introduction/#entities-composability","title":"Entities composability","text":"

    Many physical units libraries (in C++ or any other programming language) assign strong types to library entities (e.g., derived units). While metre_per_second as a type may not look too scary, consider, for example, units of angular momentum. If we followed this path, its coherent unit would look like kilogram_metre_sq_per_second. Now, consider how many scaled versions of this unit you would predefine in the library to ensure that all users are happy with your choice? How expensive would it be from the implementation point of view? What about potential future standardization efforts?

    This is why in mp-units, we put a strong requirement to make everything as composable as possible. For example, to create a quantity with a unit of speed, one may write:

    quantity<si::metre / si::second> q;\n

    In case we use such a unit often and would prefer to have a handy helper for it, we can always do something like this:

    constexpr auto metre_per_second = si::metre / si::second;\nquantity<metre_per_second> q;\n

    or choose any shorter identifier of our choice.

    Coming back to the angular momentum case, thanks to the composability of units, a user can create such a quantity in the following way:

    using namespace mp_units::si::unit_symbols;\nauto q = la_vector{1, 2, 3} * isq::angular_momentum[kg * m2 / s];\n

    It is a much better solution. It is terse and easy to understand. Please also notice how easy it is to obtain any scaled version of such a unit (e.g., mg * square(mm) / min) without having to introduce hundreds of types to predefine them.

    "},{"location":"users_guide/framework_basics/interface_introduction/#value-based-equations","title":"Value-based equations","text":"

    The mp-units library is based on C++20, significantly improving user experience. One of such improvements is the usage of value-based equations.

    As we have learned above, the entities are being used as values in the code, and they compose. Moreover, derived entities can be defined in the library using such value-based equations. This is a huge improvement compared to what we can find in other physical units libraries or what we have to deal with when we want to write some equations for std::ratio.

    For example, below are a few definitions of the SI derived units showing the power of C++20 extensions to Non-Type Template Parameters, which allow us to directly pass a result of the value-based unit equation to a class template definition:

    inline constexpr struct newton : named_unit<\"N\", kilogram * metre / square(second)> {} newton;\ninline constexpr struct pascal : named_unit<\"Pa\", newton / square(metre)> {} pascal;\ninline constexpr struct joule : named_unit<\"J\", newton * metre> {} joule;\n
    "},{"location":"users_guide/framework_basics/interface_introduction/#expression-templates","title":"Expression templates","text":"

    The previous chapter provided a rationale for not having predefined types for derived entities. In many libraries, such an approach results in long and unreadable compilation errors, as framework-generated types are typically far from being easy to read and understand.

    The mp-units library greatly improves the user experience by extensively using expression templates. Such expressions are used consistently throughout the entire library to describe the results of:

    • dimension equation - the result is put into the derived_dimension<> class template
    • quantity equation - the result is put into the derived_quantity_spec<> class template
    • unit equation - the result is put into the derived_unit<> class template

    For example, if we take the above-defined base units and put the results of their division into the quantity class template like this:

    quantity<metre / second> q;\n

    we will observe the following type in the debugger

    (gdb) ptype q\ntype = class mp_units::quantity<mp_units::derived_unit<metre, mp_units::per<second>>(), double> [with Rep = double] {\n

    The same type identifier will be visible in the compilation error (in case it happens).

    Important

    Expressions templates are extensively used throughout the library to improve the readability of the resulting types.

    "},{"location":"users_guide/framework_basics/interface_introduction/#identities","title":"Identities","text":"

    As mentioned above, equations can be performed on dimensions, quantities, and units. Each such domain must introduce an identity object that can be used in the resulting expressions. Here is the list of identities used in the library:

    Domain Concept Identity Dimension dimension_one QuantitySpec dimensionless Unit one

    In the equations, a user can explicitly refer to an identity object. For example:

    constexpr auto my_unit = one / second;\n

    Note

    Another way to achieve the same result is to call an inverse() function:

    constexpr auto my_unit = inverse(second);\n

    Both cases will result in the same expression template being generated and put into the wrapper class template.

    "},{"location":"users_guide/framework_basics/interface_introduction/#supported-operations-and-their-results","title":"Supported operations and their results","text":"

    There are only a few operations that one can do on such entities, and the result of each of them has its unique representation in the library:

    Operation Resulting template expression arguments A * B A, B B * A A, B A * A power<A, 2> {identity} * A A A * {identity} A A / B A, per<B> A / A {identity} A / {identity} A {identity} / A {identity}, per<A> pow<2>(A) power<A, 2> pow<2>({identity}) {identity} sqrt(A) or pow<1, 2>(A) power<A, 1, 2> sqrt({identity}) or pow<1, 2>({identity}) {identity}"},{"location":"users_guide/framework_basics/interface_introduction/#simplifying-the-resulting-expression-templates","title":"Simplifying the resulting expression templates","text":"

    To limit the length and improve the readability of generated types, there are many rules to simplify the resulting expression template.

    1. Ordering

      The resulting comma-separated arguments of multiplication are always sorted according to a specific predicate. This is why:

      static_assert(A * B == B * A);\nstatic_assert(std::is_same_v<decltype(A * B), decltype(B * A)>);\n

      This is probably the most important of all the steps, as it allows comparing types and enables the rest of the simplification rules.

    2. Aggregation

      In case two of the same identifiers are found next to each other on the argument list they will be aggregated in one entry:

      Before After A, A power<A, 2> A, power<A, 2> power<A, 3> power<A, 1, 2>, power<A, 2> power<A, 5, 2> power<A, 1, 2>, power<A, 1, 2> A
    3. Simplification

      In case two of the same identifiers are found in the numerator and denominator argument lists; they are being simplified into one entry:

      Before After A, per<A> {identity} power<A, 2>, per<A> A power<A, 3>, per<A> power<A, 2> A, per<power<A, 2>> {identity}, per<A>
    4. Repacking

      In case an expression uses two results of other operations, the components of its arguments are repacked into one resulting type and simplified there.

      For example, assuming:

      constexpr auto X = A / B;\n

      then:

      Operation Resulting template expression arguments X * B A X * A power<A, 2>, per<B> X * X power<A, 2>, per<power<B, 2>> X / X {identity} X / A {identity}, per<B> X / B A, per<power<B, 2>>
    "},{"location":"users_guide/framework_basics/interface_introduction/#example","title":"Example","text":"

    Thanks to all of the features described above, a user may write the code like this one:

    using namespace mp_units::si::unit_symbols;\nquantity speed = 60. * isq::speed[km / h];\nquantity duration = 8 * s;\nquantity acceleration = speed / duration;\nstd::cout << \"acceleration: \" << acceleration << \" (\" << acceleration.in(m / s2) << \")\\n\";\n

    The acceleration quantity, being the result of the above code, has the following type (after stripping the mp_units namespace for brevity):

    quantity<reference<derived_quantity_spec<isq::speed, per<isq::time>>{}, derived_unit<si::kilo_<si::metre{}>, per<non_si::hour, si::second>>{}>{}, int>\n

    and the text output presents:

    acceleration: 7.5 km h\u207b\u00b9 s\u207b\u00b9 (2.08333 m/s\u00b2)\n
    "},{"location":"users_guide/framework_basics/obtaining_metadata/","title":"Obtaining Metadata","text":""},{"location":"users_guide/framework_basics/obtaining_metadata/#quantity-spec","title":"quantity spec","text":""},{"location":"users_guide/framework_basics/obtaining_metadata/#unit","title":"unit","text":""},{"location":"users_guide/framework_basics/obtaining_metadata/#reference","title":"reference","text":""},{"location":"users_guide/framework_basics/obtaining_metadata/#quantity","title":"quantity","text":""},{"location":"users_guide/framework_basics/quantity_arithmetics/","title":"Quantity Arithmetics","text":""},{"location":"users_guide/framework_basics/quantity_arithmetics/#quantity-is-a-numeric-wrapper","title":"quantity is a numeric wrapper","text":"

    If we think about it, the quantity class template is just a \"smart\" numeric wrapper. It exposes properly constrained set of arithmetic operations on one or two operands.

    Important: quantity propagates the underlying interface

    Every single arithmetic operator is exposed by the quantity class template only if the underlying representation type provides it as well, and when its implementation has proper semantics (e.g., returns a reasonable type).

    For example, in the following code, -a will compile only if MyInt exposes such an operation as well:

    quantity a = MyInt{42} * m;\nquantity b = -a;\n

    Assuming that:

    • q is our quantity,
    • qi is a quantity implicitly convertible to q,
    • qk is a quantity of the same kind as q,
    • q1 is a quantity of dimension_one with the unit one,
    • qq is any other quantity,
    • number is a value of a type \"compatible\" with q's representation type,

    here is the list of all the supported operators:

    • unary:
      • +q
      • -q
      • ++q
      • q++
      • --q
      • q--
    • compound assignment:
      • q += qi
      • q -= qi
      • q %= qi
      • q *= number
      • q *= q1
      • q /= number
      • q /= q1
    • binary:
      • q + qk
      • q - qk
      • q % qk
      • q * qq
      • q * number
      • number * q
      • q / qq
      • q / number
      • number / q
    • ordering and comparison:
      • q == qk
      • q <=> qk

    As we can see, there are plenty of operations one can do on a value of a quantity type. As most of them are obvious, in the following chapters, we will discuss only the most important or non-trivial aspects of quantity arithmetics.

    "},{"location":"users_guide/framework_basics/quantity_arithmetics/#addition-and-subtraction","title":"Addition and subtraction","text":"

    Quantities can easily be added or subtracted from each other:

    static_assert(1 * m + 1 * m == 2 * m);\nstatic_assert(2 * m - 1 * m == 1 * m);\nstatic_assert(isq::height(1 * m) + isq::height(1 * m) == isq::height(2 * m));\nstatic_assert(isq::height(2 * m) - isq::height(1 * m) == isq::height(1 * m));\n

    The above uses the same types for LHS, RHS, and the result, but in general, we can add, subtract, or compare the values of any quantity type as long as both quantities are of the same kind. The result of such an operation will be the common type of the arguments:

    static_assert(1 * km + 1.5 * m == 1001.5 * m);\nstatic_assert(isq::height(1 * m) + isq::width(1 * m) == isq::length(2 * m));\nstatic_assert(isq::height(2 * m) - isq::distance(0.5 * m) == 1.5 * m);\nstatic_assert(isq::radius(1 * m) - 0.5 * m == isq::radius(0.5 * m));\n

    Note

    Please note that for the compound assignment operators, both arguments have to either be of the same type or the RHS has to be implicitly convertible to the LHS, as the type of LHS is always the result of such an operation:

    static_assert((1 * m += 1 * km) == 1001 * m);\nstatic_assert((isq::height(1.5 * m) -= 1 * m) == isq::height(0.5 * m));\n

    If we break those rules, the following code will not compile:

    static_assert((1 * m -= 0.5 * m) == 0.5 * m);                       // Compile-time error(1)\nstatic_assert((1 * km += 1 * m) == 1001 * m);                       // Compile-time error(2)\nstatic_assert((isq::height(1 * m) += isq::length(1 * m)) == 2 * m); // Compile-time error(3)\n
    1. The floating-point to integral representation type is considered narrowing.
    2. Conversion of quantity with integral representation type from a unit of a higher resolution to the one with a lower resolution is considered narrowing.
    3. Conversion from a more generic quantity type to a more specific one is considered unsafe.
    "},{"location":"users_guide/framework_basics/quantity_arithmetics/#multiplication-and-division","title":"Multiplication and division","text":"

    Multiplying or dividing a quantity by a number does not change its quantity type or unit. However, its representation type may change. For example:

    static_assert(isq::height(3 * m) * 0.5 == isq::height(1.5 * m));\n

    Note

    Unless we use a compound assignment operator, in which case truncating operations are again not allowed:

    static_assert((isq::height(3 * m) *= 0.5) == isq::height(1.5 * m)); // Compile-time error(1)\n
    1. The floating-point to integral representation type is considered narrowing.

    However, suppose we multiply or divide quantities of the same or different types or we divide a raw number by a quantity. In that case, we most probably will end up in a quantity of yet another type:

    static_assert(120 * km / (2 * h) == 60 * km / h);\nstatic_assert(isq::width(2 * m) * isq::length(2 * m) == isq::area(4 * m2));\nstatic_assert(50 / isq::time(1 * s) == isq::frequency(50 * Hz));\n

    Note

    An exception from the above rule happens when one of the arguments is a dimensionless quantity. If we multiply or divide by such a quantity, the quantity type will not change. If such a quantity has a unit one, also the unit of a quantity will not change:

    static_assert(120 * m / (2 * one) == 60 * m);\n

    An interesting special case happens when we divide the same quantity kinds or multiply a quantity by its inverted type. In such a case, we end up with a dimensionless quantity.

    static_assert(isq::height(4 * m) / isq::width(2 * m) == 2 * one); // (1)!\nstatic_assert(5 * h / (120 * min) == 0 * one);  // (2)!\nstatic_assert(5. * h / (120 * min) == 2.5 * one);\n
    1. The resulting quantity type of the LHS is isq::height / isq::width, which is a quantity of the dimensionless kind.
    2. The resulting quantity of the LHS is 0 * dimensionless[h / min]. To be consistent with the division of different quantity types, we do not convert quantity values to a common unit before the division.

    Important: Beware of integral division

    The physical units library can't do any runtime branching logic for the division operator. All logic must be done at compile-time when the actual values are unknown, and the quantity types can't change at runtime.

    If we expect 120 * km / (2 * h) to return 60 km / h, we have to agree with the fact that 5 * km / (24 * h) returns 0 km/h. We can't do a range check at runtime to dynamically adjust scales and types based on the values of provided function arguments.

    This is why we often prefer floating-point representation types when dealing with units. Some popular physical units libraries even forbid integer division at all.

    "},{"location":"users_guide/framework_basics/quantity_arithmetics/#modulo","title":"Modulo","text":"

    Now that we know how addition, subtraction, multiplication, and division work, it is time to discuss modulo. What would we expect to be returned from the following quantity equation?

    auto q = 5 * h % (120 * min);\n

    Most of us would probably expect to see 1 h or 60 min as a result. And this is where the problems start.

    C++ language defines its / and % operators with the quotient-remainder theorem:

    q = a / b;\nr = a % b;\nq * b + r == a;\n

    The important property of the modulo operation is that it only works for integral representation types (it is undefined what modulo for floating-point types means). However, as we saw in the previous chapter, integral types are tricky because they often truncate the value.

    From the quotient-remainder theorem, the result of modulo operation is r = a - q * b. Let's see what we get from such a quantity equation on integral representation types:

    const quantity a = 5 * h;\nconst quantity b = 120 * min;\nconst quantity q = a / b;\nconst quantity r = a - q * b;\n\nstd::cout << \"reminder: \" << r << \"\\n\";\n

    The above code outputs:

    reminder: 5 h\n

    And now, a tough question needs an answer. Do we really want modulo operation on physical units to be consistent with the quotient-remainder theorem and return 5 h for 5 * h % (120 * min)?

    This is exactly why we decided not to follow this hugely surprising path in the mp-units library. The selected approach was also consistent with the feedback from the C++ experts. For example, this is what Richard Smith said about this issue:

    Richard Smith

    I think the quotient-remainder property is a less important motivation here than other factors -- the constraints on % and / are quite different, so they lack the inherent connection they have for integers. In particular, I would expect that A / B works for all quantities A and B, whereas A % B is only meaningful when A and B have the same dimension. It seems like a nice-to-have for the property to apply in the case where both / and % are defined, but internal consistency of / across all cases seems much more important to me.

    I would expect 61 min % 1 h to be 1 min, and 1 h % 59 min to also be 1 min, so my intuition tells me that the result type of A % B, where A and B have the same dimension, should have the smaller unit of A and B (and if the smaller one doesn't divide the larger one, we should either use the gcd / std::common_type of the units of A and B or perhaps just produce an error). I think any other behavior for % is hard to defend.

    On the other hand, for division it seems to me that the choice of unit should probably not affect the result, and so if we want that 5 mm / 120 min = 0 mm/min, then 5 h / 120 min == 0 hc (where hc is a dimensionless \"hexaconta\", or 60x, unit). I don't like the idea of taking SI base units into account; that seems arbitrary and like it would do the wrong thing as often as it does the right thing, especially when the units have a multiplier that is very large or small. We could special-case the situation of a dimensionless quantity, but that could lead to problematic overflow pretty easily: a calculation such as 10 s * 5 GHz * 2 uW would overflow an int if it produces a dimensionless quantity for 10 s * 5 GHz, but it could equally produce 50 G * 2 uW = 100 kW without any overflow, and presumably would if the terms were merely reordered.

    If people want to use integer-valued quantities, I think it's fundamental that you need to know what the units of the result of an operation will be, and take that into account in how you express computations; the simplest rule for heterogeneous operators like * or / seems to be that the units of the result are determined by applying the operator to the units of the operands -- and for homogeneous operators like + or %, it seems like the only reasonable option is that you get the std::common_type of the units of the operands.

    To summarize, the modulo operation on physical units has more in common with addition and division operators than with the quotient-remainder theorem. To avoid surprising results, the operation uses a common unit to do the calculation and provide its result:

    static_assert(5 * h / (120 * min) == 0 * one);\nstatic_assert(5 * h % (120 * min) == 60 * min);\nstatic_assert(61 * min % (1 * h) == 1 * min);\nstatic_assert(1 * h % (59 * min) == 1 * min);\n
    "},{"location":"users_guide/framework_basics/quantity_arithmetics/#comparison-against-zero","title":"Comparison against zero","text":"

    In our code, we often want to compare the value of a quantity against zero. For example, we do it every time we want to ensure that we deal with a non-zero or positive value.

    We could implement such checks in the following way:

    if (q1 / q2 != 0 * m / s)\n  // ...\n

    The above would work (assuming we are dealing with the quantity of speed) but could be suboptimal if the result of q1 / q2 is not expressed in m / s. To eliminate the need for conversion, we need to write:

    if (auto q = q1 / q2; q != q.zero())\n  // ...\n

    but that is a bit inconvenient, and inexperienced users could be unaware of this technique and its reasons.

    For the above reasons, the library provides dedicated interfaces to compare against zero that follow the naming convention of named comparison functions in the C++ Standard Library. The mp-units/compare.h header file exposes the following functions:

    • is_eq_zero
    • is_neq_zero
    • is_lt_zero
    • is_gt_zero
    • is_lteq_zero
    • is_gteq_zero

    Thanks to them, to save typing and not pay for unneeded conversions, our check could be implemented as follows:

    if (is_neq_zero(q1 / q2))\n  // ...\n

    Tip

    Those functions will work with any type T that exposes zero() member function returning something comparable to T. Thanks to that, we can use them not only with quantities but also with std::chrono::duration or any other type that exposes such an interface.

    "},{"location":"users_guide/framework_basics/quantity_arithmetics/#other-maths","title":"Other maths","text":"

    This chapter scopes only on the quantity type's operators. However, there are many named math functions taking quantities as arguments. Those can be found in the mp-units/math.h header file. Among others, we can find there the following:

    • pow(), sqrt(), cbrt(),
    • exp(),
    • abs(),
    • epsilon(),
    • fma(), fmod(),
    • isfinite(), isinf(), isnan(),
    • floor(), ceil(), round(),
    • inverse(),
    • hypot(),
    • sin(), cos(), tan(),
    • asin(), acos(), atan(), atan2().

    In the library, we can also find mp-units/random.h header file with all the pseudo-random number generators working on quantity types.

    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/","title":"Simple and Typed Quantities","text":"

    ISO defines a quantity as:

    Quote

    property of a phenomenon, body, or substance, where the property has a magnitude that can be expressed as a number and a reference

    After that, it says:

    Quote

    A reference can be a measurement unit, a measurement procedure, a reference material, or a combination of such.

    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/#quantity-class-template","title":"quantity class template","text":"

    In the mp-units library, a quantity is represented with the following class template:

    template<Reference auto R,\n         RepresentationOf<get_quantity_spec(R).character> Rep = double>\nclass quantity;\n

    The concept Reference is satisfied by a type that provides all the domain-specific metadata describing a quantity (besides the representation type and its value). Such a type can be either:

    • a unit with an associated quantity type (e.g., si::metre, m / s),
    • a reference type explicitly specifying the quantity type and its unit.

    Important

    All units in the SI system have an associated quantity type.

    A reference type is implicitly created as a result of the following expression:

    constexpr auto ref = isq::length[m];\n

    The above example results in the following type reference<isq::length(), si::metre()> being instantiated.

    As we have two alternative options that satisfy the Reference concept in the mp-units library, we also have two modes of dealing with quantities.

    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/#simple-quantities","title":"Simple quantities","text":"

    The simple mode might be preferred by many developers. It is all about units. Quantities using this mode have shorter type identifiers, resulting in easier-to-understand error messages and better debugging experience.

    Here is a simple example showing how to deal with such quantities:

    C++ modulesHeader files
    #include <print>\nimport mp_units;\n\nusing namespace mp_units;\n\nconstexpr quantity<si::metre / si::second> avg_speed(quantity<si::metre> dist,\n                                                     quantity<si::second> time)\n{\n  return dist / time;\n}\n\nint main()\n{\n  using namespace mp_units::si::unit_symbols;\n\n  const quantity distance = 110 * km;\n  const quantity duration = 2 * h;\n  const quantity speed = avg_speed(distance, duration);\n\n  std::println(\"A car driving {} in {} has an average speed of {:{%N:.4} %U} ({:{%N:.4} %U})\",\n               distance, duration, speed, speed.in(km / h));\n}\n
    #include <mp-units/format.h>\n#include <mp-units/systems/si/si.h>\n#include <print>\n\nusing namespace mp_units;\n\nconstexpr quantity<si::metre / si::second> avg_speed(quantity<si::metre> dist,\n                                                     quantity<si::second> time)\n{\n  return dist / time;\n}\n\nint main()\n{\n  using namespace mp_units::si::unit_symbols;\n\n  const quantity distance = 110 * km;\n  const quantity duration = 2 * h;\n  const quantity speed = avg_speed(distance, duration);\n\n  std::println(\"A car driving {} in {} has an average speed of {:{%N:.4} %U} ({:{%N:.4} %U})\",\n               distance, duration, speed, speed.in(km / h));\n}\n

    The code above prints:

    A car driving 110 km in 2 h has an average speed of 15.28 m/s (55 km/h)\n

    Try it on Compiler Explorer

    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/#user-provided-unit-wrappers","title":"User-provided unit wrappers","text":"

    Sometimes it might be awkward to type some derived units:

    quantity speed = 60 * km / h;\n

    In case such a unit is used a lot in the project, a user can easily provide a nicely named wrapper for it with:

    constexpr auto kmph = km / h;\nquantity speed = 60 * kmph;\n
    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/#easy-to-understand-compilation-error-messages","title":"Easy-to-understand compilation error messages","text":"

    In case a user makes an error in a quantity equation and the result of the calculation will not match the function return type, the compiler will detect such an issue at compile-time.

    For example, in case we will make the following error:

    constexpr quantity<si::metre / si::second> avg_speed(quantity<si::metre> dist,\n                                                     quantity<si::second> time)\n{\n  return dist * time;  // (1)!\n}\n
    1. Quantities multiplied (instead of divided) by accident.

    the following compilation error message will be provided:

    error: no viable conversion from returned value of type\n       'quantity<mp_units::derived_unit<mp_units::si::metre, mp_units::si::second>{{{}}}, [...]>'\n       to function return type\n       'quantity<mp_units::derived_unit<mp_units::si::metre, mp_units::per<mp_units::si::second>>{{{}}}, [...]>'\n   10 |   return dist * time;\n      |          ^~~~~~~~~~~\n
    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/#typed-quantities","title":"Typed quantities","text":"

    Simple mode is all about and just about units. In case we care about a specific quantity type, typed quantities should be preferred. With this mode, for example, we can specify if we deal with width, height, or radius and ensure we will not assign one to another by accident.

    The previous example can be re-typed using typed quantities in the following way:

    C++ modulesHeader files
    #include <print>\nimport mp_units;\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\nconstexpr quantity<isq::speed[m / s]> avg_speed(quantity<isq::length[m]> dist,\n                                                quantity<isq::time[s]> time)\n{\n  return dist / time;\n}\n\nint main()\n{\n  const quantity distance = isq::distance(110 * km);\n  const quantity duration = isq::time(2 * h);\n  const quantity speed = avg_speed(distance, duration);\n\n  std::println(\"A car driving {} in {} has an average speed of {:{%N:.4} %U} ({:{%N:.4} %U})\",\n               distance, duration, speed, speed.in(km / h));\n}\n
    #include <mp-units/format.h>\n#include <mp-units/systems/isq/isq.h>\n#include <mp-units/systems/si/si.h>\n#include <print>\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\nconstexpr quantity<isq::speed[m / s]> avg_speed(quantity<isq::length[m]> dist,\n                                                quantity<isq::time[s]> time)\n{\n  return dist / time;\n}\n\nint main()\n{\n  const quantity distance = isq::distance(110 * km);\n  const quantity duration = isq::time(2 * h);\n  const quantity speed = avg_speed(distance, duration);\n\n  std::println(\"A car driving {} in {} has an average speed of {:{%N:.4} %U} ({:{%N:.4} %U})\",\n               distance, duration, speed, speed.in(km / h));\n}\n
    A car driving 110 km in 2 h has an average speed of 15.28 m/s (55 km/h)\n

    Try it on Compiler Explorer

    In case we will accidentally make the same calculation error as before, this time, we will get a bit longer error message, this time also containing information about the quantity type:

    error: no viable conversion from returned value of type\n       'quantity<reference<get_quantity_spec(metre{}) * struct time{{{}}}, metre{} * second{{}}>{}, [...]>'\n       to function return type\n       'quantity<reference<speed{}, derived_unit<metre, per<second>>{}>{}, [...]>'\n   12 |   return dist * time;\n      |          ^~~~~~~~~~~\n

    As we can see above, the compilation error is longer but still relatively easy to understand.

    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/#additional-type-safety-with-typed-quantities","title":"Additional type safety with typed quantities","text":"

    Based on the previous example, it might seem that typed quantities are not that useful, more to type and provide harder-to-understand error messages. It might be true in some cases, but there are scenarios where they offer additional level of safety.

    Let's see another example:

    C++ modulesHeader files SimpleTyped
    #include <numbers>\nimport mp_units;\n\nusing namespace mp_units;\n\nclass StorageTank {\n  quantity<square(si::metre)> base_;\n  quantity<si::metre> height_;\npublic:\n  constexpr StorageTank(const quantity<square(si::metre)>& base,\n                        const quantity<si::metre>& height) :\n    base_(base), height_(height)\n  {\n  }\n\n  // ...\n};\n\nclass CylindricalStorageTank : public StorageTank {\npublic:\n  constexpr CylindricalStorageTank(const quantity<si::metre>& radius,\n                                   const quantity<si::metre>& height) :\n    StorageTank(std::numbers::pi * pow<2>(radius), height)\n  {\n  }\n};\n\nclass RectangularStorageTank : public StorageTank {\npublic:\n  constexpr RectangularStorageTank(const quantity<si::metre>& length,\n                                   const quantity<si::metre>& width,\n                                   const quantity<si::metre>& height) :\n    StorageTank(length * width, height)\n  {\n  }\n};\n\nint main()\n{\n  using namespace mp_units::si::unit_symbols;\n  auto tank = RectangularStorageTank(1'000 * mm, 500 * mm, 200 * mm);\n  // ...\n}\n
    #include <numbers>\nimport mp_units;\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\n// add a custom quantity type of kind isq::length\ninline constexpr struct horizontal_length\n    : quantity_spec<isq::length> {} horizontal_length;\n\n// add a custom derived quantity type of kind isq::area\n// with a constrained quantity equation\ninline constexpr struct horizontal_area\n    : quantity_spec<isq::area, horizontal_length * isq::width> {} horizontal_area;\n\nclass StorageTank {\n  quantity<horizontal_area[m2]> base_;\n  quantity<isq::height[m]> height_;\npublic:\n  constexpr StorageTank(const quantity<horizontal_area[m2]>& base,\n                        const quantity<isq::height[m]>& height) :\n    base_(base), height_(height)\n  {\n  }\n\n  // ...\n};\n\nclass CylindricalStorageTank : public StorageTank {\npublic:\n  constexpr CylindricalStorageTank(const quantity<isq::radius[m]>& radius,\n                                   const quantity<isq::height[m]>& height) :\n    StorageTank(quantity_cast<horizontal_area>(std::numbers::pi * pow<2>(radius)),\n                height)\n  {\n  }\n};\n\nclass RectangularStorageTank : public StorageTank {\npublic:\n  constexpr RectangularStorageTank(const quantity<horizontal_length[m]>& length,\n                                   const quantity<isq::width[m]>& width,\n                                   const quantity<isq::height[m]>& height) :\n    StorageTank(length * width, height)\n  {\n  }\n};\n\nint main()\n{\n  auto tank = RectangularStorageTank(horizontal_length(1'000 * mm),\n                                     isq::width(500 * mm),\n                                     isq::height(200 * mm));\n  // ...\n}\n
    SimpleTyped
    #include <mp-units/math.h>\n#include <mp-units/systems/si/si.h>\n#include <numbers>\n\nusing namespace mp_units;\n\nclass StorageTank {\n  quantity<square(si::metre)> base_;\n  quantity<si::metre> height_;\npublic:\n  constexpr StorageTank(const quantity<square(si::metre)>& base,\n                        const quantity<si::metre>& height) :\n    base_(base), height_(height)\n  {\n  }\n\n  // ...\n};\n\nclass CylindricalStorageTank : public StorageTank {\npublic:\n  constexpr CylindricalStorageTank(const quantity<si::metre>& radius,\n                                   const quantity<si::metre>& height) :\n    StorageTank(std::numbers::pi * pow<2>(radius), height)\n  {\n  }\n};\n\nclass RectangularStorageTank : public StorageTank {\npublic:\n  constexpr RectangularStorageTank(const quantity<si::metre>& length,\n                                   const quantity<si::metre>& width,\n                                   const quantity<si::metre>& height) :\n    StorageTank(length * width, height)\n  {\n  }\n};\n\nint main()\n{\n  using namespace mp_units::si::unit_symbols;\n  auto tank = RectangularStorageTank(1'000 * mm, 500 * mm, 200 * mm);\n  // ...\n}\n
    #include <mp-units/math.h>\n#include <mp-units/systems/isq/isq.h>\n#include <mp-units/systems/si/si.h>\n#include <numbers>\n\nusing namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\n\n// add a custom quantity type of kind isq::length\ninline constexpr struct horizontal_length\n    : quantity_spec<isq::length> {} horizontal_length;\n\n// add a custom derived quantity type of kind isq::area\n// with a constrained quantity equation\ninline constexpr struct horizontal_area\n    : quantity_spec<isq::area, horizontal_length * isq::width> {} horizontal_area;\n\nclass StorageTank {\n  quantity<horizontal_area[m2]> base_;\n  quantity<isq::height[m]> height_;\npublic:\n  constexpr StorageTank(const quantity<horizontal_area[m2]>& base,\n                        const quantity<isq::height[m]>& height) :\n    base_(base), height_(height)\n  {\n  }\n\n  // ...\n};\n\nclass CylindricalStorageTank : public StorageTank {\npublic:\n  constexpr CylindricalStorageTank(const quantity<isq::radius[m]>& radius,\n                                   const quantity<isq::height[m]>& height) :\n    StorageTank(quantity_cast<horizontal_area>(std::numbers::pi * pow<2>(radius)),\n                height)\n  {\n  }\n};\n\nclass RectangularStorageTank : public StorageTank {\npublic:\n  constexpr RectangularStorageTank(const quantity<horizontal_length[m]>& length,\n                                   const quantity<isq::width[m]>& width,\n                                   const quantity<isq::height[m]>& height) :\n    StorageTank(length * width, height)\n  {\n  }\n};\n\nint main()\n{\n  auto tank = RectangularStorageTank(horizontal_length(1'000 * mm),\n                                     isq::width(500 * mm),\n                                     isq::height(200 * mm));\n  // ...\n}\n

    In the above example, the highlighted call doesn't look that safe anymore in the case of simple quantities, right? Suppose someone, either by mistake or due to some refactoring, will call the function with an invalid order of arguments. In that case, the program will compile fine but not work as expected.

    Let's see what will happen if we reorder the arguments in the case of typed quantities:

    auto tank = RectangularStorageTank(horizontal_length(1'000 * mm),\n                                   isq::height(200 * mm),\n                                   isq::width(500 * mm));\n

    This time, a compiler provides the following compilation error:

    <source>:53:15: error: no matching constructor for initialization of 'RectangularStorageTank'\n   53 |   auto tank = RectangularStorageTank(horizontal_length(1'000 * mm),\n      |               ^                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n   54 |                                      isq::height(200 * mm),\n      |                                      ~~~~~~~~~~~~~~~~~~~~~~\n   55 |                                      isq::width(500 * mm));\n      |                                      ~~~~~~~~~~~~~~~~~~~~\n<source>:43:13: note: candidate constructor not viable: no known conversion from\n                'quantity<mp_units::reference<mp_units::isq::height{{{{{}}}}},\n                                              mp_units::si::milli_<mp_units::si::metre{{}}>{{{{}}}}>{}, int>' to\n                'const quantity<reference<width{}, metre{}>{}, (default) double>' for 2nd argument\n   43 |   constexpr RectangularStorageTank(const quantity<horizontal_length[m]>& length,\n      |             ^\n   44 |                                    const quantity<isq::width[m]>& width,\n      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n

    What about derived quantities? In the above example, you probably noticed that we also defined a custom horizontal_area quantity of kind isq::area. This quantity has the unique property of being implicitly constructible only from the result of the multiplication of quantities of horizontal_area and isq::width or the ones that implicitly convert to them.

    Based on the above error message, we already know that a quantity of isq::height is not implicitly constructible to the quantity of isq::width. This property is transitively passed to derived quantities using them. If by accident, we will try to create a StorageTank base class in the following way:

    class RectangularStorageTank : public StorageTank {\npublic:\n  constexpr RectangularStorageTank(const quantity<horizontal_length[m]>& length,\n                                   const quantity<isq::width[m]>& width,\n                                   const quantity<isq::height[m]>& height) :\n    StorageTank(length * height, height)\n  {\n  }\n};\n

    we will again get a compilation error message like this one:

    error: no matching constructor for initialization of 'StorageTank'\n   46 |     StorageTank(length * height, height)\n      |     ^           ~~~~~~~~~~~~~~~~~~~~~~~\n<source>:22:13: note: candidate constructor not viable: no known conversion from\n                'quantity<mp_units::reference<mp_units::derived_quantity_spec<horizontal_length, mp_units::isq::height>{{}, {{}}},\n                                              mp_units::derived_unit<mp_units::power<mp_units::si::metre, 2>>{{{}}}>{}, [...]>' to\n                'const quantity<reference<horizontal_area{}, derived_unit<power<metre, 2>>{}>{}, [...]>' for 1st argument\n   22 |   constexpr StorageTank(const quantity<horizontal_area[m2]>& base,\n      |             ^           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n

    Tip

    If you need to use various quantities of the same kind, consider using typed quantities to bring an additional level of safety to your project.

    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/#quantity_cast-to-force-unsafe-conversions","title":"quantity_cast() to force unsafe conversions","text":"

    Did you notice the quantity_cast() usage in the other child class?

    class CylindricalStorageTank : public StorageTank {\npublic:\n  constexpr CylindricalStorageTank(const quantity<isq::radius[m]>& radius,\n                                   const quantity<isq::height[m]>& height) :\n    StorageTank(quantity_cast<horizontal_area>(std::numbers::pi * pow<2>(radius)),\n                height)\n  {\n  }\n};\n

    As isq::radius is not convertible to horizontal_length, the derived quantity of pow<2>(radius) can't be converted to horizontal_area as well. It would be unsafe to allow such a conversion as not all of the circles lie flat on the ground, right?

    In such a case, the user has to explicitly force such an unsafe conversion with the help of a quantity_cast(). This function name is easy to spot in code reviews or while searching the project for problems if something goes sideways. In case of unexpected quantities-related issues, this should be the first function to look for.

    Tip

    Do not overuse quantity_cast(). Use it only when necessary and ensure that the requested conversion is exactly what you need in this case.

    "},{"location":"users_guide/framework_basics/simple_and_typed_quantities/#which-mode-should-i-use-in-my-project","title":"Which mode should I use in my project?","text":"

    We have good news for you if you wonder which mode you should choose for your project. Simple and typed quantity modes can be freely mixed with each other. When you use different quantities of the same kind (e.g., radius, wavelength, altitude, ...), you should probably reach for typed quantities to bring additional safety for those cases. Otherwise, just use simple mode for the remaining quantities. The mp-units library will do its best to protect your project based on the information provided.

    Tip

    You can easily mix simple and typed quantities in your project.

    "},{"location":"users_guide/framework_basics/systems_of_quantities/","title":"Systems of Quantities","text":"

    The physical units libraries on the market typically only scope on modeling one or more systems of units. However, this is not the only system kind to model. Another, and maybe even more important, system kind is a system of quantities.

    Info

    Please note that the mp-units is probably the first library on the Open Source market (in any programming language) that models the ISQ with all its definitions provided in ISO 80000. Please provide feedback if something looks odd or could be improved.

    "},{"location":"users_guide/framework_basics/systems_of_quantities/#dimension-is-not-enough-to-describe-a-quantity","title":"Dimension is not enough to describe a quantity","text":"

    Most of the products on the market are aware of physical dimensions. However, a dimension is not enough to describe a quantity. For example, let's see the following implementation:

    class Box {\n  area base_;\n  length height_;\npublic:\n  Box(length l, length w, length h) : base_(l * w), height_(h) {}\n  // ...\n};\n\nBox my_box(2 * m, 3 * m, 1 * m);\n

    How do you like such an interface? It turns out that in most existing strongly-typed libraries this is often the best we can do

    Another typical question many users ask is how to deal with work and torque. Both of those have the same dimension but are different quantities.

    A similar issue is related to figuring out what should be the result of:

    auto res = 1 * Hz + 1 * Bq + 1 * Bd;\n

    where:

    • Hz (hertz) - unit of frequency
    • Bq (becquerel) - unit of activity
    • Bd (baud) - unit of modulation rate

    All of those quantities have the same dimension, namely \\(\\mathsf{T}^{-1}\\), but probably it is not wise to allow adding, subtracting, or comparing them, as they describe vastly different physical properties.

    If the above example seems too abstract, let's consider a fuel consumption (fuel volume divided by distance, e.g., 6.7 l/km) and an area. Again, both have the same dimension \\(\\mathsf{L}^{2}\\), but probably it wouldn't be wise to allow adding, subtracting, or comparing a fuel consumption of a car and the area of a football field. Such an operation does not have any physical sense and should fail to compile.

    Important

    More than one quantity may be defined for the same dimension:

    • quantities of different kinds (e.g. frequency, modulation rate, activity, ...)
    • quantities of the same kind (e.g. length, width, altitude, distance, radius, wavelength, position vector, ...)

    It turns out that the above issues can't be solved correctly without proper modeling of a system of quantities.

    "},{"location":"users_guide/framework_basics/systems_of_quantities/#quantities-of-the-same-kind","title":"Quantities of the same kind","text":"

    ISO 80000-1

    • Quantities may be grouped together into categories of quantities that are mutually comparable
    • Mutually comparable quantities are called quantities of the same kind
    • Two or more quantities cannot be added or subtracted unless they belong to the same category of mutually comparable quantities
    • Quantities of the same kind within a given system of quantities have the same quantity dimension
    • Quantities of the same dimension are not necessarily of the same kind

    The above quotes from ISO 80000 provide answers to all the issues above. Two quantities can't be added, subtracted, or compared unless they belong to the same kind. As frequency, activity, and modulation rate are different kinds, the expression provided above should not compile.

    "},{"location":"users_guide/framework_basics/systems_of_quantities/#system-of-quantities-is-not-only-about-kinds","title":"System of quantities is not only about kinds","text":"

    ISO 80000 specify hundreds of different quantities. There are plenty of different kinds provided and often each kind contains more than one quantity. In fact, it turns out that such quantities form a hierarchy of quantities of the same kind.

    For example, here are all quantities of the kind length provided in the ISO 80000:

    flowchart TD\n    length --- width[width, breadth]\n    length --- height[height, depth, altitude]\n    width --- thickness\n    width --- diameter\n    width --- radius\n    length --- path_length\n    path_length --- distance\n    distance --- radial_distance\n    length --- wavelength\n    length --- position_vector[\"position_vector\\n{vector}\"]\n    length --- displacement[\"displacement\\n{vector}\"]\n    radius --- radius_of_curvature

    Each of the above quantities expresses some kind of length, and each can be measured with si::metre. However, each of them has different properties, usage, and sometimes even requires a different representation type (notice that position_vector and displacement are vector quantities).

    Such a hierarchy helps us in defining arithmetics and conversion rules for various quantities of the same kind.

    "},{"location":"users_guide/framework_basics/systems_of_quantities/#defining-quantities","title":"Defining quantities","text":"

    In the mp-units library all the information about the quantity is provided with the quantity_spec class template. In order to define a specific quantity a user should inherit a strong type from such an instantiation.

    Tip

    Quantity specification definitions benefit from an explicit object parameter added in C++23 to remove the need for CRTP idiom, which significantly simplifies the code. However, as C++23 is far from being mainstream today, a portability macro QUANTITY_SPEC() is provided and used consistently through the library to allow the code to compile with C++20 compilers, thanks to the CRTP usage under the hood.

    For example, here is how the above quantity kind tree can be modeled in the library:

    C++23C++20Portable
    inline constexpr struct length : quantity_spec<dim_length> {} length;\ninline constexpr struct width : quantity_spec<length> {} width;\ninline constexpr auto breadth = width;\ninline constexpr struct height : quantity_spec<length> {} height;\ninline constexpr auto depth = height;\ninline constexpr auto altitude = height;\ninline constexpr struct thickness : quantity_spec<width> {} thickness;\ninline constexpr struct diameter : quantity_spec<width> {} diameter;\ninline constexpr struct radius : quantity_spec<width> {} radius;\ninline constexpr struct radius_of_curvature : quantity_spec<radius> {} radius_of_curvature;\ninline constexpr struct path_length : quantity_spec<length> {} path_length;\ninline constexpr auto arc_length = path_length;\ninline constexpr struct distance : quantity_spec<path_length> {} distance;\ninline constexpr struct radial_distance : quantity_spec<distance> {} radial_distance;\ninline constexpr struct wavelength : quantity_spec<length> {} wavelength;\ninline constexpr struct position_vector : quantity_spec<length, quantity_character::vector> {} position_vector;\ninline constexpr struct displacement : quantity_spec<length, quantity_character::vector> {} displacement;\n
    inline constexpr struct length : quantity_spec<length, dim_length> {} length;\ninline constexpr struct width : quantity_spec<width, length> {} width;\ninline constexpr auto breadth = width;\ninline constexpr struct height : quantity_spec<height, length> {} height;\ninline constexpr auto depth = height;\ninline constexpr auto altitude = height;\ninline constexpr struct thickness : quantity_spec<thickness, width> {} thickness;\ninline constexpr struct diameter : quantity_spec<diameter, width> {} diameter;\ninline constexpr struct radius : quantity_spec<radius, width> {} radius;\ninline constexpr struct radius_of_curvature : quantity_spec<radius_of_curvature, radius> {} radius_of_curvature;\ninline constexpr struct path_length : quantity_spec<path_length, length> {} path_length;\ninline constexpr auto arc_length = path_length;\ninline constexpr struct distance : quantity_spec<distance, path_length> {} distance;\ninline constexpr struct radial_distance : quantity_spec<radial_distance, distance> {} radial_distance;\ninline constexpr struct wavelength : quantity_spec<wavelength, length> {} wavelength;\ninline constexpr struct position_vector : quantity_spec<position_vector, length, quantity_character::vector> {} position_vector;\ninline constexpr struct displacement : quantity_spec<displacement, length, quantity_character::vector> {} displacement;\n
    QUANTITY_SPEC(length, dim_length);\nQUANTITY_SPEC(width, length);\ninline constexpr auto breadth = width;\nQUANTITY_SPEC(height, length);\ninline constexpr auto depth = height;\ninline constexpr auto altitude = height;\nQUANTITY_SPEC(thickness, width);\nQUANTITY_SPEC(diameter, width);\nQUANTITY_SPEC(radius, width);\nQUANTITY_SPEC(radius_of_curvature, radius);\nQUANTITY_SPEC(path_length, length);\ninline constexpr auto arc_length = path_length;\nQUANTITY_SPEC(distance, path_length);\nQUANTITY_SPEC(radial_distance, distance);\nQUANTITY_SPEC(wavelength, length);\nQUANTITY_SPEC(position_vector, length, quantity_character::vector);\nQUANTITY_SPEC(displacement, length, quantity_character::vector);\n

    Note

    More information on how to define a system of quantities can be found in the \"International System of Quantities (ISQ)\" chapter.

    "},{"location":"users_guide/framework_basics/systems_of_quantities/#comparing-adding-and-subtracting-quantities","title":"Comparing, adding, and subtracting quantities","text":"

    ISO 80000 explicitly states that width and height are quantities of the same kind, and as such they:

    • are mutually comparable,
    • can be added and subtracted.

    If we take the above for granted, the only reasonable result of 1 * width + 1 * height is 2 * length, where the result of length is known as a common quantity type. A result of such an equation is always the first common node in a hierarchy tree of the same kind. For example:

    static_assert(common_quantity_spec(isq::width, isq::height) == isq::length);\nstatic_assert(common_quantity_spec(isq::thickness, isq::radius) == isq::width);\nstatic_assert(common_quantity_spec(isq::distance, isq::path_length) == isq::path_length);\n
    "},{"location":"users_guide/framework_basics/systems_of_quantities/#converting-between-quantities","title":"Converting between quantities","text":"

    Based on the same hierarchy of quantities of kind length, we can define quantity conversion rules.

    1. Implicit conversions

      • every width is a length
      • every radius is a width
      static_assert(implicitly_convertible(isq::width, isq::length));\nstatic_assert(implicitly_convertible(isq::radius, isq::width));\nstatic_assert(implicitly_convertible(isq::radius, isq::length));\n
    2. Explicit conversions

      • not every length is a width
      • not every width is a radius
      static_assert(!implicitly_convertible(isq::length, isq::width));\nstatic_assert(!implicitly_convertible(isq::width, isq::radius));\nstatic_assert(!implicitly_convertible(isq::length, isq::radius));\nstatic_assert(explicitly_convertible(isq::length, isq::width));\nstatic_assert(explicitly_convertible(isq::width, isq::radius));\nstatic_assert(explicitly_convertible(isq::length, isq::radius));\n
    3. Explicit casts

      • height is not a width
      • both height and width are quantities of kind length
      static_assert(!implicitly_convertible(isq::height, isq::width));\nstatic_assert(!explicitly_convertible(isq::height, isq::width));\nstatic_assert(castable(isq::height, isq::width));\n
    4. No conversion

      • time has nothing in common with length
      static_assert(!implicitly_convertible(isq::time, isq::length));\nstatic_assert(!explicitly_convertible(isq::time, isq::length));\nstatic_assert(!castable(isq::time, isq::length));\n
    "},{"location":"users_guide/framework_basics/systems_of_quantities/#hierarchies-of-derived-quantities","title":"Hierarchies of derived quantities","text":"

    Derived quantity equations often do not automatically form a hierarchy tree. This is why it is sometimes not obvious what such a tree should look like. Also, ISO explicitly states:

    ISO/IEC Guide 99

    The division of \u2018quantity\u2019 according to \u2018kind of quantity\u2019 is, to some extent, arbitrary.

    The below presents some arbitrary hierarchy of derived quantities of kind energy:

    flowchart TD\n    energy[\"energy\\n(mass * length<sup>2</sup> / time<sup>2</sup>)\"]\n    energy --- mechanical_energy\n    mechanical_energy --- potential_energy\n    potential_energy --- gravitational_potential_energy[\"gravitational_potential_energy\\n(mass * acceleration_of_free_fall * height)\"]\n    potential_energy --- elastic_potential_energy[\"elastic_potential_energy\\n(spring_constant * amount_of_compression<sup>2</sup>)\"]\n    mechanical_energy --- kinetic_energy[\"kinetic_energy\\n(mass * speed<sup>2</sup>)\"]\n    energy --- enthalpy\n    enthalpy --- internal_energy[internal_energy, thermodynamic_energy]\n    internal_energy --- Helmholtz_energy[Helmholtz_energy, Helmholtz_function]\n    enthalpy --- Gibbs_energy[Gibbs_energy, Gibbs_function]\n    energy --- active_energy

    Notice, that even though all of those quantities have the same dimension and can be expressed in the same units, they have different quantity equations that can be used to create them implicitly:

    • energy is the most generic one and thus can be created from base quantities of mass, length, and time. As those are also the roots of quantities of their kinds and all other quantities from their trees are implicitly convertible to them (we agreed on that \"every width is a length\" already), it means that an energy can be implicitly constructed from any quantity of mass, length, and time:

      static_assert(implicitly_convertible(isq::mass * pow<2>(isq::length) / pow<2>(isq::time), isq::energy));\nstatic_assert(implicitly_convertible(isq::mass * pow<2>(isq::height) / pow<2>(isq::time), isq::energy));\n
    • mechanical energy is a more \"specialized\" quantity than energy (not every energy is a mechanical energy). It is why an explicit cast is needed to convert from either energy or the results of its quantity equation:

      static_assert(!implicitly_convertible(isq::energy, isq::mechanical_energy));\nstatic_assert(explicitly_convertible(isq::energy, isq::mechanical_energy));\nstatic_assert(!implicitly_convertible(isq::mass * pow<2>(isq::length) / pow<2>(isq::time),\n                                      isq::mechanical_energy));\nstatic_assert(explicitly_convertible(isq::mass * pow<2>(isq::length) / pow<2>(isq::time),\n                                     isq::mechanical_energy));\n
    • gravitational potential energy is not only even more specialized one but additionally, it is special in a way that it provides its own \"constrained\" quantity equation. Maybe not every mass * pow<2>(length) / pow<2>(time) is a gravitational potential energy, but every mass * acceleration_of_free_fall * height is.

      static_assert(!implicitly_convertible(isq::energy, gravitational_potential_energy));\nstatic_assert(explicitly_convertible(isq::energy, gravitational_potential_energy));\nstatic_assert(!implicitly_convertible(isq::mass * pow<2>(isq::length) / pow<2>(isq::time),\n                                      gravitational_potential_energy));\nstatic_assert(explicitly_convertible(isq::mass * pow<2>(isq::length) / pow<2>(isq::time),\n                                     gravitational_potential_energy));\nstatic_assert(implicitly_convertible(isq::mass * isq::acceleration_of_free_fall * isq::height,\n                                     gravitational_potential_energy));\n
    "},{"location":"users_guide/framework_basics/systems_of_quantities/#modeling-a-quantity-kind","title":"Modeling a quantity kind","text":"

    In the physical units library, we also need an abstraction describing an entire family of quantities of the same kind. Such quantities have not only the same dimension but also can be expressed in the same units.

    To annotate a quantity to represent its kind (and not just a hierarchy tree's root quantity) we introduced a kind_of<> specifier. For example, to express any quantity of length, we need to type kind_of<isq::length>.

    Important

    isq::length and kind_of<isq::length> are two different things.

    Such an entity behaves as any quantity of its kind. This means that it is implicitly convertible to any quantity in a tree.

    static_assert(!implicitly_convertible(isq::length, isq::height));\nstatic_assert(implicitly_convertible(kind_of<isq::length>, isq::height));\n

    Additionally, the result of operations on quantity kinds is also a quantity kind:

    static_assert(same_type<kind_of<isq::length> / kind_of<isq::time>, kind_of<isq::length / isq::time>>);\n

    However, if at least one equation's operand is not a quantity kind, the result becomes a \"strong\" quantity where all the kinds are converted to the hierarchy tree's root quantities:

    static_assert(!same_type<kind_of<isq::length> / isq::time, kind_of<isq::length / isq::time>>);\nstatic_assert(same_type<kind_of<isq::length> / isq::time, isq::length / isq::time>);\n

    Info

    Only a root quantity from the hierarchy tree or the one marked with is_kind specifier in the quantity_spec definition can be put as a template parameter to the kind_of specifier. For example, kind_of<isq::width> will fail to compile. However, we can call get_kind(q) to obtain a kind of any quantity:

    static_assert(get_kind(isq::width) == kind_of<isq::length>);\n
    "},{"location":"users_guide/framework_basics/systems_of_units/","title":"Systems of Units","text":"

    Modeling a system of units is probably the most important feature and a selling point of every physical units library. Thanks to that, the library can protect users from performing invalid operations on quantities and provide automated conversion factors between various compatible units.

    Probably all the libraries in the wild model the SI and many of them provide support for additional units belonging to various other systems (e.g., imperial, cgs, etc).

    "},{"location":"users_guide/framework_basics/systems_of_units/#systems-of-units-are-based-on-systems-of-quantities","title":"Systems of Units are based on Systems of Quantities","text":"

    Systems of quantities specify a set of quantities and equations relating to those quantities. Those equations do not take any unit or a numerical representation into account at all. To create a quantity, we need to add those missing pieces of information. This is where a system of units kicks in.

    The SI is explicitly stated to be based on the ISQ. Among others, it defines 7 base units, one for each base quantity. In the mp-units this is expressed by associating a quantity kind (that we discussed in detail in the previous chapter) with a unit that is used to express it:

    inline constexpr struct metre : named_unit<\"m\", kind_of<isq::length>> {} metre;\n

    Important

    The kind_of<isq::length> above states explicitly that this unit has an associated quantity kind. In other words, si::metre (and scaled units based on it) can be used to express the amount of any quantity of kind length.

    "},{"location":"users_guide/framework_basics/systems_of_units/#units-compose","title":"Units compose","text":"

    One of the most vital points of the SI system is that its units compose. This allows providing thousands of different units for hundreds of various quantities with a tiny set of predefined units and prefixes.

    The same is modeled in the mp-units library, which also allows composing predefined units to create a nearly infinite number of different derived units. For example, one can write:

    quantity<si::metre / si::second> q;\n

    to express a quantity of speed. The resulting quantity type is implicitly inferred from the unit equation by repeating the same operations on the associated quantity kinds.

    "},{"location":"users_guide/framework_basics/systems_of_units/#many-shades-of-the-same-unit","title":"Many shades of the same unit","text":"

    The SI provides the names for 22 common coherent units of 22 derived quantities.

    Each such named derived unit is a result of a specific predefined unit equation. For example, a unit of power quantity is defined in the library as:

    inline constexpr struct watt : named_unit<\"W\", joule / second> {} watt;\n

    However, a power quantity can be expressed in other units as well. For example, the following:

    auto q1 = 42 * W;\nstd::cout << q1 << \"\\n\";\nstd::cout << q1.in(J / s) << \"\\n\";\nstd::cout << q1.in(N * m / s) << \"\\n\";\nstd::cout << q1.in(kg * m2 / s3) << \"\\n\";\n

    prints:

    42 W\n42 J/s\n42 N m/s\n42 kg m\u00b2/s\u00b3\n

    All of the above quantities are equivalent and mean exactly the same.

    "},{"location":"users_guide/framework_basics/systems_of_units/#constraining-a-derived-unit-to-work-only-with-a-specific-derived-quantity","title":"Constraining a derived unit to work only with a specific derived quantity","text":"

    Some derived units are valid only for specific derived quantities. For example, SI specifies both hertz and becquerel derived units with the same unit equation 1 / s. However, it also explicitly states:

    SI Brochure

    The hertz shall only be used for periodic phenomena and the becquerel shall only be used for stochastic processes in activity referred to a radionuclide.

    The above means that the usage of becquerel as a unit of a frequency quantity is an error.

    The library allows constraining such units to work only with quantities of a specific kind in the following way:

    inline constexpr struct hertz : named_unit<\"Hz\", one / second, kind_of<isq::frequency>> {} hertz;\ninline constexpr struct becquerel : named_unit<\"Bq\", one / second, kind_of<isq::activity>> {} becquerel;\n

    With the above, hertz can only be used with frequencies, while becquerel should only be used with quantities of activity. This means that the following equation will not compile:

    auto q = 1 * Hz + 1 * Bq;   // Fails to compile\n

    This is exactly what we wanted to achieve to improve the type-safety of the library.

    "},{"location":"users_guide/framework_basics/systems_of_units/#prefixed-units","title":"Prefixed units","text":"

    Besides named units, the SI specifies also 24 prefixes (all being a power of 10) that can be prepended to all named units to obtain various scaled versions of them.

    Implementation of std::ratio provided by all major compilers is able to express only 16 of them. This is why, in the mp-units, we had to find an alternative way to represent unit magnitude in a more flexible way.

    Each prefix is implemented similarly to the following:

    template<PrefixableUnit auto U> struct quecto_ : prefixed_unit<\"q\", mag_power<10, -30>, U> {};\ntemplate<PrefixableUnit auto U> inline constexpr quecto_<U> quecto;\n

    and then a PrefixableUnit can be prefixed in the following way:

    inline constexpr auto qm = quecto<metre>;\n

    The usage of mag_power not only enables providing support for SI prefixes, but it can also efficiently represent any rational magnitude. For example, IEC 80000 prefixes used in the IT industry can be implemented as:

    template<PrefixableUnit auto U> struct yobi_ : prefixed_unit<\"Yi\", mag_power<2, 80>, U> {};\ntemplate<PrefixableUnit auto U> inline constexpr yobi_<U> yobi;\n
    "},{"location":"users_guide/framework_basics/systems_of_units/#scaled-units","title":"Scaled units","text":"

    In the SI, all units are either base or derived units or prefixed versions of those. However, those are only some of the options possible.

    For example, there is a list of off-system units accepted for use with SI. Those are scaled versions of the SI units with ratios that can't be explicitly expressed with predefined SI prefixes. Those include units like minute, hour, or electronvolt:

    inline constexpr struct minute : named_unit<\"min\", mag<60> * si::second> {} minute;\ninline constexpr struct hour : named_unit<\"h\", mag<60> * minute> {} hour;\ninline constexpr struct electronvolt : named_unit<\"eV\", mag<ratio{1'602'176'634, 1'000'000'000}> * mag_power<10, -19> * si::joule> {} electronvolt;\n

    Also, units of other systems of units are often defined in terms of scaled versions of the SI units. For example, the international yard is defined as:

    inline constexpr struct yard : named_unit<\"yd\", mag<ratio{9'144, 10'000}> * si::metre> {} yard;\n

    For some units, a magnitude might also be irrational. The best example here is a degree which is defined using a floating-point magnitude having a factor of the number \u03c0 (Pi):

    inline constexpr struct mag_pi : magnitude<std::numbers::pi_v<long double>> {} mag_pi;\n
    inline constexpr struct degree : named_unit<{u8\"\u00b0\", \"deg\"}, mag_pi / mag<180> * si::radian> {} degree;\n
    "},{"location":"users_guide/framework_basics/text_output/","title":"Text Output","text":"

    Besides providing dimensional analysis and units conversions, the library also tries hard to print any quantity in the most user-friendly way.

    Note

    The library does not provide a text output for quantity points, as printing just a number and a unit is not enough to adequately describe a quantity point. Often, an additional postfix is required.

    For example, the text output of 42 m may mean many things and can also be confused with an output of a regular quantity. On the other hand, printing 42 m AMSL for altitudes above mean sea level is a much better solution, but the library does not have enough information to print it that way by itself.

    "},{"location":"users_guide/framework_basics/text_output/#derived-unit-symbols-generation","title":"Derived unit symbols generation","text":"

    The library creates symbols for derived ones based on the provided definitions for base units.

    "},{"location":"users_guide/framework_basics/text_output/#unit_symbol_formatting","title":"unit_symbol_formatting","text":"

    unit_symbol_formatting is a data type describing the configuration of the symbol generation algorithm. It contains three orthogonal fields, each with a default value.

    enum class text_encoding : std::int8_t {\n  unicode,  // m\u00b3;  \u00b5s\n  ascii,    // m^3; us\n  default_encoding = unicode\n};\n\nenum class unit_symbol_solidus : std::int8_t {\n  one_denominator,  // m/s;   kg m\u207b\u00b9 s\u207b\u00b9\n  always,           // m/s;   kg/(m s)\n  never,            // m s\u207b\u00b9; kg m\u207b\u00b9 s\u207b\u00b9\n  default_denominator = one_denominator\n};\n\nenum class unit_symbol_separator : std::int8_t {\n  space,          // kg m\u00b2/s\u00b2\n  half_high_dot,  // kg\u22c5m\u00b2/s\u00b2  (valid only for unicode encoding)\n  default_separator = space\n};\n\nstruct unit_symbol_formatting {\n  text_encoding encoding = text_encoding::default_encoding;\n  unit_symbol_solidus solidus = unit_symbol_solidus::default_denominator;\n  unit_symbol_separator separator = unit_symbol_separator::default_separator;\n};\n
    "},{"location":"users_guide/framework_basics/text_output/#unit_symbol","title":"unit_symbol()","text":"

    Returns a std::string_view with the symbol of a unit for the provided configuration:

    template<unit_symbol_formatting fmt = unit_symbol_formatting{}, typename CharT = char, Unit U>\n[[nodiscard]] consteval auto unit_symbol(U);\n

    For example:

    static_assert(unit_symbol<{.solidus = unit_symbol_solidus::never,\n                           .separator = unit_symbol_separator::half_high_dot}>(kg * m / s2) == \"kg\u22c5m\u22c5s\u207b\u00b2\");\n

    Note

    std::string_view is returned only when C++23 is available. Otherwise, an instance of a basic_fixed_string is being returned.

    "},{"location":"users_guide/framework_basics/text_output/#unit_symbol_to","title":"unit_symbol_to()","text":"

    Inserts the generated unit symbol to the output text iterator at runtime based on the provided configuration.

    template<typename CharT = char, std::output_iterator<CharT> Out, Unit U>\nconstexpr Out unit_symbol_to(Out out, U u, unit_symbol_formatting fmt = unit_symbol_formatting{});\n

    For example:

    std::string txt;\nunit_symbol_to(std::back_inserter(txt), kg * m / s2,\n               {.solidus = unit_symbol_solidus::never, .separator = unit_symbol_separator::half_high_dot});\nstd::cout << txt << \"\\n\";\n

    The above prints:

    kg\u22c5m\u22c5s\u207b\u00b2\n
    "},{"location":"users_guide/framework_basics/text_output/#quantity-text-output","title":"Quantity text output","text":""},{"location":"users_guide/framework_basics/text_output/#customization-point","title":"Customization point","text":"

    The SI Brochure says:

    SI Brochure

    The numerical value always precedes the unit and a space is always used to separate the unit from the number. ... The only exceptions to this rule are for the unit symbols for degree, minute and second for plane angle, \u00b0, \u2032 and \u2033, respectively, for which no space is left between the numerical value and the unit symbol.

    To support the above, the library exposes space_before_unit_symbol customization point. By default, its value is true for all the units, so the space between a number and a unit will be present in the output text. To change this behavior, we have to provide a partial specialization for a specific unit:

    template<>\ninline constexpr bool space_before_unit_symbol<non_si::degree> = false;\n

    Note

    The above works only for the default formatting. In case we provide our own format specification (e.g., std::format(\"{:%Q %q}\", q)), the library will always obey this specification for all the units (no matter of what is the actual value of the space_before_unit_symbol customization point) and the separating space will always be present in this case.

    "},{"location":"users_guide/framework_basics/text_output/#output-streams","title":"Output streams","text":"

    Tip

    The output streaming support is opt-in and can be enabled by including the <mp-units/ostream.h> header file.

    The easiest way to print a quantity is to provide its object to the output stream:

    using namespace mp_units;\nusing namespace mp_units::si::unit_symbols;\nusing namespace mp_units::international::unit_symbols;\n\nconst QuantityOf<isq::speed> auto v1 = avg_speed(220. * km, 2 * h);\nconst QuantityOf<isq::speed> auto v2 = avg_speed(140. * mi, 2 * h);\nstd::cout << v1 << '\\n';  // 110 km/h\nstd::cout << v2 << '\\n';  // 70 mi/h\n

    The text output will always print the value of a quantity typically followed by a space and then the symbol of a unit associated with this quantity.

    Important: Don't assume a unit

    Remember that when we deal with a quantity of an \"unknown\" (e.g., auto) type, it is a good practice to always convert the unit to the expected one before passing it to the text output:

    std::cout << v1.in(km / h) << '\\n';       // 110 km/h\nstd::cout << v1.force_in(m / s) << '\\n';  // 30.5556 m/s\n
    "},{"location":"users_guide/framework_basics/text_output/#output-stream-formatting","title":"Output stream formatting","text":"

    Only basic formatting can be applied to output streams. It includes control over width, fill, and alignment of the entire quantity and formatting of a quantity numerical value according to the general C++ rules:

    std::cout << \"|\" << std::setw(10) << 123 * m << \"|\\n\";                       // |     123 m|\nstd::cout << \"|\" << std::setw(10) << std::left << 123 * m << \"|\\n\";          // |123 m     |\nstd::cout << \"|\" << std::setw(10) << std::setfill('*') << 123 * m << \"|\\n\";  // |123 m*****|\n

    Note

    To have more control over the formatting of the quantity that is printed with the output stream just use std::cout << std::format(...).

    "},{"location":"users_guide/framework_basics/text_output/#stdformat","title":"std::format","text":"

    Tip

    The text formatting facility support is opt-in and can be enabled by including the <mp-units/format.h> header file.

    The mp-units library provides custom formatters for std::format facility which allows fine-grained control over what and how it is being printed in the text output.

    "},{"location":"users_guide/framework_basics/text_output/#grammar","title":"Grammar","text":"
    quantity-format-spec  ::=  [fill-and-align] [width] [quantity-specs]\nquantity-specs        ::=  conversion-spec\n                           quantity-specs conversion-spec\n                           quantity-specs literal-char\nliteral-char          ::=  any character other than '{' or '}'\nconversion-spec       ::=  '%' type\ntype                  ::=  [rep-modifier] 'Q'\n                           [unit-modifier] 'q'\nrep-modifier          ::=  [sign] [#] [precision] [L] [rep-type]\nrep-type              ::=  one of\n                           a A b B d e E f F g G o x X\nunit-modifier         ::=  [text-encoding] [unit-symbol-solidus] [unit-symbol-separator]\n                           [text-encoding] [unit-symbol-separator] [unit-symbol-solidus]\n                           [unit-symbol-solidus] [text-encoding] [unit-symbol-separator]\n                           [unit-symbol-solidus] [unit-symbol-separator] [text-encoding]\n                           [unit-symbol-separator] [text-encoding] [unit-symbol-solidus]\n                           [unit-symbol-separator] [unit-symbol-solidus] [text-encoding]\ntext-encoding         ::=  one of\n                           U A\nunit-symbol-solidus   ::=  one of\n                           o a n\nunit-symbol-separator ::=  one of\n                           s d\n

    In the above grammar:

    • fill-and-align, width, sign, #, precision, and L tokens, as well as the individual tokens of rep-type are defined in the format.string.std chapter of the C++ standard specification,
    • tokens Q and q of type are described in the time.format chapter of the C++ standard specification,
    • text-encoding tokens specify the unit text encoding:
      • U (default) uses the Unicode symbols defined by the SI specification (e.g., m\u00b3, \u00b5s)
      • A token forces non-standard ASCII-only output (e.g., m^3, us)
    • unit-symbol-solidus tokens specify how the division of units should look like:
      • o (default) outputs / only when there is only one unit in the denominator, otherwise negative exponents are printed (e.g., m/s, kg m\u207b\u00b9 s\u207b\u00b9)
      • a always uses solidus (e.g., m/s, kg/(m s))
      • n never prints solidus, which means that negative exponents are always used (e.g., m s\u207b\u00b9, kg m\u207b\u00b9 s\u207b\u00b9)
    • unit-symbol-separator tokens specify how multiplied unit symbols should be separated:
      • s (default) uses space as a separator (e.g., kg m\u00b2/s\u00b2)
      • d uses half-high dot (\u22c5) as a separator (e.g., kg\u22c5m\u00b2/s\u00b2)
    "},{"location":"users_guide/framework_basics/text_output/#default-formatting","title":"Default formatting","text":"

    To format quantity values, the formatting facility uses quantity-format-spec. If left empty, the default formatting is applied. The same default formatting is also applied to the output streams. This is why the following code lines produce the same output:

    std::cout << \"Distance: \" << 123 * km << \"\\n\";\nstd::cout << std::format(\"Distance: {}\\n\", 123 * km);\nstd::cout << std::format(\"Distance: {:%Q %q}\\n\", 123 * km);\n

    Note

    For some quantities, the {:%Q %q} format may provide a different output than the default one. It will happen, for example for:

    • units for which space_before_unit_symbol customization point is set to false,
    • quantities of dimension one with a unit one.
    "},{"location":"users_guide/framework_basics/text_output/#controlling-width-fill-and-alignment","title":"Controlling width, fill, and alignment","text":"

    To control width, fill, and alignment, the C++ standard grammar tokens fill-and-align and width are being used, and they treat a quantity value and symbol as a contiguous text:

    std::println(\"|{:0}|\", 123 * m);     // |123 m|\nstd::println(\"|{:10}|\", 123 * m);    // |     123 m|\nstd::println(\"|{:<10}|\", 123 * m);   // |123 m     |\nstd::println(\"|{:>10}|\", 123 * m);   // |     123 m|\nstd::println(\"|{:^10}|\", 123 * m);   // |  123 m   |\nstd::println(\"|{:*<10}|\", 123 * m);  // |123 m*****|\nstd::println(\"|{:*>10}|\", 123 * m);  // |*****123 m|\nstd::println(\"|{:*^10}|\", 123 * m);  // |**123 m***|\n

    Note

    std::println is a C++23 facility. In case we do not have access to C++23, we can obtain the same output with:

    std::cout << std::format(\"<format-string>\\n\", <format-args>);\n
    "},{"location":"users_guide/framework_basics/text_output/#quantity-value-symbol-or-both","title":"Quantity value, symbol, or both?","text":"

    The user can easily decide to either print a whole quantity (value and symbol) or only its parts. Also, a custom style of quantity formatting might be applied:

    std::println(\"{:%Q}\", 123 * km);    // 123\nstd::println(\"{:%q}\", 123 * km);    // km\nstd::println(\"{:%Q%q}\", 123 * km);  // 123km\n
    "},{"location":"users_guide/framework_basics/text_output/#quantity-value-formatting","title":"Quantity value formatting","text":"

    sign token allows us to specify how the value's sign is being printed:

    std::println(\"{0:%Q %q},{0:%+Q %q},{0:%-Q %q},{0:% Q %q}\", 1 * m);   // 1 m,+1 m,1 m, 1 m\nstd::println(\"{0:%Q %q},{0:%+Q %q},{0:%-Q %q},{0:% Q %q}\", -1 * m);  // -1 m,-1 m,-1 m,-1 m\n

    where:

    • + indicates that a sign should be used for both non-negative and negative numbers,
    • - indicates that a sign should be used for negative numbers and negative zero only (this is the default behavior),
    • <space> indicates that a leading space should be used for non-negative numbers other than negative zero, and a minus sign for negative numbers and negative zero.

    precision token is allowed only for floating-point representation types:

    std::println(\"{:%.0Q %q}\", 1.2345 * m);  // 1 m\nstd::println(\"{:%.1Q %q}\", 1.2345 * m);  // 1.2 m\nstd::println(\"{:%.2Q %q}\", 1.2345 * m);  // 1.23 m\n

    rep-type specifies how a value of the representation type is being printed. For integral types:

    std::println(\"{:%bQ %q}\", 42 * m);    // 101010 m\nstd::println(\"{:%BQ %q}\", 42 * m);    // 101010 m\nstd::println(\"{:%dQ %q}\", 42 * m);    // 42 m\nstd::println(\"{:%oQ %q}\", 42 * m);    // 52 m\nstd::println(\"{:%xQ %q}\", 42 * m);    // 2a m\nstd::println(\"{:%XQ %q}\", 42 * m);    // 2A m\n

    The above can be printed in an alternate version thanks to the # token:

    std::println(\"{:%#bQ %q}\", 42 * m);   // 0b101010 m\nstd::println(\"{:%#BQ %q}\", 42 * m);   // 0B101010 m\nstd::println(\"{:%#oQ %q}\", 42 * m);   // 052 m\nstd::println(\"{:%#xQ %q}\", 42 * m);   // 0x2a m\nstd::println(\"{:%#XQ %q}\", 42 * m);   // 0X2A m\n

    For floating-point values, the rep-type token works as follows:

    std::println(\"{:%aQ %q}\",   1.2345678 * m);      // 0x1.3c0ca2a5b1d5dp+0 m\nstd::println(\"{:%.3aQ %q}\", 1.2345678 * m);      // 0x1.3c1p+0 m\nstd::println(\"{:%AQ %q}\",   1.2345678 * m);      // 0X1.3C0CA2A5B1D5DP+0 m\nstd::println(\"{:%.3AQ %q}\", 1.2345678 * m);      // 0X1.3C1P+0 m\nstd::println(\"{:%eQ %q}\",   1.2345678 * m);      // 1.234568e+00 m\nstd::println(\"{:%.3eQ %q}\", 1.2345678 * m);      // 1.235e+00 m\nstd::println(\"{:%EQ %q}\",   1.2345678 * m);      // 1.234568E+00 m\nstd::println(\"{:%.3EQ %q}\", 1.2345678 * m);      // 1.235E+00 m\nstd::println(\"{:%gQ %q}\",   1.2345678 * m);      // 1.23457 m\nstd::println(\"{:%gQ %q}\",   1.2345678e8 * m);    // 1.23457e+08 m\nstd::println(\"{:%.3gQ %q}\", 1.2345678 * m);      // 1.23 m\nstd::println(\"{:%.3gQ %q}\", 1.2345678e8 * m);    // 1.23e+08 m\nstd::println(\"{:%GQ %q}\",   1.2345678 * m);      // 1.23457 m\nstd::println(\"{:%GQ %q}\",   1.2345678e8 * m);    // 1.23457E+08 m\nstd::println(\"{:%.3GQ %q}\", 1.2345678 * m);      // 1.23 m\nstd::println(\"{:%.3GQ %q}\", 1.2345678e8 * m);    // 1.23E+08 m\n
    "},{"location":"users_guide/framework_basics/text_output/#unit-symbol-formatting","title":"Unit symbol formatting","text":"

    Unit symbols of some quantities are specified to use Unicode signs by the SI (e.g., \u03a9 symbol for the resistance quantity). The mp-units library follows this by default. From the engineering point of view, sometimes Unicode text might not be the best solution as terminals of many (especially embedded) devices are ASCII-only. In such a case, the unit symbol can be forced to be printed using ASCII-only characters thanks to the text-encoding token:

    std::println(\"{}\", 10 * si::ohm);             // 10 \u03a9\nstd::println(\"{:%Q %Aq}\", 10 * si::ohm);      // 10 ohm\nstd::println(\"{}\", 125 * us);                 // 125 \u00b5s\nstd::println(\"{:%Q %Aq}\", 125 * us);          // 125 us\nstd::println(\"{}\", 9.8 * (m / s2));           // 9.8 m/s\u00b2\nstd::println(\"{:%Q %Aq}\", 9.8 * (m / s2));    // 9.8 m/s^2\n

    Additionally, both ISQ and SI leave some freedom on how to print unit symbols. This is why two additional tokens were introduced.

    unit-symbol-solidus specifies how the division of units should look like. By default, / will be used only when the denominator contains only one unit. However, with the a or n options, we can force the facility to print the / character always (even when there are more units in the denominator), or never in which case a parenthesis will be added to enclose all denominator units.

    std::println(\"{:%Q %q}\", 1 * m / s);         // 1 m/s\nstd::println(\"{:%Q %q}\", 1 * kg / m / s2);   // 1 kg m\u207b\u00b9 s\u207b\u00b2\nstd::println(\"{:%Q %aq}\", 1 * m / s);        // 1 m/s\nstd::println(\"{:%Q %aq}\", 1 * kg / m / s2);  // 1 kg/(m s\u00b2)\nstd::println(\"{:%Q %nq}\", 1 * m / s);        // 1 m s\u207b\u00b9\nstd::println(\"{:%Q %nq}\", 1 * kg / m / s2);  // 1 kg m\u207b\u00b9 s\u207b\u00b2\n

    Also, there are a few options to separate the units being multiplied:

    ISO 80000-1

    When symbols for quantities are combined in a product of two or more quantities, this combination is indicated in one of the following ways: ab, a b, a \u00b7 b, a \u00d7 b

    NOTE 1 In some fields, e.g., vector algebra, distinction is made between a \u2219 b and a \u00d7 b.

    As of today, the mp-units library only supports a b and a \u00b7 b. Additionally, we decided that the extraneous space in the latter case makes the result too verbose, so we decided to just use the \u00b7 symbol as a separator.

    Note

    Please let us know if you require more formatting options here.

    The unit-symbol-separator token allows us to obtain the following outputs:

    std::println(\"{:%Q %q}\", 1 * kg * m2 / s2);   // 1 kg m\u00b2/s\u00b2\nstd::println(\"{:%Q %dq}\", 1 * kg * m2 / s2);  // 1 kg\u22c5m\u00b2/s\u00b2\n
    "},{"location":"users_guide/framework_basics/the_affine_space/","title":"The Affine Space","text":"

    The affine space has two types of entities:

    • Point - a position specified with coordinate values (e.g., location, address, etc.)
    • Vector - the difference between two points (e.g., shift, offset, displacement, duration, etc.)

    Note

    The Vector described here is specific to the affine space theory and is not the same thing as the quantity of a vector character that we discussed in the \"Scalars, vectors, and tensors\" chapter (although, in some cases, those terms may overlap).

    "},{"location":"users_guide/framework_basics/the_affine_space/#operations-in-the-affine-space","title":"Operations in the affine space","text":"

    Here are the primary operations one can do in the affine space:

    • Vector + Vector -> Vector
    • Vector - Vector -> Vector
    • -Vector -> Vector
    • Vector * Scalar -> Vector
    • Scalar * Vector -> Vector
    • Vector / Scalar -> Vector
    • Point - Point -> Vector
    • Point + Vector -> Point
    • Vector + Point -> Point
    • Point - Vector -> Point

    Important

    It is not possible to:

    • add two Points,
    • subtract a Point from a Vector,
    • multiply nor divide Points with anything else.
    "},{"location":"users_guide/framework_basics/the_affine_space/#points-are-more-common-than-most-of-us-imagine","title":"Points are more common than most of us imagine","text":"

    Point abstractions should be used more often in the C++ software. They are not only about temperature or time. Points are everywhere around us and should become more popular in the products we implement. They can be used to implement:

    • temperature points,
    • timestamps,
    • daily mass readouts from the scale,
    • altitudes of mountain peaks on a map,
    • current speed displayed on a car's speed-o-meter,
    • today's price of instruments on the market,
    • and many more.

    Improving the affine space's Points intuition will allow us to write better and safer software.

    "},{"location":"users_guide/framework_basics/the_affine_space/#vector-is-modeled-by-quantity","title":"Vector is modeled by quantity","text":"

    Up until now, each time we used a quantity in our code, we were modeling some kind of a difference between two things:

    • the distance between two points,
    • duration between two time points,
    • the difference in speed (even if relative to zero).

    As we already know, a quantity type provides all operations required for a Vector type in the affine space.

    "},{"location":"users_guide/framework_basics/the_affine_space/#point-is-modeled-by-quantity_point-and-pointorigin","title":"Point is modeled by quantity_point and PointOrigin","text":"

    In the mp-units library the Point abstraction is modelled by:

    • PointOrigin concept that specifies measurement origin,
    • quantity_point class template that specifies a Point relative to a specific predefined origin.
    "},{"location":"users_guide/framework_basics/the_affine_space/#quantity_point","title":"quantity_point","text":"

    The quantity_point class template specifies an absolute quantity measured from a predefined origin:

    template<Reference auto R,\n         PointOriginFor<get_quantity_spec(R)> auto PO = default_point_origin(R),\n         RepresentationOf<get_quantity_spec(R).character> Rep = double>\nclass quantity_point;\n

    As we can see above, the quantity_point class template exposes one additional parameter compared to quantity. The PO parameter satisfies a PointOriginFor concept and specifies the origin of our measurement scale. By default, it is initialized with a quantity's zeroth point using the following rules:

    • if the measurement unit of a quantity specifies its point origin in its definition (e.g., degree Celsius), then this point is being used,
    • otherwise, an instantiation of zeroth_point_origin<QuantitySpec> is being used which provides a zeroth point for a specific quantity type.

    Tip

    The quantity_point definition can be found in the mp-units/quantity_point.h header file.

    "},{"location":"users_guide/framework_basics/the_affine_space/#implicit-point-origin","title":"Implicit point origin","text":"

    Let's assume that Alice goes for a trip driving a car. She likes taking notes about interesting places that she visits on the road. For every such item, she writes down:

    • its name,
    • a readout from the car's odometer at the location,
    • a current timestamp.

    We can implement this in the following way:

    using std::chrono::system_clock;\n\nstruct trip_log_item {\n  std::string name;\n  quantity_point<isq::distance[km]> odometer;\n  quantity_point<si::second> timestamp;\n};\nusing trip_log = std::vector<trip_log_item>;\n
    trip_log log;\n\nquantity_point timestamp_1{quantity{system_clock::now().time_since_epoch()}};\nlog.emplace_back(\"home\", quantity_point{1356 * km}, timestamp_1);\n\n// some time passes\n\nquantity_point timestamp_2{quantity{system_clock::now().time_since_epoch()}};\nlog.emplace_back(\"castle\", quantity_point{1401 * km}, timestamp_2);\n

    This is an excellent example of where points are helpful. There is no doubt about the correctness of their usage in this scenario:

    • adding two odometer readouts or two timestamps have no physical sense, and that is why we will expect a compile-time error when we try to perform such operations accidentally,
    • subtracting two odometer readouts or timestamps is perfectly valid and results in a quantity storing the interval value between the two points.

    Having such a database, we can print the trip log in the following way:

    for (const auto& item : log) {\n  std::cout << \"POI: \" << item.name << \"\\n\";\n  std::cout << \"- Distance from home: \" << item.odometer - log.front().odometer << \"\\n\";\n  std::cout << \"- Trip duration from start: \" << (item.timestamp - log.front().timestamp).in(non_si::minute) << \"\\n\";\n}\n

    Moreover, if Alice had reset the car's trip odometer before leaving home, we could have rewritten one of the previous lines like that:

    std::cout << \"Distance from home: \" << item.odometer.quantity_from_zero() << \"\\n\";\n

    The above always returns a quantity measured from the \"ultimate\" zeroth point of a scale used for this specific quantity type.

    Tip

    Storing Points is the most efficient representation we can choose in this scenario:

    • to store a value, we read it directly from the instrument, and no additional transformation is needed,
    • to print the absolute value (e.g., odometer), we have the value available right away,
    • to get any relative quantity (e.g., distance from the start, distance from the previous point, etc.), we have to perform a single subtraction operation.

    If we stored Vectors in our database instead, we would have to pay at runtime for additional operations:

    • to store a quantity, we would have to perform the subtraction right away to get the interval between the current value and some reference point,
    • to print the absolute value, we would have to add the quantity to the reference point that we need to store somewhere in the database as well,
    • to get a relative quantity, only the currently stored one is immediate; all other values will require at least one quantity addition operation.

    Now, let's assume that Bob, a friend of Alice, also keeps a log of his trips but he, of course, measures distances from his own home with the odometer in his car. Everything is fine as long as we deal with one trip at a time, but if we start to work with both at once, we may accidentally subtract points from different trips. The library will not prevent us from doing so.

    The points from Alice's and Bob's trips should be considered separate, and to enforce it at compilation time, we need to introduce explicit origins.

    "},{"location":"users_guide/framework_basics/the_affine_space/#absolute-point-origin","title":"Absolute Point origin","text":"

    The absolute point origin specifies the \"zero\" of our measurement's scale. User can specify such an origin by deriving from the absolute_point_origin class template:

    enum class actor { alice, bob };\n\ntemplate<actor A>\nstruct zeroth_odometer_t : absolute_point_origin<zeroth_odometer_t<A>, isq::distance> {};\n\ntemplate<actor A>\ninline constexpr zeroth_odometer_t<A> zeroth_odometer;\n

    Info

    The absolute_point_origin class template uses the CRTP idiom to enforce the uniqueness of such a type. You should pass the type of a derived class as the first argument of the template instantiation.

    Note

    Unfortunately, due to inconsistencies in C++ language rules:

    • we can't define the above in one line of code,
    • provide the same identifier for a class and variable template.

    Odometer is not the only one that can get an explicit point origin in our case. As timestamps are provided by the std::chrono::system_clock, their values are always relative to the epoch of this clock.

    Note

    The mp-units library provides means to specify interoperability with other units libraries. It also has built-in compatibility with std::chrono types, so users do not have to define interoperability traits or point origins for such types by themselves. Those are already provided in the mp-units/systems/si/chrono.h header file.

    Now, we can refactor our database to benefit from the explicit points:

    template<actor A>\nstruct trip_log_item {\n  std::string point_name;\n  quantity_point<si::kilo<si::metre>, zeroth_odometer<A>> odometer;\n  quantity_point<si::second, chrono_point_origin<system_clock>> timestamp;\n};\n\ntemplate<actor A>\nusing trip_log = std::vector<trip_log_item<A>>;\n

    We also need to update the initialization part in our code. In the case of implicit zeroth origins, we could construct quantity_point directly from the value of a quantity. This is no longer the case. As a Point can be represented with a Vector from the origin, to improve the safety of the code we write, a quantity_point class template must be created with one of the following operations:

    quantity_point qp1 = zeroth_odometer<actor::alice> + 1356 * km;\nquantity_point qp2 = 1356 * km + zeroth_odometer<actor::alice>;\nquantity_point qp3 = zeroth_odometer<actor::alice> - 1356 * km;\n

    Although, the qp3 above does not have a physical sense in this specific scenario.

    Note

    It is not allowed to subtract a Point from a Vector thus 1356 * km - zeroth_odometer<actor::alice> is an invalid operation.

    Info

    A rationale for this longer construction syntax can be found in the Why can't I create a quantity by passing a number to a constructor? chapter.

    Similarly to creation of a quantity, if someone does not like the operator-based syntax to create a quantity_point, the same results can be achieved with a two-parameter constructor:

    quantity_point qp4{1356 * km, zeroth_odometer<actor::alice>};\n

    Also, as now our timestamps have a proper point origin provided in a type, we can simplify the previous code by directly converting std::chrono::time_point to our quantity_point type.

    With all the above, we can refactor our initialization part to the following:

    trip_log<actor::alice> alice_log;\n\nalice_log.emplace_back(\"home\", zeroth_odometer<actor::alice> + 1356 * km, system_clock::now());\n\n// some time passes\n\nalice_log.emplace_back(\"castle\", zeroth_odometer<actor::alice> + 1401 * km, system_clock::now());\n
    "},{"location":"users_guide/framework_basics/the_affine_space/#point-arithmetics","title":"Point arithmetics","text":"

    As another example, let's assume we will attend the CppCon conference hosted in Aurora, CO, and we want to estimate the distance we will travel. We have to take a taxi to a local airport, fly to DEN airport with a stopover in FRA, and, in the end, get a cab to the Gaylord Rockies Resort & Convention Center:

    constexpr struct home : absolute_point_origin<home, isq::distance> {} home;\n\nquantity_point<isq::distance[km], home> home_airport = home + 15 * km;\nquantity_point<isq::distance[km], home> fra_airport = home_airport + 829 * km;\nquantity_point<isq::distance[km], home> den_airport = fra_airport + 8115 * km;\nquantity_point<isq::distance[km], home> cppcon_venue = den_airport + 10.1 * mi;\n

    As we can see above, we can easily get a new point by adding a quantity to an origin or another quantity point.

    If we want to find out the distance traveled between two points, we simply subtract them:

    quantity<isq::distance[km]> total = cppcon_venue - home;\nquantity<isq::distance[km]> flight = den_airport - home_airport;\n

    If we would like to find out the total distance traveled by taxi as well, we have to do a bit more calculations:

    quantity<isq::distance[km]> taxi1 = home_airport - home;\nquantity<isq::distance[km]> taxi2 = cppcon_venue - den_airport;\nquantity<isq::distance[km]> taxi = taxi1 + taxi2;\n

    Now, if we print the results:

    std::cout << \"Total distance:  \" << total << \"\\n\";\nstd::cout << \"Flight distance: \" << flight << \"\\n\";\nstd::cout << \"Taxi distance:   \" << taxi << \"\\n\";\n

    we will see the following output:

    Total distance:  8975.25 km\nFlight distance: 8944 km\nTaxi distance:   31.2544 km\n

    Note

    It is not allowed to subtract two point origins defined in terms of absolute_point_origin (e.g., home - home) as those do not contain information about the unit, so we are not able to determine a resulting quantity type.

    "},{"location":"users_guide/framework_basics/the_affine_space/#relative-point-origin","title":"Relative Point origin","text":"

    We often do not have only one ultimate \"zero\" point when we measure things.

    For example, let's assume that we have the following absolute point origin:

    constexpr struct mean_sea_level : absolute_point_origin<mean_sea_level, isq::altitude> {} mean_sea_level;\n

    If we want to model a trip to Mount Everest, measuring all daily hikes from the mean_sea_level might not be efficient. We may know that we are not good climbers, so all our climbs can be represented with an 8-bit integer type, allowing us to save memory in our database of climbs.

    For this purpose, we can define a relative_point_origin in the following way:

    constexpr struct everest_base_camp : relative_point_origin<mean_sea_level + 5364 * m> {} everest_base_camp;\n

    The above can be used as an origin for subsequent Points:

    constexpr quantity_point first_climb_alt = everest_base_camp + isq::altitude(std::uint8_t{42} * m);\nstatic_assert(first_climb_alt.quantity_from(everest_base_camp) == 42 * m);\nstatic_assert(first_climb_alt.quantity_from(mean_sea_level) == 5406 * m);\nstatic_assert(first_climb_alt.quantity_from_zero() == 5406 * m);\n

    As we can see above, the quantity_from() member function returns a relative distance from the provided point origin while the quantity_from_zero() returns the distance from the absolute point origin.

    "},{"location":"users_guide/framework_basics/the_affine_space/#converting-between-different-representations-of-the-same-point","title":"Converting between different representations of the same Point","text":"

    As we might represent the same Point with Vectors from various origins, the mp-units library provides facilities to convert the Point to quantity_point class templates expressed in terms of origins relative to each other in the type system.

    For this purpose, we can use:

    • a converting constructor:

      constexpr quantity_point<isq::altitude[m], mean_sea_level, int> qp = first_climb_alt;\nstatic_assert(qp.quantity_ref_from(qp.point_origin) == 5406 * m);\n
    • a dedicated conversion interface:

      constexpr quantity_point qp = first_climb_alt.point_for(mean_sea_level);\nstatic_assert(qp.quantity_ref_from(qp.point_origin) == 5406 * m);\n

    Note

    It is only allowed to convert between various origins defined in terms of the same absolute_point_origin. Even if it is theoretically possible to express the same Point as a Vector from another absolute_point_origin, the library will not allow such a conversion. A custom user-defined conversion function will be needed to add this functionality.

    Said otherwise, in the mp-units library, there is no way to spell how two distinct absolute_point_origin types relate to each other.

    "},{"location":"users_guide/framework_basics/the_affine_space/#temperature-support","title":"Temperature support","text":"

    Another important example of relative point origins is the support of temperature quantity points. The mp-units library provides a few predefined point origins for this purpose:

    namespace si {\n\ninline constexpr struct absolute_zero : absolute_point_origin<absolute_zero, isq::thermodynamic_temperature> {} absolute_zero;\ninline constexpr struct zeroth_kelvin : decltype(absolute_zero) {} zeroth_kelvin;\n\ninline constexpr struct ice_point : relative_point_origin<quantity_point{273'150 * milli<kelvin>}> {} ice_point;\ninline constexpr struct zeroth_degree_Celsius : decltype(ice_point) {} zeroth_degree_Celsius;\n\n}\n\nnamespace usc {\n\ninline constexpr struct zeroth_degree_Fahrenheit :\n  relative_point_origin<si::zeroth_degree_Celsius - 32 * (mag<ratio{5, 9}> * si::degree_Celsius)> {} zeroth_degree_Fahrenheit;\n\n}\n

    The above is a great example of how point origins can be stacked on top of each other:

    • usc::zeroth_degree_Fahrenheit is defined relative to si::zeroth_degree_Celsius
    • si::zeroth_degree_Celsius is defined relative to si::zeroth_kelvin.

    Note

    Notice that while stacking point origins, we can use not only different representation types but also different units for origins and a Point. In the above example, the relative point origin for degree Celsius is defined in terms of si::kelvin, while the quantity point for it will use si::degree_Celsius as a unit.

    The temperature point origins defined above are provided explicitly in the respective units' definitions:

    namespace si {\n\ninline constexpr struct kelvin :\n    named_unit<\"K\", kind_of<isq::thermodynamic_temperature>, zeroth_kelvin> {} kelvin;\ninline constexpr struct degree_Celsius :\n    named_unit<{u8\"\u00b0C\", \"`C\"}, kelvin, zeroth_degree_Celsius> {} degree_Celsius;\n\n}\n\nnamespace usc {\n\ninline constexpr struct degree_Fahrenheit :\n    named_unit<{u8\"\u00b0F\", \"`F\"}, mag<ratio{5, 9}> * si::degree_Celsius, zeroth_degree_Fahrenheit> {} degree_Fahrenheit;\n\n}\n

    Now let's see how we can benefit from the above definitions. We have quite a few alternatives to choose from here. Depending on our needs or taste we can:

    • be explicit about the unit and origin:

      quantity_point<si::degree_Celsius, si::zeroth_degree_Celsius> q1 = si::zeroth_degree_Celsius + 20.5 * deg_C;\nquantity_point<si::degree_Celsius, si::zeroth_degree_Celsius> q2 = {20.5 * deg_C, si::zeroth_degree_Celsius};\nquantity_point<si::degree_Celsius, si::zeroth_degree_Celsius> q3{20.5 * deg_C};\n
    • specify a unit and use its zeroth point origin implicitly:

      quantity_point<si::degree_Celsius> q4 = si::zeroth_degree_Celsius + 20.5 * deg_C;\nquantity_point<si::degree_Celsius> q5 = {20.5 * deg_C, si::zeroth_degree_Celsius};\nquantity_point<si::degree_Celsius> q6{20.5 * deg_C};\n
    • benefit from CTAD:

      quantity_point q7 = si::zeroth_degree_Celsius + 20.5 * deg_C;\nquantity_point q8 = {20.5 * deg_C, si::zeroth_degree_Celsius};\nquantity_point q9{20.5 * deg_C};\n

    In all of the above cases, we end up with the quantity_point of the same type and value.

    To play a bit more with temperatures, we can implement a simple room AC temperature controller in the following way:

    constexpr struct room_reference_temp : relative_point_origin<quantity_point{21 * deg_C}> {} room_reference_temp;\nusing room_temp = quantity_point<isq::Celsius_temperature[deg_C], room_reference_temp>;\n\nconstexpr auto step_delta = isq::Celsius_temperature(0.5 * deg_C);\nconstexpr int number_of_steps = 6;\n\nroom_temp room_ref{};\nroom_temp room_low = room_ref - number_of_steps * step_delta;\nroom_temp room_high = room_ref + number_of_steps * step_delta;\n\nstd::println(\"Room reference temperature: {} ({}, {})\\n\",\n             room_ref.quantity_from_zero(),\n             room_ref.in(usc::degree_Fahrenheit).quantity_from_zero(),\n             room_ref.in(si::kelvin).quantity_from_zero());\n\nstd::println(\"| {:<14} | {:^18} | {:^18} | {:^18} |\",\n             \"Temperature\", \"Room reference\", \"Ice point\", \"Absolute zero\");\nstd::println(\"|{0:=^16}|{0:=^20}|{0:=^20}|{0:=^20}|\", \"\");\n\nauto print = [&](std::string_view label, auto v) {\n  std::println(\"| {:<14} | {:^18} | {:^18} | {:^18{%N:.2f} %U} |\", label,\n               v - room_reference_temp, (v - si::ice_point).in(deg_C), (v - si::absolute_zero).in(deg_C));\n};\n\nprint(\"Lowest\", room_low);\nprint(\"Default\", room_ref);\nprint(\"Highest\", room_high);\n

    The above prints:

    Room reference temperature: 21 \u00b0C (69.8 \u00b0F, 294.15 K)\n\n| Temperature    |   Room reference   |     Ice point      |   Absolute zero    |\n|================|====================|====================|====================|\n| Lowest         |       -3 \u00b0C        |       18 \u00b0C        |     291.15 \u00b0C      |\n| Default        |        0 \u00b0C        |       21 \u00b0C        |     294.15 \u00b0C      |\n| Highest        |        3 \u00b0C        |       24 \u00b0C        |     297.15 \u00b0C      |\n
    "},{"location":"users_guide/framework_basics/the_affine_space/#no-text-output-for-points","title":"No text output for Points","text":"

    The library does not provide a text output for quantity points, as printing just a number and a unit is not enough to adequately describe a quantity point. Often, an additional prefix or postfix is required.

    For example, the text output of 42 m may mean many things and can also be confused with an output of a regular quantity. On the other hand, printing 42 m AMSL for altitudes above mean sea level is a much better solution, but the library does not have enough information to print it that way by itself.

    "},{"location":"users_guide/framework_basics/the_affine_space/#the-affine-space-is-about-type-safety","title":"The affine space is about type-safety","text":"

    The following operations are not allowed in the affine space:

    • adding two quantity_point objects
      • It is physically impossible to add positions of home and Denver airports.
    • subtracting a quantity_point from a quantity
      • What would it mean to subtract the DEN airport location from the distance to it?
    • multiplying/dividing a quantity_point with a scalar
      • What is the position of 2 * DEN airport location?
    • multiplying/dividing a quantity_point with a quantity
      • What would multiplying the distance with the DEN airport location mean?
    • multiplying/dividing two quantity_point objects
      • What would multiplying home and DEN airport location mean?
    • mixing quantity_points of different quantity kinds
      • It is physically impossible to subtract time from length.
    • mixing quantity_points of inconvertible quantities
      • What does subtracting a distance point to DEN airport from the Mount Everest base camp altitude mean?
    • mixing quantity_points of convertible quantities but with unrelated origins
      • How do we subtract a point on our trip to CppCon measured relatively to our home location from a point measured relative to the center of the Solar System?

    Important: The affine space improves safety

    The usage of quantity_point and affine space types, in general, improves expressiveness and type-safety of the code we write.

    "},{"location":"users_guide/framework_basics/value_conversions/","title":"Value Conversions","text":""},{"location":"users_guide/framework_basics/value_conversions/#value-preserving-conversions","title":"Value-preserving conversions","text":"
    auto q1 = 5 * km;\nstd::cout << q1.in(m) << '\\n';\nquantity<si::metre, int> q2 = q1;\n

    The second line above converts the current quantity to the one expressed in meters and prints its contents. The third line converts the quantity expressed in kilometers into the one measured in meters.

    In case a user would like to perform an opposite transformation:

    auto q1 = 5 * m;\nstd::cout << q1.in(km) << '\\n';\nquantity<si::kilo<si::metre>, int> q2 = q1;\n

    Both conversions will fail to compile.

    There are two ways to make the above work. The first solution is to use a floating-point representation type:

    auto q1 = 5. * m;\nstd::cout << q1.in(km) << '\\n';\nquantity<si::kilo<si::metre>> q2 = q1;\n

    or

    auto q1 = 5 * m;\nstd::cout << value_cast<double>(q1).in(km) << '\\n';\nquantity<si::kilo<si::metre>> q2 = q1;  // double by default\n

    Important

    The mp-units library follows std::chrono::duration logic and treats floating-point types as value-preserving.

    "},{"location":"users_guide/framework_basics/value_conversions/#value-truncating-conversions","title":"Value-truncating conversions","text":"

    The second solution is to force a truncating conversion:

    auto q1 = 5 * m;\nstd::cout << value_cast<km>(q1) << '\\n';\nquantity<si::kilo<si::metre>, int> q2 = q1.force_in(km);\n

    This explicit cast makes it clear that something unsafe is going on. It is easy to spot in code reviews or while chasing a bug in the source code.

    Note

    q.force_in(U) is just a shortcut to run value_cast<U>(q). There is no difference in behavior between those two interfaces. q.force_in(U) was added for consistency with q.in(U) and q.force_numerical_value_in(U).

    Another place where this cast is useful is when a user wants to convert a quantity with a floating-point representation to the one using an integral one. Again, this is a truncating conversion, so an explicit cast is needed:

    quantity<si::metre, int> q3 = value_cast<int>(3.14 * m);\n

    Info

    It is often OK to use an integral as a representation type, but in general, floating-point types provide better precision and are privileged in the library as they are considered to be value-preserving.

    In some cases, a unit and a representation type should be changed simultaneously. Moreover, sometimes, the order of doing those operations matters. In such cases, the library provides the value_cast<U, Rep>(q) which always returns the most precise result:

    C++23C++20Portable
    inline constexpr struct dim_currency : base_dimension<\"$\"> {} dim_currency;\ninline constexpr struct currency : quantity_spec<dim_currency> {} currency;\n\ninline constexpr struct us_dollar : named_unit<\"USD\", kind_of<currency>> {} us_dollar;\ninline constexpr struct scaled_us_dollar : named_unit<\"USD_s\", mag_power<10, -8> * us_dollar> {} scaled_us_dollar;\n\nnamespace unit_symbols {\n\ninline constexpr auto USD = us_dollar;\ninline constexpr auto USD_s = scaled_us_dollar;\n\n}  // namespace unit_symbols\n\nusing Price = quantity_point<currency[us_dollar]>;\nusing Scaled = quantity_point<currency[scaled_us_dollar], zeroth_point_origin<currency>, std::int64_t>;\n\nPrice price{12.95 * USD};\nScaled spx = value_cast<USD_s, std::int64_t>(price);\n
    inline constexpr struct dim_currency : base_dimension<\"$\"> {} dim_currency;\ninline constexpr struct currency : quantity_spec<currency, dim_currency> {} currency;\n\ninline constexpr struct us_dollar : named_unit<\"USD\", kind_of<currency>> {} us_dollar;\ninline constexpr struct scaled_us_dollar : named_unit<\"USD_s\", mag_power<10, -8> * us_dollar> {} scaled_us_dollar;\n\nnamespace unit_symbols {\n\ninline constexpr auto USD = us_dollar;\ninline constexpr auto USD_s = scaled_us_dollar;\n\n}  // namespace unit_symbols\n\nusing Price = quantity_point<currency[us_dollar]>;\nusing Scaled = quantity_point<currency[scaled_us_dollar], zeroth_point_origin<currency>, std::int64_t>;\n\nPrice price{12.95 * USD};\nScaled spx = value_cast<USD_s, std::int64_t>(price);\n
    inline constexpr struct dim_currency : base_dimension<\"$\"> {} dim_currency;\nQUANTITY_SPEC(currency, dim_currency);\n\ninline constexpr struct us_dollar : named_unit<\"USD\", kind_of<currency>> {} us_dollar;\ninline constexpr struct scaled_us_dollar : named_unit<\"USD_s\", mag_power<10, -8> * us_dollar> {} scaled_us_dollar;\n\nnamespace unit_symbols {\n\ninline constexpr auto USD = us_dollar;\ninline constexpr auto USD_s = scaled_us_dollar;\n\n}  // namespace unit_symbols\n\nusing Price = quantity_point<currency[us_dollar]>;\nusing Scaled = quantity_point<currency[scaled_us_dollar], zeroth_point_origin<currency>, std::int64_t>;\n\nPrice price{12.95 * USD};\nScaled spx = value_cast<USD_s, std::int64_t>(price);\n
    "},{"location":"users_guide/use_cases/interoperability_with_other_libraries/","title":"Interoperability with Other Libraries","text":"

    mp-units makes it easy to cooperate with similar entities of other libraries. No matter if we want to provide interoperability with a simple home-grown strongly typed wrapper type (e.g., Meter, Timestamp, ...) or with a feature-rich quantities and units library, we have to provide specializations of:

    • a quantity_like_traits for external quantity-like type,
    • a quantity_point_like_traits for external quantity_point-like type.
    "},{"location":"users_guide/use_cases/interoperability_with_other_libraries/#specifying-a-conversion-kind","title":"Specifying a conversion kind","text":"

    Before we delve into the template specialization details, let's first decide if we want the conversions to happen implicitly or if explicit ones would be a better choice. Or maybe the conversion should be implicit in one direction only (e.g., into mp-units abstractions) while the explicit conversions in the other direction should be preferred?

    There is no one unified answer to the above questions. Everything depends on the use case.

    Typically, the implicit conversions are allowed in cases where:

    • both abstractions mean exactly the same, and interchanging them in the code should not change its logic,
    • there is no significant runtime overhead introduced by such a conversion (e.g., no need for dynamic allocation or copying of huge internal buffers),
    • the target type of the conversion provides the same or better safety to the users,
    • we prefer the simplicity of implicit conversions over safety during the (hopefully short) transition period of refactoring our code base from the usage of one library to the other.

    In all other scenarios, we should probably enforce explicit conversions.

    The kinds of inter-library conversions can be easily configured in partial specializations of conversion traits in the mp-units library. To require an explicit conversion, the return type of the conversion function should be wrapped in convert_explicitly<T>. Otherwise, convert_implicitly<T> should be used.

    "},{"location":"users_guide/use_cases/interoperability_with_other_libraries/#quantities-conversions","title":"Quantities conversions","text":"

    For example, let's assume that some company has its own Meter strong-type wrapper:

    struct Meter {\n  int value;\n};\n

    As every usage of Meter is at least as good and safe as the usage of quantity<si::metre, int>, and as there is no significant runtime performance penalty, we would like to allow the conversion to mp_units::quantity to happen implicitly.

    On the other hand, the quantity type is much safer than the Meter, and that is why we would prefer to see the opposite conversions stated explicitly in our code.

    To enable such interoperability, we must define a partial specialization of the quantity_like_traits<T> type trait. Such specialization should provide:

    • static data member reference that provides the quantity reference (e.g., unit),
    • rep type that specifies the underlying storage type,
    • to_numerical_value(T) static member function returning a quantity's raw value of rep type packed in either convert_explicitly or convert_implicitly wrapper.
    • from_numerical_value(rep) static member function returning T packed in either convert_explicitly or convert_implicitly wrapper.

    For example, for our Meter type, we could provide the following:

    template<>\nstruct mp_units::quantity_like_traits<Meter> {\n  static constexpr auto reference = si::metre;\n  using rep = decltype(Meter::value);\n  static constexpr convert_implicitly<rep> to_numerical_value(Meter m) { return m.value; }\n  static constexpr convert_explicitly<Meter> from_numerical_value(rep v) { return Meter{v}; }\n};\n

    After that, we can check that the QuantityLike concept is satisfied:

    static_assert(mp_units::QuantityLike<Meter>);\n

    and we can write the following:

    void print(Meter m) { std::cout << m.value << \" m\\n\"; }\n\nint main()\n{\n  using namespace mp_units;\n  using namespace mp_units::si::unit_symbols;\n\n  Meter height{42};\n\n  // implicit conversions\n  quantity h1 = height;\n  quantity<isq::height[m], int> h2 = height;\n\n  std::cout << h1 << \"\\n\";\n  std::cout << h2 << \"\\n\";\n\n  // explicit conversions\n  print(Meter(h1));\n  print(Meter(h2));\n}\n

    Note

    No matter if we decide to use implicit or explicit conversions, the mp-units will not allow unsafe operations to happen.

    If we extend the above example with unsafe conversions, the code will not compile, and we will have to fix the issues first before the conversion may be performed:

    UnsafeFixed
    quantity<isq::height[m]> h3 = height;\nquantity<isq::height[mm], int> h4 = height;\nquantity<isq::height[km], int> h5 = height;  // Compile-time error (1)\n\nstd::cout << h3 << \"\\n\";\nstd::cout << h4 << \"\\n\";\nstd::cout << h5 << \"\\n\";\n\nprint(Meter(h3));                            // Compile-time error (2)\nprint(Meter(h4));                            // Compile-time error (3)\nprint(Meter(h5));\n
    1. Truncation of value while converting from meters to kilometers.
    2. Conversion of double to int is not value-preserving.
    3. Truncation of value while converting from millimeters to meters.
    quantity<isq::height[m]> h3 = height;\nquantity<isq::height[mm], int> h4 = height;\nquantity<isq::height[km], int> h5 = quantity{height}.force_in(km);\n\nstd::cout << h3 << \"\\n\";\nstd::cout << h4 << \"\\n\";\nstd::cout << h5 << \"\\n\";\n\nprint(Meter(value_cast<int>(h3)));\nprint(Meter(h4.force_in(m)));\nprint(Meter(h5));\n
    "},{"location":"users_guide/use_cases/interoperability_with_other_libraries/#quantity-points-conversions","title":"Quantity points conversions","text":"

    To play with quantity point conversions, let's assume that we have a Timestamp strong type in our codebase, and we would like to start using mp-units to work with this abstraction.

    struct Timestamp {\n  int seconds;\n};\n

    As we described in The Affine Space chapter, timestamps should be modeled as quantity points rather than regular quantities.

    To allow the conversion between our custom Timestamp type and the quantity_point class template we need to provide the following in the partial specialization of the quantity_point_like_traits<T> type trait:

    • static data member reference that provides the quantity point reference (e.g., unit),
    • static data member point_origin that specifies the absolute point, which is the beginning of our measurement scale for our points,
    • rep type that specifies the underlying storage type,
    • to_quantity(T) static member function returning the quantity being the offset of the point from the origin packed in either convert_explicitly or convert_implicitly wrapper,
    • from_quantity(quantity<reference, rep>) static member function returning T packed in either convert_explicitly or convert_implicitly wrapper.

    For example, for our Timestamp type, we could provide the following:

    template<>\nstruct mp_units::quantity_point_like_traits<Timestamp> {\n  static constexpr auto reference = si::second;\n  static constexpr auto point_origin = default_point_origin(reference);\n  using rep = decltype(Timestamp::seconds);\n\n  static constexpr convert_implicitly<quantity<reference, rep>> to_quantity(Timestamp ts)\n  {\n    return ts.seconds * si::second;\n  }\n\n  static constexpr convert_explicitly<Timestamp> from_quantity(quantity<reference, rep> q)\n  {\n    return Timestamp(q.numerical_value_ref_in(si::second));\n  }\n};\n

    After that, we can check that the QuantityPointLike concept is satisfied:

    static_assert(mp_units::QuantityPointLike<Timestamp>);\n

    and we can write the following:

    void print(Timestamp ts) { std::cout << ts.seconds << \" s\\n\"; }\n\nint main()\n{\n  using namespace mp_units;\n  using namespace mp_units::si::unit_symbols;\n\n  Timestamp ts{42};\n\n  // implicit conversion\n  quantity_point qp = ts;\n\n  std::cout << qp.quantity_from_zero() << \"\\n\";\n\n  // explicit conversion\n  print(Timestamp(qp));\n}\n
    "},{"location":"users_guide/use_cases/interoperability_with_other_libraries/#interoperability-with-the-c-standard-library","title":"Interoperability with the C++ Standard Library","text":"

    In the C++ standard library, we have two types that handle quantities and model the affine space. Those are:

    • std::chrono::duration - specifies quantities of time,
    • std::chrono::time_point - specifies quantity points of time.

    The mp-units library comes with built-in interoperability with those types. It is enough to include the mp-units/systems/si/chrono.h file to benefit from it. This file provides:

    • partial specializations of quantity_like_traits and quantity_point_like_traits that provide support for implicit conversions between std and mp_units types in both directions,
    • chrono_point_origin<Clock> point origin for std clocks,
    • to_chrono_duration and to_chrono_time_point dedicated conversion functions that result in types exactly representing mp-units abstractions.

    Important

    Only a quantity_point that uses chrono_point_origin<Clock> as its origin can be converted to the std::chrono abstractions:

    inline constexpr struct ts_origin : relative_point_origin<chrono_point_origin<system_clock> + 1 * h> {} ts_origin;\ninline constexpr struct my_origin : absolute_point_origin<my_origin, isq::time> {} my_origin;\n\nquantity_point qp1 = sys_seconds{1s};\nauto tp1 = to_chrono_time_point(qp1);  // OK\n\nquantity_point qp2 = chrono_point_origin<system_clock> + 1 * s;\nauto tp2 = to_chrono_time_point(qp2);  // OK\n\nquantity_point qp3 = ts_origin + 1 * s;\nauto tp3 = to_chrono_time_point(qp3);  // OK\n\nquantity_point qp4 = my_origin + 1 * s;\nauto tp4 = to_chrono_time_point(qp4);  // Compile-time Error (1)\n\nquantity_point qp5{1 * s};\nauto tp5 = to_chrono_time_point(qp5);  // Compile-time Error (2)\n
    1. my_origin is not defined in terms of chrono_point_origin<Clock>.
    2. zeroth_point_origin is not defined in terms of chrono_point_origin<Clock>.

    Here is an example of how interoperability described in this chapter can be used in practice:

    using namespace std::chrono;\n\nsys_seconds ts_now = floor<seconds>(system_clock::now());\n\nquantity_point start_time = ts_now;\nquantity speed = 925. * km / h;\nquantity distance = 8111. * km;\nquantity flight_time = distance / speed;\nquantity_point exp_end_time = start_time + flight_time;\n\nsys_seconds ts_end = value_cast<int>(exp_end_time.in(s));\n\nauto curr_time = zoned_time(current_zone(), ts_now);\nauto mst_time = zoned_time(\"America/Denver\", ts_end);\n\nstd::cout << \"Takeoff: \" << curr_time << \"\\n\";\nstd::cout << \"Landing: \" << mst_time << \"\\n\";\n

    The above may print the following output:

    Takeoff: 2023-11-18 13:20:54 UTC\nLanding: 2023-11-18 15:07:01 MST\n
    "},{"location":"users_guide/use_cases/wide_compatibility/","title":"Wide Compatibility","text":"

    The mp-units allows us to implement nice and terse code targeting a specific C++ version and configuration. Such code is easy to write and understand but might not be portable to some older environments.

    However, sometimes, we want to develop code that can be compiled on a wide range of various compilers and configurations. This is why the library also exposes and uses special preprocessor macros that can be used to ensure the wide compatibility of our code.

    Note

    Those macros are used in our short example applications as those are meant to be built on all of the supported compilers. Some still do not support std::format, C++ modules, or C++ versions newer than C++20.

    "},{"location":"users_guide/use_cases/wide_compatibility/#various-compatibility-options","title":"Various compatibility options","text":"

    Depending on your compiler's conformance, you can choose to use any of the below styles to write your code using mp-units:

    C++23C++20C++20 with header filesC++20 with header files + libfmtWide Compatibility
    #include <format>\n#include <iostream>\nimport mp_units;\n\n// ...\n\ninline constexpr struct horizontal_length : quantity_spec<isq::length> {} horizontal_length;\n\n// ...\n\nstd::cout << std::format(...) << \"\\n\";\n
    #include <format>\n#include <iostream>\nimport mp_units;\n\n// ...\n\ninline constexpr struct horizontal_length : quantity_spec<horizontal_length, isq::length> {} horizontal_length;\n\n// ...\n\nstd::cout << std::format(...) << \"\\n\";\n
    #include <mp-units/format.h>\n#include <mp-units/ostream.h>\n#include <mp-units/systems/international/international.h>\n#include <mp-units/systems/isq/isq.h>\n#include <mp-units/systems/si/si.h>\n#include <format>\n#include <iostream>\n\n// ...\n\ninline constexpr struct horizontal_length : quantity_spec<horizontal_length, isq::length> {} horizontal_length;\n\n// ...\n\nstd::cout << std::format(...) << \"\\n\";\n
    #include <mp-units/format.h>\n#include <mp-units/ostream.h>\n#include <mp-units/systems/international/international.h>\n#include <mp-units/systems/isq/isq.h>\n#include <mp-units/systems/si/si.h>\n#include <fmt/format.h>\n#include <iostream>\n\n// ...\n\ninline constexpr struct horizontal_length : quantity_spec<horizontal_length, isq::length> {} horizontal_length;\n\n// ...\n\nstd::cout << fmt::format(...) << \"\\n\";\n
    #include <iostream>\n#ifdef MP_UNITS_MODULES\n#include <mp-units/compat_macros.h>\nimport mp_units;\n#else\n#include <mp-units/format.h>\n#include <mp-units/ostream.h>\n#include <mp-units/systems/international/international.h>\n#include <mp-units/systems/isq/isq.h>\n#include <mp-units/systems/si/si.h>\n#endif\n\n// ...\n\nQUANTITY_SPEC(horizontal_length, isq::length);\n\n// ...\n\nstd::cout << MP_UNITS_STD_FMT::format(...) << \"\\n\";\n

    Tip

    Depending on your preferences, you can either write:

    • terse code directly targeting your specific compiler's abilities,
    • verbose code using preprocessor branches and macros that provide the widest compatibility across various compilers.
    "},{"location":"users_guide/use_cases/wide_compatibility/#compatibility-macros","title":"Compatibility macros","text":"

    This chapter describes only the most essential tools the mp-units users need. All the compatibility macros can be found in the mp-units/compat_macros.h header file.

    Tip

    The mp-units/compat_macros.h header file is implicitly included when we use \"legacy\" headers in our translation units. However, it has to be explicitly included when we use C++20 modules, as those do not propagate preprocessor macros.

    "},{"location":"users_guide/use_cases/wide_compatibility/#QUANTITY_SPEC","title":"QUANTITY_SPEC(name, ...)","text":"

    Quantity specification definitions benefit from an explicit object parameter added in C++23 to remove the need for CRTP idiom, which significantly simplifies the code.

    This macro benefits from the new C++23 feature if available. Otherwise, it uses the CRTP idiom under the hood.

    "},{"location":"users_guide/use_cases/wide_compatibility/#mp_units_std_fmt","title":"MP_UNITS_STD_FMT","text":"

    Some of the supported compilers do not support std::format and related tools. Also, despite using a conformant compiler, some projects still choose to use fmtlib as their primary formatting facility (e.g., to benefit from additional features provided with the library).

    This macro resolves to either the std or fmt namespace, depending on the value of MP_UNITS_USE_FMTLIB CMake option.

    "},{"location":"users_guide/use_cases/working_with_legacy_interfaces/","title":"Working with Legacy interfaces","text":"

    In case we are working with a legacy/unsafe interface, we may need to extract the numerical value of a quantity and pass it to some third-party legacy unsafe interfaces.

    In such situations we can use .numerical_value_in(Unit) member function:

    void legacy_check_speed_limit(int speed_in_km_per_h);\n
    legacy_check_speed_limit((180 * km / (2 * h)).numerical_value_in(km / h));\n

    Such a getter will explicitly enforce the usage of a correct unit required by the underlying interface, which reduces a significant number of safety-related issues.

    The above code will not compile in case value truncation may happen. To solve the issue, we need to either use a value-preserving representation type or force the truncating conversion with .force_numerical_value_in(Unit):

    legacy_check_speed_limit((140 * mi / (2 * h)).force_numerical_value_in(km / h));\n

    The getters mentioned above always return by value as a quantity value conversion may be required to adjust it to the target unit. In case a user needs a reference to the underlying storage .numerical_value_ref_in(Unit) should be used:

    void legacy_set_speed_limit(int* speed_in_km_per_h) { *speed_in_km_per_h = 100; }\n
    quantity<km / h, int> speed_limit;\nlegacy_set_speed_limit(&speed_limit.numerical_value_ref_in(km / h));\n

    This member function again requires a target unit to enforce safety. This overload does not participate in overload resolution if the provided unit has a different scaling factor than the current one.

    "},{"location":"blog/archive/2023/","title":"2023","text":""},{"location":"blog/category/releases/","title":"Releases","text":""},{"location":"blog/category/wg21/","title":"WG21","text":""},{"location":"users_guide/examples/tags_index/","title":"Tags Index","text":"

    Note

    mp-units usage example applications are meant to be built on all of the supported compilers. This is why they benefit from the Wide Compatibility mode.

    Tip

    All usage examples in this chapter are categorized with appropriate tags to simplify navigation and search of relevant code. You can either read all the examples one-by-one in the order provided by the documentation authors or, thanks to the tagging system, jump straight to the example that is the most interesting for you.

    "},{"location":"users_guide/examples/tags_index/#cgs-system","title":"CGS System","text":"
    • avg_speed
    "},{"location":"users_guide/examples/tags_index/#international-system","title":"International System","text":"
    • avg_speed
    • hello_units
    "},{"location":"users_guide/examples/tags_index/#physical-constants","title":"Physical Constants","text":"
    • si_constants
    "},{"location":"users_guide/examples/tags_index/#text-formatting","title":"Text Formatting","text":"
    • avg_speed
    • hello_units
    • si_constants
    "}]} \ No newline at end of file diff --git a/2.2/sitemap.xml.gz b/2.2/sitemap.xml.gz index 1d476160e..f4f6c702f 100644 Binary files a/2.2/sitemap.xml.gz and b/2.2/sitemap.xml.gz differ