diff --git a/docs/users_guide/framework_basics/character_of_a_quantity.md b/docs/users_guide/framework_basics/character_of_a_quantity.md index 90402906a..769fe55d8 100644 --- a/docs/users_guide/framework_basics/character_of_a_quantity.md +++ b/docs/users_guide/framework_basics/character_of_a_quantity.md @@ -148,7 +148,7 @@ As we remember, the `quantity` class template is defined as follows: ```cpp template Rep = double> + RepresentationOf Rep = double> class quantity; ``` diff --git a/docs/users_guide/framework_basics/concepts.md b/docs/users_guide/framework_basics/concepts.md index 797e7201a..c8177d39d 100644 --- a/docs/users_guide/framework_basics/concepts.md +++ b/docs/users_guide/framework_basics/concepts.md @@ -159,15 +159,28 @@ A `Reference` can either be: [value of a quantity](../../appendix/glossary.md#quantity-value). -### `RepresentationOf` { #RepresentationOf } +### `RepresentationOf` { #RepresentationOf } -`RepresentationOf` concept is satisfied by all `Representation` types that are of a specified -[quantity character](../../appendix/glossary.md#character) `Ch`. +`RepresentationOf` concept is satisfied: + +- if the type of `V` satisfies [`QuantitySpec`](#QuantitySpec): + + - by all [`Representation`](#Representation) types when `V` describes + a [quantity kind](../../appendix/glossary.md#kind), + - otherwise, by [`Representation`](#Representation) types that are of + a [quantity character](../../appendix/glossary.md#character) associated with a provided + quantity specification `V`. + +- if `V` is of `quantity_character` type: + + - by [`Representation`](#Representation) types that are of a provided + [quantity character](../../appendix/glossary.md#character). 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` +- `is_complex` - `is_vector` - `is_tensor` diff --git a/docs/users_guide/framework_basics/design_overview.md b/docs/users_guide/framework_basics/design_overview.md index 7446bea7f..3e5de7037 100644 --- a/docs/users_guide/framework_basics/design_overview.md +++ b/docs/users_guide/framework_basics/design_overview.md @@ -316,7 +316,7 @@ This is why a `quantity` class template is defined in the library as: ```cpp template Rep = double> + RepresentationOf Rep = double> class quantity; ``` @@ -365,7 +365,7 @@ In the **mp-units** library, the quantity point is implemented as: ```cpp template auto PO, - RepresentationOf Rep = double> + RepresentationOf Rep = double> class quantity_point; ``` diff --git a/docs/users_guide/framework_basics/simple_and_typed_quantities.md b/docs/users_guide/framework_basics/simple_and_typed_quantities.md index 178c36e24..dd7ee228a 100644 --- a/docs/users_guide/framework_basics/simple_and_typed_quantities.md +++ b/docs/users_guide/framework_basics/simple_and_typed_quantities.md @@ -20,7 +20,7 @@ In the **mp-units** library, a quantity is represented with the following class ```cpp template Rep = double> + RepresentationOf Rep = double> class quantity; ``` diff --git a/docs/users_guide/framework_basics/the_affine_space.md b/docs/users_guide/framework_basics/the_affine_space.md index 688c4379e..50a297722 100644 --- a/docs/users_guide/framework_basics/the_affine_space.md +++ b/docs/users_guide/framework_basics/the_affine_space.md @@ -96,7 +96,7 @@ origin: ```cpp template auto PO = default_point_origin(R), - RepresentationOf Rep = double> + RepresentationOf Rep = double> class quantity_point; ``` diff --git a/src/core/include/mp-units/framework/construction_helpers.h b/src/core/include/mp-units/framework/construction_helpers.h index 43eade949..36ffac6eb 100644 --- a/src/core/include/mp-units/framework/construction_helpers.h +++ b/src/core/include/mp-units/framework/construction_helpers.h @@ -39,7 +39,7 @@ namespace mp_units { template struct delta_ { - template Rep = std::remove_cvref_t> + template Rep = std::remove_cvref_t> [[nodiscard]] constexpr quantity operator()(FwdRep&& lhs) const { return quantity{std::forward(lhs), R{}}; @@ -48,7 +48,7 @@ struct delta_ { template struct absolute_ { - template Rep = std::remove_cvref_t> + template Rep = std::remove_cvref_t> [[nodiscard]] constexpr quantity_point operator()(FwdRep&& lhs) const { diff --git a/src/core/include/mp-units/framework/quantity.h b/src/core/include/mp-units/framework/quantity.h index 8c265516c..bfd55b924 100644 --- a/src/core/include/mp-units/framework/quantity.h +++ b/src/core/include/mp-units/framework/quantity.h @@ -78,14 +78,15 @@ concept QuantityConvertibleTo = // deduced thus the function is evaluated here and may emit truncating conversion or other warnings) requires(QFrom q) { sudo_cast(q); }; -template -concept InvokeResultOf = std::regular_invocable && RepresentationOf, Ch>; +template +concept InvokeResultOf = QuantitySpec && std::regular_invocable && + RepresentationOf, QS>; template, - std::remove_const_t>::character> -concept InvocableQuantities = - Quantity && Quantity && InvokeResultOf; + auto QS = std::invoke_result_t, + std::remove_const_t>{}> +concept InvocableQuantities = QuantitySpec && Quantity && Quantity && + InvokeResultOf; // TODO remove the following when clang diagnostics improve // https://github.com/llvm/llvm-project/issues/96660 @@ -104,7 +105,7 @@ concept CommonlyInvocableQuantities = Quantity && Quantity && HaveCommonReference && std::convertible_to> && std::convertible_to> && - InvocableQuantities; + InvocableQuantities; template concept SameValueAs = (equivalent(get_unit(R1), get_unit(R2))) && std::convertible_to; @@ -128,7 +129,7 @@ MP_UNITS_EXPORT_BEGIN * @tparam R a reference of the quantity providing all information about quantity properties * @tparam Rep a type to be used to represent values of a quantity */ -template Rep = double> +template Rep = double> class quantity { public: Rep numerical_value_is_an_implementation_detail_; ///< needs to be public for a structural type @@ -227,14 +228,14 @@ class quantity { return quantity{*this}; } - template ToRep> + template ToRep> requires detail::QuantityConvertibleTo> [[nodiscard]] constexpr QuantityOf auto in() const { return quantity{*this}; } - template ToRep, detail::UnitCompatibleWith ToU> + template ToRep, detail::UnitCompatibleWith ToU> requires detail::QuantityConvertibleTo> [[nodiscard]] constexpr QuantityOf auto in(ToU) const { @@ -248,14 +249,14 @@ class quantity { return value_cast(*this); } - template ToRep> + template ToRep> requires requires(quantity q) { value_cast(q); } [[nodiscard]] constexpr QuantityOf auto force_in() const { return value_cast(*this); } - template ToRep, detail::UnitCompatibleWith ToU> + template ToRep, detail::UnitCompatibleWith ToU> requires requires(quantity q) { value_cast(q); } [[nodiscard]] constexpr QuantityOf auto force_in(ToU) const { @@ -474,17 +475,15 @@ class quantity { ret::reference}; } - template Q, RepresentationOf Value> - requires(Q::unit == ::mp_units::one) && - detail::InvokeResultOf, Rep, const Value&> + template Q, Representation Value> + requires(Q::unit == ::mp_units::one) && detail::InvokeResultOf, Rep, const Value&> [[nodiscard]] friend constexpr Quantity auto operator+(const Q& lhs, const Value& rhs) { return lhs + ::mp_units::quantity{rhs}; } - template Q, RepresentationOf Value> - requires(Q::unit == ::mp_units::one) && - detail::InvokeResultOf, Rep, const Value&> + template Q, Representation Value> + requires(Q::unit == ::mp_units::one) && detail::InvokeResultOf, Rep, const Value&> [[nodiscard]] friend constexpr Quantity auto operator+(const Value& lhs, const Q& rhs) { return ::mp_units::quantity{lhs} + rhs; @@ -501,17 +500,15 @@ class quantity { ret::reference}; } - template Q, RepresentationOf Value> - requires(Q::unit == ::mp_units::one) && - detail::InvokeResultOf, Rep, const Value&> + template Q, Representation Value> + requires(Q::unit == ::mp_units::one) && detail::InvokeResultOf, Rep, const Value&> [[nodiscard]] friend constexpr Quantity auto operator-(const Q& lhs, const Value& rhs) { return lhs - ::mp_units::quantity{rhs}; } - template Q, RepresentationOf Value> - requires(Q::unit == ::mp_units::one) && - detail::InvokeResultOf, Rep, const Value&> + template Q, Representation Value> + requires(Q::unit == ::mp_units::one) && detail::InvokeResultOf, Rep, const Value&> [[nodiscard]] friend constexpr Quantity auto operator-(const Value& lhs, const Q& rhs) { return ::mp_units::quantity{lhs} - rhs; @@ -530,17 +527,15 @@ class quantity { ret::reference}; } - template Q, RepresentationOf Value> - requires(Q::unit == ::mp_units::one) && - detail::InvokeResultOf, Rep, const Value&> + template Q, Representation Value> + requires(Q::unit == ::mp_units::one) && detail::InvokeResultOf, Rep, const Value&> [[nodiscard]] friend constexpr Quantity auto operator%(const Q& lhs, const Value& rhs) { return lhs % ::mp_units::quantity{rhs}; } - template Q, RepresentationOf Value> - requires(Q::unit == ::mp_units::one) && - detail::InvokeResultOf, Rep, const Value&> + template Q, Representation Value> + requires(Q::unit == ::mp_units::one) && detail::InvokeResultOf, Rep, const Value&> [[nodiscard]] friend constexpr Quantity auto operator%(const Value& lhs, const Q& rhs) { return ::mp_units::quantity{lhs} % rhs; @@ -555,7 +550,7 @@ class quantity { template Q, typename Value> requires(!Quantity) && - (!Reference) && detail::InvokeResultOf, Rep, const Value&> + (!Reference) && detail::InvokeResultOf, Rep, const Value&> [[nodiscard]] friend constexpr QuantityOf auto operator*(const Q& q, const Value& v) { return ::mp_units::quantity{q.numerical_value_ref_in(unit) * v, R}; @@ -563,7 +558,7 @@ class quantity { template Q> requires(!Quantity) && - (!Reference) && detail::InvokeResultOf, const Value&, Rep> + (!Reference) && detail::InvokeResultOf, const Value&, Rep> [[nodiscard]] friend constexpr QuantityOf auto operator*(const Value& v, const Q& q) { return ::mp_units::quantity{v * q.numerical_value_ref_in(unit), R}; @@ -579,7 +574,7 @@ class quantity { template Q, typename Value> requires(!Quantity) && - (!Reference) && detail::InvokeResultOf, Rep, const Value&> + (!Reference) && detail::InvokeResultOf, Rep, const Value&> [[nodiscard]] friend constexpr QuantityOf auto operator/(const Q& q, const Value& v) { MP_UNITS_EXPECTS_DEBUG(v != quantity_values::zero()); @@ -588,7 +583,7 @@ class quantity { template Q> requires(!Quantity) && - (!Reference) && detail::InvokeResultOf, const Value&, Rep> + (!Reference) && detail::InvokeResultOf, const Value&, Rep> [[nodiscard]] friend constexpr QuantityOf auto operator/(const Value& v, const Q& q) { return ::mp_units::quantity{v / q.numerical_value_ref_in(unit), ::mp_units::one / R}; @@ -605,7 +600,7 @@ class quantity { return ct_lhs.numerical_value_ref_in(ct::unit) == ct_rhs.numerical_value_ref_in(ct::unit); } - template Q, RepresentationOf Value> + template Q, Representation Value> requires(Q::unit == ::mp_units::one) && std::equality_comparable_with [[nodiscard]] friend constexpr bool operator==(const Q& lhs, const Value& rhs) { @@ -623,7 +618,7 @@ class quantity { return ct_lhs.numerical_value_ref_in(ct::unit) <=> ct_rhs.numerical_value_ref_in(ct::unit); } - template Q, RepresentationOf Value> + template Q, Representation Value> requires(Q::unit == ::mp_units::one) && std::three_way_comparable_with [[nodiscard]] friend constexpr auto operator<=>(const Q& lhs, const Value& rhs) { @@ -650,18 +645,21 @@ template requires requires { { mp_units::get_common_reference(Q1::reference, Q2::reference) } -> mp_units::Reference; typename std::common_type_t; + requires mp_units::RepresentationOf, + mp_units::get_common_quantity_spec(Q1::quantity_spec, Q2::quantity_spec)>; } struct std::common_type { using type = mp_units::quantity>; }; -template Value> - requires(Q::unit == mp_units::one) && requires { typename std::common_type_t; } +template + requires(Q::unit == mp_units::one) && + requires { typename mp_units::quantity>; } struct std::common_type { using type = mp_units::quantity>; }; -template Value> - requires(Q::unit == mp_units::one) && requires { typename std::common_type_t; } +template + requires requires { typename std::common_type; } struct std::common_type : std::common_type {}; diff --git a/src/core/include/mp-units/framework/quantity_concepts.h b/src/core/include/mp-units/framework/quantity_concepts.h index 15cdc260b..b402a9270 100644 --- a/src/core/include/mp-units/framework/quantity_concepts.h +++ b/src/core/include/mp-units/framework/quantity_concepts.h @@ -31,7 +31,7 @@ namespace mp_units { -MP_UNITS_EXPORT template Rep> +MP_UNITS_EXPORT template Rep> class quantity; namespace detail { diff --git a/src/core/include/mp-units/framework/quantity_point.h b/src/core/include/mp-units/framework/quantity_point.h index e96220d0d..782b89b69 100644 --- a/src/core/include/mp-units/framework/quantity_point.h +++ b/src/core/include/mp-units/framework/quantity_point.h @@ -172,7 +172,7 @@ template * @tparam Rep a type to be used to represent values of a quantity point */ MP_UNITS_EXPORT template auto PO = default_point_origin(R), - RepresentationOf Rep = double> + RepresentationOf Rep = double> class quantity_point { public: // member types and values @@ -329,14 +329,14 @@ class quantity_point { return ::mp_units::quantity_point{quantity_ref_from(point_origin).in(ToU{}), point_origin}; } - template ToRep> + template ToRep> requires detail::QuantityConvertibleTo> [[nodiscard]] constexpr QuantityPointOf auto in() const { return ::mp_units::quantity_point{quantity_ref_from(point_origin).template in(), point_origin}; } - template ToRep, detail::UnitCompatibleWith ToU> + template ToRep, detail::UnitCompatibleWith ToU> requires detail::QuantityConvertibleTo> [[nodiscard]] constexpr QuantityPointOf auto in(ToU) const { @@ -350,14 +350,14 @@ class quantity_point { return ::mp_units::quantity_point{quantity_ref_from(point_origin).force_in(ToU{}), point_origin}; } - template ToRep> + template ToRep> requires requires(quantity_type q) { value_cast(q); } [[nodiscard]] constexpr QuantityPointOf auto force_in() const { return ::mp_units::quantity_point{quantity_ref_from(point_origin).template force_in(), point_origin}; } - template ToRep, detail::UnitCompatibleWith ToU> + template ToRep, detail::UnitCompatibleWith ToU> requires requires(quantity_type q) { value_cast(q); } [[nodiscard]] constexpr QuantityPointOf auto force_in(ToU) const { diff --git a/src/core/include/mp-units/framework/quantity_point_concepts.h b/src/core/include/mp-units/framework/quantity_point_concepts.h index 08834c9ae..e76836e9b 100644 --- a/src/core/include/mp-units/framework/quantity_point_concepts.h +++ b/src/core/include/mp-units/framework/quantity_point_concepts.h @@ -76,7 +76,7 @@ MP_UNITS_EXPORT template concept PointOriginFor = PointOrigin && QuantitySpecOf; MP_UNITS_EXPORT template auto PO, - RepresentationOf Rep> + RepresentationOf Rep> class quantity_point; namespace detail { diff --git a/src/core/include/mp-units/framework/reference.h b/src/core/include/mp-units/framework/reference.h index 0e209eb8d..10d0597d0 100644 --- a/src/core/include/mp-units/framework/reference.h +++ b/src/core/include/mp-units/framework/reference.h @@ -190,24 +190,21 @@ struct reference { }; -template Rep = std::remove_cvref_t> +template Rep = std::remove_cvref_t> requires(!detail::OffsetUnit) [[nodiscard]] constexpr quantity operator*(FwdRep&& lhs, R r) { return quantity{std::forward(lhs), r}; } -template Rep = std::remove_cvref_t> +template Rep = std::remove_cvref_t> requires(!detail::OffsetUnit) [[nodiscard]] constexpr quantity operator/(FwdRep&& lhs, R) { return quantity{std::forward(lhs), inverse(R{})}; } -template Rep = std::remove_cvref_t> +template Rep = std::remove_cvref_t> requires detail::OffsetUnit [[deprecated( "References using offset units (e.g., temperatures) should be constructed with the `delta` or `absolute` " @@ -217,8 +214,7 @@ operator*(FwdRep&& lhs, R r) return quantity{std::forward(lhs), r}; } -template Rep = std::remove_cvref_t> +template Rep = std::remove_cvref_t> requires detail::OffsetUnit [[deprecated( "References using offset units (e.g., temperatures) should be constructed with the `delta` or `absolute` " @@ -229,7 +225,7 @@ operator/(FwdRep&& lhs, R) } template - requires RepresentationOf, get_quantity_spec(R{}).character> + requires RepresentationOf, get_quantity_spec(R{})> // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) constexpr auto operator*(R, Rep&&) #if __cpp_deleted_function @@ -239,7 +235,7 @@ constexpr auto operator*(R, Rep&&) #endif template - requires RepresentationOf, get_quantity_spec(R{}).character> + requires RepresentationOf, get_quantity_spec(R{})> // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) constexpr auto operator/(R, Rep&&) #if __cpp_deleted_function diff --git a/src/core/include/mp-units/framework/representation_concepts.h b/src/core/include/mp-units/framework/representation_concepts.h index 6a999516d..2edfad8ec 100644 --- a/src/core/include/mp-units/framework/representation_concepts.h +++ b/src/core/include/mp-units/framework/representation_concepts.h @@ -25,6 +25,7 @@ // IWYU pragma: private, include #include #include +#include #ifndef MP_UNITS_IN_MODULE_INTERFACE #ifdef MP_UNITS_IMPORT_STD @@ -165,7 +166,11 @@ MP_UNITS_EXPORT template concept Representation = detail::ScalarRepresentation || detail::ComplexRepresentation || detail::VectorRepresentation || detail::TensorRepresentation; -MP_UNITS_EXPORT template -concept RepresentationOf = detail::IsOfCharacter && Representation; +MP_UNITS_EXPORT template +concept RepresentationOf = + Representation && + ((QuantitySpec && + (detail::QuantityKindSpec || detail::IsOfCharacter)) || + (std::same_as && detail::IsOfCharacter)); } // namespace mp_units diff --git a/src/core/include/mp-units/framework/value_cast.h b/src/core/include/mp-units/framework/value_cast.h index 020424dcd..1d70616c6 100644 --- a/src/core/include/mp-units/framework/value_cast.h +++ b/src/core/include/mp-units/framework/value_cast.h @@ -63,7 +63,7 @@ template> * @tparam ToRep a representation type to use for a target quantity */ template> - requires RepresentationOf && std::constructible_from + requires RepresentationOf && std::constructible_from [[nodiscard]] constexpr quantity value_cast(FwdQ&& q) { return detail::sudo_cast>(std::forward(q)); @@ -81,7 +81,7 @@ template> - requires(convertible(Q::reference, ToU)) && RepresentationOf && + requires(convertible(Q::reference, ToU)) && RepresentationOf && std::constructible_from [[nodiscard]] constexpr Quantity auto value_cast(FwdQ&& q) { @@ -89,7 +89,7 @@ template> - requires(convertible(Q::reference, ToU)) && RepresentationOf && + requires(convertible(Q::reference, ToU)) && RepresentationOf && std::constructible_from [[nodiscard]] constexpr Quantity auto value_cast(FwdQ&& q) { @@ -148,7 +148,7 @@ template> - requires RepresentationOf && std::constructible_from + requires RepresentationOf && std::constructible_from [[nodiscard]] constexpr quantity_point value_cast(FwdQP&& qp) { return {value_cast(std::forward(qp).quantity_from_origin_is_an_implementation_detail_), @@ -167,7 +167,7 @@ template> - requires(convertible(QP::reference, ToU)) && RepresentationOf && + requires(convertible(QP::reference, ToU)) && RepresentationOf && std::constructible_from [[nodiscard]] constexpr QuantityPoint auto value_cast(FwdQP&& qp) { @@ -177,7 +177,7 @@ template> - requires(convertible(QP::reference, ToU)) && RepresentationOf && + requires(convertible(QP::reference, ToU)) && RepresentationOf && std::constructible_from [[nodiscard]] constexpr QuantityPoint auto value_cast(FwdQP&& qp) { diff --git a/test/static/quantity_test.cpp b/test/static/quantity_test.cpp index ba34a1601..adc3cd41e 100644 --- a/test/static/quantity_test.cpp +++ b/test/static/quantity_test.cpp @@ -319,7 +319,7 @@ static_assert(invalid_getter_with_unit_conversion); /////////////////////////////////////// template Rep = double> + RepresentationOf Rep = double> struct child_quantity : quantity { using quantity_type = quantity; static constexpr auto reference = R;