diff --git a/docs/reference/core/concepts.rst b/docs/reference/core/concepts.rst index 103d1b41..ea22df6b 100644 --- a/docs/reference/core/concepts.rst +++ b/docs/reference/core/concepts.rst @@ -65,14 +65,15 @@ Concepts .. concept:: template QuantityValue - A concept matching non-Quantity types. Satisfied by types that match - :expr:`(!Quantity) && (!WrappedQuantity) && std::regular` and satisfy one of the - following: + A concept matching types that can be used as a `Quantity` representation type. Satisfied + by types that match :expr:`(!Quantity) && (!WrappedQuantity) && std::regular` and + satisfy one of the following: - - if type :expr:`T` is constructible from ``std::int64_t`` (which is the type that stores - the elements of `ratio`), :expr:`T * T` and :expr:`T / T` must be valid, - - otherwise, :expr:`T * std::int64_t`, :expr:`std::int64_t * T`, and :expr:`T / std::int64_t` - must be valid. + - if :expr:`common_type_with` is ``true``, then :expr:`std::common_type_t` + must at least provide binary multiplication and division operators, + - otherwise, :expr:`T::value_type` must be valid, :expr:`common_type_with` be + ``true``, and :expr:`std::common_type_t` must at least provide binary + multiplication and division operators with itself and ``T``. .. concept:: template QuantityPoint diff --git a/docs/use_cases/custom_representation_types.rst b/docs/use_cases/custom_representation_types.rst index a88d7b27..b70f0728 100644 --- a/docs/use_cases/custom_representation_types.rst +++ b/docs/use_cases/custom_representation_types.rst @@ -18,10 +18,10 @@ satisfy at least the `QuantityValue` concept. Which means that they: - cannot be quantities by themselves, - cannot be wrappers over the `quantity` type (i.e. ``std::optional>``), - have to be regular types (e.g. they have to provide equality operators) -- if they are constructible from a fundamental integral type, they have to provide multiplication - and division operators for their types, -- otherwise, their values need to support the multiplication or division by the values of the - ``std::int64_t`` type which is the type used to store elements of a `ratio`. +- should either have common type with ``std::intmax_t`` which provides multiplication and + division operators +- or :expr:`T::value_type` must be valid, and common type of ``T::value_type`` and ``std::intmax_t`` + must at least provide multiplication and division operators with itself and ``T``. With the above we will be able to construct quantities, convert between the units of the same dimension, and compare them for equality. @@ -30,14 +30,16 @@ dimension, and compare them for equality. The Simplest Custom Representation Type ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -The simplest representation type that fullfills the above requirements can look as follows:: +The simplest representation type that fulfills the above requirements can look as follows:: class my_rep { - int value_{}; + std::intmax_t value_; public: my_rep() = default; - constexpr my_rep(int v) : value_(v) {} - [[nodiscard]] friend constexpr bool operator==(my_rep lhs, my_rep rhs) = default; + explicit constexpr my_rep(std::intmax_t v) noexcept : value_(v) {} + + [[nodiscard]] bool operator==(my_rep) const = default; + [[nodiscard]] friend constexpr my_rep operator*(my_rep lhs, my_rep rhs) { return my_rep(lhs.value_ * rhs.value_); @@ -51,7 +53,7 @@ The simplest representation type that fullfills the above requirements can look Now we can put ``my_rep`` as the last parameter of the `quantity` class template and the following code will work just fine:: - static_assert(si::length(2'000) == si::length(2)); + static_assert(si::length(my_rep(2'000)) == si::length(my_rep(2))); Construction of Quantities with Custom Representation Types @@ -158,7 +160,7 @@ Thanks to it the following code will run as expected:: si::length(3)); Of course, the above operators are the smallest possible set to provide support for basic -arithmetic operations. In case the user wants to use faster or more sofisticated operators +arithmetic operations. In case the user wants to use faster or more sophisticated operators the following ones can be provided:: class my_rep {