From 1a7321c98f0ae251eb6c8527258ad77ea51065fe Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Fri, 5 Apr 2024 01:42:26 -0400 Subject: [PATCH] xo-unit: quantity: ++ constexpr where appropriate --- include/xo/unit/quantity.hpp | 61 ++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/include/xo/unit/quantity.hpp b/include/xo/unit/quantity.hpp index 457afd4e..8085bcd5 100644 --- a/include/xo/unit/quantity.hpp +++ b/include/xo/unit/quantity.hpp @@ -286,7 +286,7 @@ namespace xo { // ----- operator+ ----- template - inline Quantity1 operator+ (Quantity1 x, Quantity2 y) { + inline constexpr Quantity1 operator+ (Quantity1 x, Quantity2 y) { static_assert(same_dimension_v); /* convert y to match units used by x; @@ -298,7 +298,7 @@ namespace xo { } template - inline Quantity1 operator- (Quantity1 x, Quantity2 y) { + inline constexpr Quantity1 operator- (Quantity1 x, Quantity2 y) { static_assert(std::same_as < typename Quantity1::unit_type::dimension_type::canon_type, typename Quantity2::unit_type::dimension_type::canon_type >); @@ -315,7 +315,7 @@ namespace xo { } template - inline auto operator* (Quantity1 x, Quantity2 y) { + inline constexpr auto operator* (Quantity1 x, Quantity2 y) { static_assert(quantity_concept); static_assert(quantity_concept); @@ -325,7 +325,7 @@ namespace xo { /** e.g. DECLARE_LH_MULT(int32_t) **/ # define DECLARE_LH_MULT(lhtype) \ template \ - inline auto \ + inline constexpr auto \ operator* (lhtype x, Quantity y) { \ static_assert(quantity_concept); \ return y.scale_by(x); \ @@ -346,7 +346,7 @@ namespace xo { /** e.g. DECLARE_RH_MULT(int32_t) **/ # define DECLARE_RH_MULT(rhtype) \ template \ - inline auto \ + inline constexpr auto \ operator* (Quantity x, rhtype y) { \ static_assert(quantity_concept); \ return x.scale_by(y); \ @@ -365,7 +365,7 @@ namespace xo { # undef DECLARE_LH_MULT template - inline auto operator/ (Quantity1 x, Quantity2 y) { + inline constexpr auto operator/ (Quantity1 x, Quantity2 y) { static_assert(quantity_concept); static_assert(quantity_concept); @@ -374,7 +374,7 @@ namespace xo { # define DECLARE_LH_DIV(lhtype) \ template \ - inline auto \ + inline constexpr auto \ operator/ (lhtype x, Quantity y) { \ static_assert(quantity_concept); \ return y.divide_into(x); \ @@ -394,7 +394,7 @@ namespace xo { # define DECLARE_RH_DIV(rhtype) \ template \ - inline auto \ + inline constexpr auto \ operator/ (Quantity x, rhtype y) { \ static_assert(quantity_concept); \ return x.divide_by(y); \ @@ -422,72 +422,85 @@ namespace xo { namespace qty { // ----- mass ----- + /** @brief create quantity representing @p x milligrams **/ template - inline auto milligrams(Repr x) -> quantity { + inline constexpr auto milligrams(Repr x) -> quantity { return quantity::promote(x); }; + /** @brief create quantity representing @p x grams **/ template - inline auto grams(Repr x) -> quantity { + inline constexpr auto grams(Repr x) -> quantity { return quantity::promote(x); }; + /** @brief create quantity representing @p x kilograms **/ template - inline auto kilograms(Repr x) -> quantity { + inline constexpr auto kilograms(Repr x) -> quantity { return quantity::promote(x); }; // ----- distance ----- + /** @brief create quantity representing @p x millimeters **/ template - inline auto millimeters(Repr x) -> quantity { + inline constexpr auto millimeters(Repr x) -> quantity { return quantity::promote(x); } + /** @brief create quantity representing @p x meters **/ template - inline auto meters(Repr x) -> quantity { + inline constexpr auto meters(Repr x) -> quantity { return quantity::promote(x); } + /** @brief create quantity representing @p x kilometers **/ template - inline auto kilometers(Repr x) -> quantity { + inline constexpr auto kilometers(Repr x) -> quantity { return quantity::promote(x); } // ----- time ----- + /** @brief create quantity representing @p x nanoseconds **/ template - inline auto nanoseconds(Repr x) -> quantity { + inline constexpr auto nanoseconds(Repr x) -> quantity { return quantity::promote(x); } + /** @brief create quantity representing @p x microseconds **/ template - inline auto microseconds(Repr x) -> quantity { + inline constexpr auto microseconds(Repr x) -> quantity { return quantity::promote(x); } + /** @brief create quantity representing @p x milliseconds **/ template - inline auto milliseconds(Repr x) -> quantity { + inline constexpr auto milliseconds(Repr x) -> quantity { return quantity::promote(x); } + /** @brief create quantity representing @p x seconds **/ template - inline auto seconds(Repr x) -> quantity { + inline constexpr auto seconds(Repr x) -> quantity { return quantity::promote(x); } + /** @brief create quantity representing @p x minutes **/ template - inline auto minutes(Repr x) -> quantity { + inline constexpr auto minutes(Repr x) -> quantity { return quantity::promote(x); } + /** @brief create quantity representing @p x hours **/ template - inline auto hours(Repr x) -> quantity { + inline constexpr auto hours(Repr x) -> quantity { return quantity::promote(x); } + /** @brief create quantity representing @p x days (1 day = exactly 24 hours) **/ template - inline auto days(Repr x) -> quantity { + inline constexpr auto days(Repr x) -> quantity { return quantity::promote(x); } @@ -495,21 +508,21 @@ namespace xo { /** quantity in units of 1/sqrt(1dy) **/ template - inline auto volatility1d(Repr x) -> quantity { + inline constexpr auto volatility1d(Repr x) -> quantity { return quantity::promote(x); } /** quantity in units of 1/sqrt(30days) **/ template - inline auto volatility30d(Repr x) -> quantity { + inline constexpr auto volatility30d(Repr x) -> quantity { return quantity::promote(x); } /** quantity in units of 1/sqrt(250days) **/ template - inline auto volatility250d(Repr x) -> quantity { + inline constexpr auto volatility250d(Repr x) -> quantity { return quantity::promote(x); } } /*namespace qty*/