mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-29 18:07:16 +02:00
better handle simultaneous change of representation, unit and point_origin in value_cast, to prevent overflow in more cases.
This commit is contained in:
@ -1695,21 +1695,25 @@ static_assert(value_cast<m, float>(lvalue_qp).quantity_from_zero().numerical_val
|
||||
static_assert(value_cast<quantity<km, int>>(quantity_point{2000 * m}).quantity_from_zero().numerical_value_in(km) == 2);
|
||||
static_assert(value_cast<quantity_point<km>>(quantity_point{2000 * m}).quantity_from_zero().numerical_value_in(km) ==
|
||||
2);
|
||||
static_assert(
|
||||
!requires(quantity_point<isq::width[m]> qp) { value_cast<quantity<m>>(qp); },
|
||||
"value_cast shall not cast between different quantity types");
|
||||
static_assert(
|
||||
!requires(quantity_point<m> qp) { value_cast<quantity<isq::width[m]>>(qp); },
|
||||
"value_cast shall not cast between different quantity types");
|
||||
static_assert(value_cast<quantity_point<m, mean_sea_level>>(quantity_point<km, ground_level>{2 * km})
|
||||
.quantity_ref_from(mean_sea_level)
|
||||
|
||||
template<typename ToQ, typename FromQ>
|
||||
constexpr bool value_cast_is_forbidden()
|
||||
{
|
||||
// it appears we cannot have the requires clause right inside static_assert
|
||||
return !requires(FromQ q) { value_cast<ToQ>(q); };
|
||||
}
|
||||
static_assert(value_cast_is_forbidden<quantity_point<m>, quantity_point<isq::width[m]>>(),
|
||||
"value_cast shall not cast between different quantity types");
|
||||
static_assert(value_cast_is_forbidden<quantity_point<isq::width[m]>, quantity_point<m>>(),
|
||||
"value_cast shall not cast between different quantity types");
|
||||
static_assert(value_cast<quantity_point<isq::height[m], mean_sea_level>>(quantity_point{2 * isq::height[km], ground_level})
|
||||
.quantity_from_origin_is_an_implementation_detail_
|
||||
.numerical_value_in(m) == 2042);
|
||||
static_assert(value_cast<quantity_point<cm, mean_sea_level, int>>(quantity_point<mm, ground_level, std::int8_t>{
|
||||
std::int8_t{100} * mm})
|
||||
.quantity_ref_from(mean_sea_level)
|
||||
static_assert(value_cast<quantity_point<isq::height[cm], mean_sea_level, int>>(quantity_point{std::int8_t{100} * isq::height[mm], ground_level})
|
||||
.quantity_from_origin_is_an_implementation_detail_
|
||||
.numerical_value_in(cm) == 4210);
|
||||
static_assert(value_cast<quantity_point<mm, ground_level, std::int8_t>>(quantity_point<cm, mean_sea_level>{4210 * cm})
|
||||
.quantity_ref_from(ground_level)
|
||||
static_assert(value_cast<quantity_point<isq::height[mm], ground_level, std::int8_t>>(quantity_point{4210 * isq::height[cm], mean_sea_level})
|
||||
.quantity_from_origin_is_an_implementation_detail_
|
||||
.numerical_value_in(mm) == 100);
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user