[[nodiscard]] added to quantity_cast and DESIGN.md update with the new quantity_cast functionality

This commit is contained in:
Mateusz Pusz
2019-09-30 09:05:50 +02:00
parent 3b07c51f50
commit f9d6b53b72
2 changed files with 15 additions and 9 deletions

View File

@@ -392,11 +392,17 @@ a `quantity` type or only its template parameters (`Unit`, `Rep`):
```cpp ```cpp
template<Quantity To, typename U, typename Rep> template<Quantity To, typename U, typename Rep>
requires std::same_as<typename To::dimension, typename U::dimension> requires same_dim<typename To::dimension, typename U::dimension>
constexpr To quantity_cast(const quantity<U, Rep>& q); [[nodiscard]] constexpr To quantity_cast(const quantity<U, Rep>& q);
template<Unit ToU, Scalar ToRep = double, typename U, typename Rep> template<Unit ToU, Scalar ToRep, typename U, typename Rep>
constexpr quantity<ToU, ToRep> quantity_cast(const quantity<U, Rep>& q); [[nodiscard]] constexpr quantity<ToU, ToRep> quantity_cast(const quantity<U, Rep>& q);
template<Unit ToU, typename U, typename Rep>
[[nodiscard]] constexpr quantity<ToU, Rep> quantity_cast(const quantity<U, Rep>& q);
template<Scalar ToRep, typename U, typename Rep>
[[nodiscard]] constexpr quantity<U, ToRep> quantity_cast(const quantity<U, Rep>& q);
``` ```
## Strong types instead of aliases, and type downcasting facility ## Strong types instead of aliases, and type downcasting facility

View File

@@ -126,8 +126,8 @@ namespace units {
} // namespace detail } // namespace detail
template<Quantity To, typename U, typename Rep> template<Quantity To, typename U, typename Rep>
requires same_dim<typename To::dimension, typename U::dimension> [[nodiscard]] constexpr To quantity_cast(const quantity<U, Rep>& q)
constexpr To quantity_cast(const quantity<U, Rep>& q) requires same_dim<typename To::dimension, typename U::dimension>
{ {
using c_ratio = ratio_divide<typename U::ratio, typename To::unit::ratio>; using c_ratio = ratio_divide<typename U::ratio, typename To::unit::ratio>;
using c_rep = std::common_type_t<typename To::rep, Rep, intmax_t>; using c_rep = std::common_type_t<typename To::rep, Rep, intmax_t>;
@@ -136,17 +136,17 @@ namespace units {
} }
template<Unit ToU, Scalar ToRep, typename U, typename Rep> template<Unit ToU, Scalar ToRep, typename U, typename Rep>
constexpr quantity<ToU, ToRep> quantity_cast(const quantity<U, Rep>& q) [[nodiscard]] constexpr quantity<ToU, ToRep> quantity_cast(const quantity<U, Rep>& q)
{ {
return quantity_cast<quantity<ToU, ToRep>>(q); return quantity_cast<quantity<ToU, ToRep>>(q);
} }
template<Unit ToU, typename U, typename Rep> template<Unit ToU, typename U, typename Rep>
constexpr quantity<ToU, Rep> quantity_cast(const quantity<U, Rep>& q) [[nodiscard]] constexpr quantity<ToU, Rep> quantity_cast(const quantity<U, Rep>& q)
{ {
return quantity_cast<quantity<ToU, Rep>>(q); return quantity_cast<quantity<ToU, Rep>>(q);
} }
template<Scalar ToRep, typename U, typename Rep> template<Scalar ToRep, typename U, typename Rep>
constexpr quantity<U, ToRep> quantity_cast(const quantity<U, Rep>& q) [[nodiscard]] constexpr quantity<U, ToRep> quantity_cast(const quantity<U, Rep>& q)
{ {
return quantity_cast<quantity<U, ToRep>>(q); return quantity_cast<quantity<U, ToRep>>(q);
} }