Design cleanup

- unknown_unit added
- examples refactored
- base_type renamed to downcast_base_type
- scaled_unit renamed to named_scaled_unit
- detail::reference_unit renamed to scaled_unit
- quantity_test cleanup
This commit is contained in:
Mateusz Pusz
2019-12-06 12:18:39 +01:00
parent c48bfe2098
commit f31b26b5e5
22 changed files with 354 additions and 160 deletions

View File

@@ -42,4 +42,4 @@ add_subdirectory(src)
add_subdirectory(test) add_subdirectory(test)
# add usage example # add usage example
#add_subdirectory(example) add_subdirectory(example)

View File

@@ -20,15 +20,11 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
# example app function(add_example target)
add_executable(example example.cpp) add_executable(${target} ${target}.cpp)
target_link_libraries(example target_link_libraries(${target} PRIVATE mp::units)
PRIVATE endfunction()
mp::units
)
add_executable(measurement measurement.cpp) add_example(avg_velocity)
target_link_libraries(measurement add_example(measurement)
PRIVATE add_example(unknown_dimension)
mp::units
)

147
example/avg_velocity.cpp Normal file
View File

@@ -0,0 +1,147 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <units/physical/si/velocity.h>
#include <iostream>
namespace {
constexpr units::si::velocity<units::si::metre_per_second, int>
fixed_int_si_avg_speed(units::si::length<units::si::metre, int> d,
units::si::time<units::si::second, int> t)
{
return d / t;
}
constexpr units::si::velocity<units::si::metre_per_second>
fixed_double_si_avg_speed(units::si::length<units::si::metre> d,
units::si::time<units::si::second> t)
{
return d / t;
}
template<typename U1, typename R1, typename U2, typename R2>
constexpr units::Velocity AUTO si_avg_speed(units::si::length<U1, R1> d,
units::si::time<U2, R2> t)
{
return d / t;
}
constexpr units::Velocity AUTO avg_speed(units::Length AUTO d, units::Time AUTO t)
{
return d / t;
}
template<units::Length D, units::Time T, units::Velocity V>
void print_result(D distance, T duration, V velocity)
{
const auto result_in_kmph = units::quantity_cast<units::si::kilometre_per_hour>(velocity);
std::cout << "Average speed of a car that makes " << distance << " in "
<< duration << " is " << result_in_kmph << ".\n";
}
void example()
{
using namespace units;
using namespace units::si::literals;
// SI (int)
{
constexpr Length AUTO distance = 220km; // constructed from a UDL
constexpr si::time<si::hour, int> duration(2); // constructed from a value
std::cout << "SI units with 'int' as representation\n";
print_result(distance, duration, fixed_int_si_avg_speed(distance, duration));
print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));
// the framework will not allow a division (and multiplication) of different dimensions
// with two integral representation (at least one of them have to ba floating-point one)
print_result(distance, duration, si_avg_speed(quantity_cast<double>(distance), duration));
print_result(distance, duration, avg_speed(quantity_cast<double>(distance), duration));
}
// SI (double)
{
constexpr Length AUTO distance = 220.km; // constructed from a UDL
constexpr si::time<si::hour> duration(2); // constructed from a value
std::cout << "\nSI units with 'double' as representation\n";
// conversion from a floating-point to an integral type is a truncating one so an explicit cast is needed
print_result(distance, duration, fixed_int_si_avg_speed(quantity_cast<int>(distance), quantity_cast<int>(duration)));
print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));
print_result(distance, duration, si_avg_speed(distance, duration));
print_result(distance, duration, avg_speed(distance, duration));
}
// Customary Units (int)
{
constexpr Length AUTO distance = 140mi; // constructed from a UDL
constexpr si::time<si::hour, int> duration(2); // constructed from a value
std::cout << "\nUS Customary Units with 'int' as representation\n";
// it is not possible to make a lossless conversion of miles to meters on an integral type
// (explicit cast needed)
print_result(distance, duration, fixed_int_si_avg_speed(quantity_cast<si::metre>(distance), duration));
print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));
// the framework will not allow a division (and multiplication) of different dimensions
// with two integral representation (at least one of them have to ba floating-point one)
print_result(distance, duration, si_avg_speed(quantity_cast<double>(distance), duration));
print_result(distance, duration, avg_speed(quantity_cast<double>(distance), duration));
}
// Customary Units (double)
{
constexpr Length AUTO distance = 140.mi; // constructed from a UDL
constexpr si::time<si::hour> duration(2); // constructed from a value
std::cout << "\nUS Customary Units with 'double' as representation\n";
// conversion from a floating-point to an integral type is a truncating one so an explicit cast is needed
// also it is not possible to make a lossless conversion of miles to meters on an integral type
// (explicit cast needed)
print_result(distance, duration, fixed_int_si_avg_speed(quantity_cast<si::metre, int>(distance), quantity_cast<int>(duration)));
print_result(distance, duration, fixed_double_si_avg_speed(distance, duration));
print_result(distance, duration, si_avg_speed(distance, duration));
print_result(distance, duration, avg_speed(distance, duration));
}
}
} // namespace
int main()
{
try {
example();
}
catch (const std::exception& ex) {
std::cerr << "Unhandled std exception caught: " << ex.what() << '\n';
}
catch (...) {
std::cerr << "Unhandled unknown exception caught\n";
}
}

View File

@@ -20,14 +20,11 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
#include <units/dimensions/acceleration.h> #include <units/physical/si/acceleration.h>
#include <iostream> #include <iostream>
namespace { namespace {
template<typename T, template<typename> typename Trait>
concept Satisfies = Trait<T>::value;
// root sum of squares // root sum of squares
template<typename T> template<typename T>
T rss(const T& v1, const T& v2) T rss(const T& v1, const T& v2)
@@ -118,24 +115,38 @@ namespace {
value_type uncertainty_{}; value_type uncertainty_{};
}; };
template<units::Unit U>
using m_quantity = units::quantity<U, measurement<double>>;
} // namespace } // namespace
template<typename T> template<typename T>
inline constexpr bool units::treat_as_floating_point<measurement<T>> = std::is_floating_point_v<T>; inline constexpr bool units::treat_as_floating_point<measurement<T>> = std::is_floating_point_v<T>;
namespace {
void example()
{
using namespace units;
const auto a = si::acceleration<si::metre_per_second_sq, measurement<double>>(measurement(9.8, 0.1));
const auto t = si::time<si::second, measurement<double>>(measurement(1.2, 0.1));
const Velocity AUTO v1 = a * t;
std::cout << a << " * " << t << " = " << v1 << " = " << quantity_cast<si::kilometre_per_hour>(v1) << '\n';
si::length<si::metre, measurement<double>> length(measurement(123., 1.));
std::cout << "10 * " << length << " = " << 10 * length << '\n';
}
} // namespace
int main() int main()
{ {
const auto a = m_quantity<units::metre_per_second_sq>(measurement(9.8, 0.1)); try {
const auto t = m_quantity<units::second>(measurement(1.2, 0.1)); example();
}
units::Velocity AUTO v1 = a * t; catch (const std::exception& ex) {
m_quantity<units::kilometre_per_hour> v2(v1); std::cerr << "Unhandled std exception caught: " << ex.what() << '\n';
std::cout << a << " * " << t << " = " << v1 << " = " << v2 << '\n'; }
catch (...) {
m_quantity<units::metre> length(measurement(123., 1.)); std::cerr << "Unhandled unknown exception caught\n";
std::cout << "10 * " << length << " = " << 10 * length << '\n'; }
} }

View File

@@ -20,43 +20,43 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE. // SOFTWARE.
#include <units/dimensions/velocity.h> #include <units/physical/si/velocity.h>
#include <iostream> #include <iostream>
namespace { namespace {
using namespace units::literals;
template<units::Length D, units::Time T> template<units::Length D, units::Time T>
constexpr units::Velocity AUTO avg_speed(D d, T t) constexpr units::Velocity AUTO avg_speed(D d, T t)
{ {
return d / t; return d / t;
} }
template<units::Velocity V, units::Time T> void example()
void example_1(V v, T t)
{ {
const units::Length AUTO distance = v * t; using namespace units::si::literals;
std::cout << "A car driving " << v << " in a time of " << t << " will pass "
<< units::quantity_cast<units::quantity<units::metre, double>>(distance) << ".\n"; units::Length AUTO d1 = 123m;
units::Time AUTO t1 = 10s;
units::Velocity AUTO v1 = avg_speed(d1, t1);
auto temp1 = v1 * 50m; // produces intermediate unknown dimension with 'unknown_unit' as its 'coherent_unit'
units::Velocity AUTO v2 = temp1 / 100m; // back to known dimensions again
units::Length AUTO d2 = v2 * 60s;
std::cout << "d1 = " << d1 << '\n';
std::cout << "t1 = " << t1 << '\n';
std::cout << "v1 = " << v1 << '\n';
std::cout << "temp1 = " << temp1 << '\n';
std::cout << "v2 = " << v2 << '\n';
std::cout << "d2 = " << d2 << '\n';
} }
void example_2(double distance_v, double duration_v) } // namespace
{
units::quantity<units::kilometre> distance(distance_v);
units::quantity<units::hour> duration(duration_v);
const auto kmph = quantity_cast<units::kilometre_per_hour>(avg_speed(distance, duration));
std::cout << "Average speed of a car that makes " << distance << " in "
<< duration << " is " << kmph << ".\n";
}
}
int main() int main()
{ {
try { try {
example_1(60kmph, 10.0min); example();
example_2(220, 2);
} }
catch (const std::exception& ex) { catch (const std::exception& ex) {
std::cerr << "Unhandled std exception caught: " << ex.what() << '\n'; std::cerr << "Unhandled std exception caught: " << ex.what() << '\n';

View File

@@ -29,16 +29,16 @@ namespace units {
template<typename BaseType> template<typename BaseType>
struct downcast_base { struct downcast_base {
using base_type = BaseType; using downcast_base_type = BaseType;
friend auto downcast_guide(downcast_base); friend auto downcast_guide(downcast_base);
}; };
template<typename T> template<typename T>
concept Downcastable = concept Downcastable =
requires { requires {
typename T::base_type; typename T::downcast_base_type;
} && } &&
std::derived_from<T, downcast_base<typename T::base_type>>; std::derived_from<T, downcast_base<typename T::downcast_base_type>>;
template<typename Target, Downcastable T> template<typename Target, Downcastable T>
struct downcast_child : T { struct downcast_child : T {
@@ -67,6 +67,6 @@ namespace units {
using downcast = decltype(detail::downcast_impl<T>()); using downcast = decltype(detail::downcast_impl<T>());
template<Downcastable T> template<Downcastable T>
using downcast_base_t = T::base_type; using downcast_base_t = T::downcast_base_type;
} // namespace units } // namespace units

View File

@@ -27,15 +27,14 @@
namespace units { namespace units {
namespace detail { template<typename R>
concept UnitRatio = Ratio<R> && (R::num * R::den > 0);
template<typename U, Ratio R> template<typename U, UnitRatio R>
struct reference_unit; struct scaled_unit;
} // namespace detail
// Unit // Unit
template<typename T> template<typename T>
concept Unit = is_derived_from_instantiation<T, detail::reference_unit>; concept Unit = is_derived_from_instantiation<T, scaled_unit>;
} // namespace units } // namespace units

View File

@@ -246,15 +246,39 @@ struct derived_dimension<Child, U, E, ERest...> : downcast_child<Child, typename
using coherent_unit = U; using coherent_unit = U;
}; };
// unknown_unit
struct unknown_unit {};
namespace detail {
template<typename T>
concept PredefinedDimension = Dimension<T> && requires { typename T::coherent_unit; };
template<Dimension D, Ratio R>
auto unit_for_dimension_impl()
{
if constexpr(PredefinedDimension<D>) {
return downcast<scaled_unit<typename D::coherent_unit::reference, R>>{};
}
else {
return scaled_unit<unknown_unit, R>{};
}
}
template<Dimension D, Ratio R>
using unit_for_dimension = decltype(unit_for_dimension_impl<D, R>());
}
// same_dim // same_dim
template<Dimension D1, Dimension D2> template<Dimension D1, Dimension D2>
inline constexpr bool same_dim; inline constexpr bool same_dim = false;
template<BaseDimension D1, BaseDimension D2> template<BaseDimension D1, BaseDimension D2>
inline constexpr bool same_dim<D1, D2> = std::is_same_v<D1, D2>; inline constexpr bool same_dim<D1, D2> = std::is_same_v<D1, D2>;
template<DerivedDimension D1, DerivedDimension D2> template<DerivedDimension D1, DerivedDimension D2>
inline constexpr bool same_dim<D1, D2> = std::is_same_v<typename D1::base_type, typename D2::base_type>; inline constexpr bool same_dim<D1, D2> = std::is_same_v<typename D1::downcast_base_type, typename D2::downcast_base_type>;
// dim_invert // dim_invert
namespace detail { namespace detail {
@@ -321,7 +345,7 @@ struct dimension_multiply_impl<D1, D2> {
template<BaseDimension D1, DerivedDimension D2> template<BaseDimension D1, DerivedDimension D2>
struct dimension_multiply_impl<D1, D2> { struct dimension_multiply_impl<D1, D2> {
using type = downcast<merge_dimension<derived_dimension<exp<D1, 1>>, typename D2::base_type>>; using type = downcast<merge_dimension<derived_dimension<exp<D1, 1>>, typename D2::downcast_base_type>>;
}; };
template<DerivedDimension D1, BaseDimension D2> template<DerivedDimension D1, BaseDimension D2>
@@ -331,7 +355,7 @@ struct dimension_multiply_impl<D1, D2> {
template<DerivedDimension D1, DerivedDimension D2> template<DerivedDimension D1, DerivedDimension D2>
struct dimension_multiply_impl<D1, D2> { struct dimension_multiply_impl<D1, D2> {
using type = downcast<merge_dimension<typename D1::base_type, typename D2::base_type>>; using type = downcast<merge_dimension<typename D1::downcast_base_type, typename D2::downcast_base_type>>;
}; };
} // namespace detail } // namespace detail
@@ -360,7 +384,7 @@ struct dimension_sqrt_impl<derived_dimension<exp<D, 2>>> {
template<DerivedDimension D> template<DerivedDimension D>
struct dimension_sqrt_impl<D> { struct dimension_sqrt_impl<D> {
using type = dimension_sqrt_impl<typename D::base_type>; using type = dimension_sqrt_impl<typename D::downcast_base_type>;
}; };
template<typename... Es> template<typename... Es>
@@ -371,7 +395,7 @@ struct dimension_sqrt_impl<derived_dimension<Es...>> {
} // namespace detail } // namespace detail
template<Dimension D> template<Dimension D>
using dimension_sqrt = detail::dimension_sqrt_impl<typename D::base_type>::type; using dimension_sqrt = detail::dimension_sqrt_impl<typename D::downcast_base_type>::type;
// dimension_pow // dimension_pow
namespace detail { namespace detail {

View File

@@ -39,7 +39,7 @@ namespace units {
{ {
using dim = dimension_pow<D, N>; using dim = dimension_pow<D, N>;
using r = ratio_pow<typename U::ratio, N>; using r = ratio_pow<typename U::ratio, N>;
return quantity<dim, downcast<detail::reference_unit<typename dim::coherent_unit::reference, r>>, Rep>(static_cast<Rep>(std::pow(q.count(), N))); return quantity<dim, downcast<scaled_unit<typename dim::coherent_unit::reference, r>>, Rep>(static_cast<Rep>(std::pow(q.count(), N)));
} }
template<typename D, typename U, typename Rep> template<typename D, typename U, typename Rep>
@@ -47,7 +47,7 @@ namespace units {
{ {
using dim = dimension_sqrt<typename U::dimension>; using dim = dimension_sqrt<typename U::dimension>;
using r = ratio_sqrt<typename U::ratio>; using r = ratio_sqrt<typename U::ratio>;
return quantity<dim, downcast<detail::reference_unit<typename dim::coherent_unit::reference, r>>, Rep>(static_cast<Rep>(std::sqrt(q.count()))); return quantity<dim, downcast<scaled_unit<typename dim::coherent_unit::reference, r>>, Rep>(static_cast<Rep>(std::sqrt(q.count())));
} }
} // namespace units } // namespace units

View File

@@ -35,7 +35,7 @@ template<typename Dim, template<typename...> typename DimTemplate>
concept DimensionOf = (Dimension<Dim> || BaseDimension<Dim>) && is_derived_from_instantiation<Dim, DimTemplate>; concept DimensionOf = (Dimension<Dim> || BaseDimension<Dim>) && is_derived_from_instantiation<Dim, DimTemplate>;
template<typename Q, template<typename...> typename DimTemplate> template<typename Q, template<typename...> typename DimTemplate>
concept QuantityOf = Quantity<Q> && is_derived_from_instantiation<Q::dimension, DimTemplate>; concept QuantityOf = Quantity<Q> && is_derived_from_instantiation<typename Q::dimension, DimTemplate>;
// ------------------------ base dimensions ----------------------------- // ------------------------ base dimensions -----------------------------

View File

@@ -22,23 +22,23 @@
#pragma once #pragma once
#include <units/dimensions/velocity.h> #include <units/physical/dimensions.h>
#include <units/physical/si/velocity.h>
namespace units { namespace units::si {
struct acceleration : derived_dimension<acceleration, exp<velocity, 1>, exp<time, -1>> {}; struct metre_per_second_sq : unit<metre_per_second_sq> {};
struct dim_acceleration : physical::dim_acceleration<dim_acceleration, metre_per_second_sq, dim_length, dim_time> {};
template<typename T> template<Unit U, Scalar Rep = double>
concept Acceleration = QuantityOf<T, acceleration>; using acceleration = quantity<dim_acceleration, U, Rep>;
struct metre_per_second_sq : coherent_derived_unit<metre_per_second_sq, acceleration> {};
inline namespace literals { inline namespace literals {
// mps_sq // mps_sq
constexpr auto operator""mps_sq(unsigned long long l) { return quantity<metre_per_second_sq, std::int64_t>(l); } constexpr auto operator""mps_sq(unsigned long long l) { return acceleration<metre_per_second_sq, std::int64_t>(l); }
constexpr auto operator""mps_sq(long double l) { return quantity<metre_per_second_sq, long double>(l); } constexpr auto operator""mps_sq(long double l) { return acceleration<metre_per_second_sq, long double>(l); }
} // namespace literals } // namespace literals
} // namespace units } // namespace units::si

View File

@@ -58,10 +58,10 @@ constexpr auto operator"" km(long double l) { return length<kilometre, long doub
} // namespace literals } // namespace literals
// US customary units // US customary units
struct yard : scaled_unit<yard, "yd", no_prefix, ratio<9'144, 10'000>, metre> {}; struct yard : named_scaled_unit<yard, "yd", no_prefix, ratio<9'144, 10'000>, metre> {};
struct foot : scaled_unit<foot, "ft", no_prefix, ratio<1, 3>, yard> {}; struct foot : named_scaled_unit<foot, "ft", no_prefix, ratio<1, 3>, yard> {};
struct inch : scaled_unit<inch, "in", no_prefix, ratio<1, 12>, foot> {}; struct inch : named_scaled_unit<inch, "in", no_prefix, ratio<1, 12>, foot> {};
struct mile : scaled_unit<mile, "mi", no_prefix, ratio<1'760>, yard> {}; struct mile : named_scaled_unit<mile, "mi", no_prefix, ratio<1'760>, yard> {};
inline namespace literals { inline namespace literals {

View File

@@ -31,8 +31,8 @@ struct second : named_unit<second, "s", prefix> {};
struct nanosecond : prefixed_unit<nanosecond, nano, second> {}; struct nanosecond : prefixed_unit<nanosecond, nano, second> {};
struct microsecond : prefixed_unit<microsecond, micro, second> {}; struct microsecond : prefixed_unit<microsecond, micro, second> {};
struct millisecond : prefixed_unit<millisecond, milli, second> {}; struct millisecond : prefixed_unit<millisecond, milli, second> {};
struct minute : scaled_unit<minute, "min", no_prefix, ratio<60>, second> {}; struct minute : named_scaled_unit<minute, "min", no_prefix, ratio<60>, second> {};
struct hour : scaled_unit<hour, "h", no_prefix, ratio<3600>, second> {}; struct hour : named_scaled_unit<hour, "h", no_prefix, ratio<3600>, second> {};
struct dim_time : physical::dim_time<second> {}; struct dim_time : physical::dim_time<second> {};

View File

@@ -74,7 +74,7 @@ struct common_quantity_impl<quantity<D, U, Rep1>, quantity<D, U, Rep2>, Rep> {
template<typename D, typename U1, typename Rep1, typename U2, typename Rep2, typename Rep> template<typename D, typename U1, typename Rep1, typename U2, typename Rep2, typename Rep>
struct common_quantity_impl<quantity<D, U1, Rep1>, quantity<D, U2, Rep2>, Rep> { struct common_quantity_impl<quantity<D, U1, Rep1>, quantity<D, U2, Rep2>, Rep> {
using type = quantity< using type = quantity<
D, downcast<detail::reference_unit<typename U1::reference, common_ratio<typename U1::ratio, typename U2::ratio>>>, D, downcast<scaled_unit<typename U1::reference, common_ratio<typename U1::ratio, typename U2::ratio>>>,
Rep>; Rep>;
}; };
@@ -154,7 +154,7 @@ template<Quantity To, typename D, typename U, typename Rep>
{ {
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>;
using ret_unit = downcast<detail::reference_unit<typename U::reference, typename To::unit::ratio>>; using ret_unit = downcast<scaled_unit<typename U::reference, typename To::unit::ratio>>;
using ret = quantity<D, ret_unit, typename To::rep>; using ret = quantity<D, ret_unit, typename To::rep>;
using cast = detail::quantity_cast_impl<ret, c_ratio, c_rep, c_ratio::num == 1, c_ratio::den == 1>; using cast = detail::quantity_cast_impl<ret, c_ratio, c_rep, c_ratio::num == 1, c_ratio::den == 1>;
return cast::cast(q); return cast::cast(q);
@@ -410,7 +410,7 @@ public:
template<class CharT, class Traits> template<class CharT, class Traits>
friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const quantity& q) friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const quantity& q)
{ {
return os; // << q.count() << " " << detail::unit_text<quantity::unit>(); return os << q.count(); // << " " << detail::unit_text<quantity::unit>(); TODO add support
} }
}; };
@@ -466,7 +466,7 @@ template<typename D1, typename U1, typename Rep1, typename D2, typename U2, type
{ {
using dim = dimension_multiply<D1, D2>; using dim = dimension_multiply<D1, D2>;
using ratio = ratio_multiply<typename U1::ratio, typename U2::ratio>; using ratio = ratio_multiply<typename U1::ratio, typename U2::ratio>;
using unit = downcast<detail::reference_unit<typename dim::coherent_unit::reference, ratio>>; using unit = detail::unit_for_dimension<dim, ratio>;
using common_rep = decltype(lhs.count() * rhs.count()); using common_rep = decltype(lhs.count() * rhs.count());
using ret = quantity<dim, unit, common_rep>; using ret = quantity<dim, unit, common_rep>;
return ret(lhs.count() * rhs.count()); return ret(lhs.count() * rhs.count());
@@ -480,7 +480,7 @@ template<typename D, Scalar Value, typename U, typename Rep>
using dim = dim_invert<D>; using dim = dim_invert<D>;
using ratio = ratio<U::ratio::den, U::ratio::num>; using ratio = ratio<U::ratio::den, U::ratio::num>;
using unit = downcast<detail::reference_unit<typename dim::coherent_unit::reference, ratio>>; using unit = detail::unit_for_dimension<dim, ratio>;
using common_rep = decltype(v / q.count()); using common_rep = decltype(v / q.count());
using ret = quantity<dim, unit, common_rep>; using ret = quantity<dim, unit, common_rep>;
return ret(v / q.count()); return ret(v / q.count());
@@ -518,8 +518,8 @@ template<typename D1, typename U1, typename Rep1, typename D2, typename U2, type
using common_rep = decltype(lhs.count() / rhs.count()); using common_rep = decltype(lhs.count() / rhs.count());
using dim = dimension_divide<D1, D2>; using dim = dimension_divide<D1, D2>;
using unit = downcast<detail::reference_unit<typename dim::coherent_unit::reference, using ratio = ratio_divide<typename U1::ratio, typename U2::ratio>;
ratio_divide<typename U1::ratio, typename U2::ratio>>>; using unit = detail::unit_for_dimension<dim, ratio>;
using ret = quantity<dim, unit, common_rep>; using ret = quantity<dim, unit, common_rep>;
return ret(lhs.count() / rhs.count()); return ret(lhs.count() / rhs.count());
} }

View File

@@ -32,31 +32,35 @@
namespace units { namespace units {
namespace detail { // scaled_unit
template<typename U, UnitRatio R>
template<typename U, Ratio R> struct scaled_unit : downcast_base<scaled_unit<U, R>> {
struct reference_unit : downcast_base<reference_unit<U, R>> {
using reference = U; using reference = U;
using ratio = R; using ratio = R;
}; };
} // namespace detail
// UnitOf // UnitOf
namespace detail {
template<typename U, typename D>
concept SameReference = std::same_as<typename U::reference, typename D::coherent_unit::reference>;
}
template<typename U, typename D> template<typename U, typename D>
concept UnitOf = concept UnitOf =
Unit<U> && Unit<U> &&
Dimension<D> && Dimension<D> &&
std::same_as<typename U::reference, typename D::coherent_unit::reference>; (std::same_as<typename U::reference, unknown_unit> || detail::SameReference<U, D>);
namespace detail { namespace detail {
// same_reference_units // same_scaled_units
template<DerivedDimension D, Unit... Us> template<DerivedDimension D, Unit... Us>
inline constexpr bool same_reference_units = false; inline constexpr bool same_scaled_units = false;
template<typename... Es, Unit... Us> template<typename... Es, Unit... Us>
inline constexpr bool same_reference_units<derived_dimension<Es...>, Us...> = (UnitOf<Us, typename Es::dimension> && ...); inline constexpr bool same_scaled_units<derived_dimension<Es...>, Us...> = (UnitOf<Us, typename Es::dimension> && ...);
// deduced_unit // deduced_unit
template<typename Result, int UnitExpNum, int UnitExpDen, typename UnitRatio> template<typename Result, int UnitExpNum, int UnitExpDen, typename UnitRatio>
@@ -91,7 +95,7 @@ struct derived_ratio<derived_dimension<E, ERest...>, U, URest...> {
template<DerivedDimension D, Unit... Us> template<DerivedDimension D, Unit... Us>
using deduced_unit = using deduced_unit =
reference_unit<typename D::coherent_unit::reference, typename detail::derived_ratio<typename D::recipe, Us...>::ratio>; scaled_unit<typename D::coherent_unit::reference, typename detail::derived_ratio<typename D::recipe, Us...>::ratio>;
} // namespace detail } // namespace detail
@@ -112,7 +116,7 @@ using deduced_unit =
* @tparam Child inherited class type used by the downcasting facility (CRTP Idiom) * @tparam Child inherited class type used by the downcasting facility (CRTP Idiom)
*/ */
template<typename Child> template<typename Child>
struct unit : downcast_child<Child, detail::reference_unit<Child, ratio<1>>> { struct unit : downcast_child<Child, scaled_unit<Child, ratio<1>>> {
static constexpr bool is_named = false; static constexpr bool is_named = false;
using prefix_type = no_prefix; using prefix_type = no_prefix;
}; };
@@ -130,7 +134,7 @@ struct unit : downcast_child<Child, detail::reference_unit<Child, ratio<1>>> {
* @tparam PT no_prefix or a type of prefix family * @tparam PT no_prefix or a type of prefix family
*/ */
template<typename Child, basic_fixed_string Symbol, PrefixType PT> template<typename Child, basic_fixed_string Symbol, PrefixType PT>
struct named_unit : downcast_child<Child, detail::reference_unit<Child, ratio<1>>> { struct named_unit : downcast_child<Child, scaled_unit<Child, ratio<1>>> {
static constexpr bool is_named = true; static constexpr bool is_named = true;
static constexpr auto symbol = Symbol; static constexpr auto symbol = Symbol;
using prefix_type = PT; using prefix_type = PT;
@@ -150,8 +154,8 @@ struct named_unit : downcast_child<Child, detail::reference_unit<Child, ratio<1>
* @tparam R a scale to apply to U * @tparam R a scale to apply to U
* @tparam U a reference unit to scale * @tparam U a reference unit to scale
*/ */
template<typename Child, basic_fixed_string Symbol, PrefixType PT, Ratio R, Unit U> template<typename Child, basic_fixed_string Symbol, PrefixType PT, UnitRatio R, Unit U>
struct scaled_unit : downcast_child<Child, detail::reference_unit<typename U::reference, ratio_multiply<R, typename U::ratio>>> { struct named_scaled_unit : downcast_child<Child, scaled_unit<typename U::reference, ratio_multiply<R, typename U::ratio>>> {
static constexpr bool is_named = true; static constexpr bool is_named = true;
static constexpr auto symbol = Symbol; static constexpr auto symbol = Symbol;
using prefix_type = PT; using prefix_type = PT;
@@ -171,11 +175,11 @@ struct scaled_unit : downcast_child<Child, detail::reference_unit<typename U::re
template<typename Child, Prefix P, Unit U> template<typename Child, Prefix P, Unit U>
requires std::same_as<typename P::prefix_type, typename U::prefix_type> requires std::same_as<typename P::prefix_type, typename U::prefix_type>
// TODO replace with the below code when gcc will stop to crash on it ;-) // TODO replace with the below code when gcc will stop to crash on it ;-)
// struct prefixed_unit : scaled_unit<Child, P::symbol + U::symbol, typename P::prefix_type, // struct prefixed_unit : named_scaled_unit<Child, P::symbol + U::symbol, typename P::prefix_type,
// ratio_multiply<typename P::ratio, typename U::ratio>, // ratio_multiply<typename P::ratio, typename U::ratio>,
// typename U::reference> {}; // typename U::reference> {};
struct prefixed_unit : struct prefixed_unit :
downcast_child<Child, detail::reference_unit<typename U::reference, ratio_multiply<typename P::ratio, typename U::ratio>>> { downcast_child<Child, scaled_unit<typename U::reference, ratio_multiply<typename P::ratio, typename U::ratio>>> {
static constexpr bool is_named = true; static constexpr bool is_named = true;
static constexpr auto symbol = P::symbol + U::symbol; static constexpr auto symbol = P::symbol + U::symbol;
using prefix_type = P::prefix_type; using prefix_type = P::prefix_type;
@@ -194,7 +198,7 @@ struct prefixed_unit :
* @tparam URest the units for the rest of dimensions from the recipe * @tparam URest the units for the rest of dimensions from the recipe
*/ */
template<typename Child, DerivedDimension Dim, Unit U, Unit... URest> template<typename Child, DerivedDimension Dim, Unit U, Unit... URest>
requires detail::same_reference_units<typename Dim::recipe, U, URest...> && requires detail::same_scaled_units<typename Dim::recipe, U, URest...> &&
(U::is_named && (URest::is_named && ... && true)) (U::is_named && (URest::is_named && ... && true))
struct deduced_unit : downcast_child<Child, detail::deduced_unit<Dim, U, URest...>> { struct deduced_unit : downcast_child<Child, detail::deduced_unit<Dim, U, URest...>> {
static constexpr bool is_named = false; static constexpr bool is_named = false;

View File

@@ -131,7 +131,7 @@ namespace units {
struct dim_invert<dimension<Es...>> : std::type_identity<downcast_traits_t<dimension<exp_invert_t<Es>...>>> {}; struct dim_invert<dimension<Es...>> : std::type_identity<downcast_traits_t<dimension<exp_invert_t<Es>...>>> {};
template<Dimension D> template<Dimension D>
using dim_invert_t = dim_invert<typename D::base_type>::type; using dim_invert_t = dim_invert<typename D::downcast_base_type>::type;
// make_dimension // make_dimension
@@ -196,7 +196,7 @@ namespace units {
struct dimension_multiply<dimension<E1...>, dimension<E2...>> : std::type_identity<downcast_traits_t<merge_dimension_t<dimension<E1...>, dimension<E2...>>>> {}; struct dimension_multiply<dimension<E1...>, dimension<E2...>> : std::type_identity<downcast_traits_t<merge_dimension_t<dimension<E1...>, dimension<E2...>>>> {};
template<Dimension D1, Dimension D2> template<Dimension D1, Dimension D2>
using dimension_multiply_t = dimension_multiply<typename D1::base_type, typename D2::base_type>::type; using dimension_multiply_t = dimension_multiply<typename D1::downcast_base_type, typename D2::downcast_base_type>::type;
// dimension_divide // dimension_divide
@@ -209,6 +209,6 @@ namespace units {
}; };
template<Dimension D1, Dimension D2> template<Dimension D1, Dimension D2>
using dimension_divide_t = dimension_divide<typename D1::base_type, typename D2::base_type>::type; using dimension_divide_t = dimension_divide<typename D1::downcast_base_type, typename D2::downcast_base_type>::type;
} // namespace units } // namespace units

View File

@@ -131,7 +131,7 @@ namespace units {
struct dim_invert<dimension<Es...>> : std::type_identity<downcast_traits_t<dimension<exp_invert_t<Es>...>>> {}; struct dim_invert<dimension<Es...>> : std::type_identity<downcast_traits_t<dimension<exp_invert_t<Es>...>>> {};
template<Dimension D> template<Dimension D>
using dim_invert_t = dim_invert<typename D::base_type>::type; using dim_invert_t = dim_invert<typename D::downcast_base_type>::type;
// make_dimension // make_dimension
@@ -196,7 +196,7 @@ namespace units {
struct dimension_multiply<dimension<E1...>, dimension<E2...>> : std::type_identity<downcast_traits_t<merge_dimension_t<dimension<E1...>, dimension<E2...>>>> {}; struct dimension_multiply<dimension<E1...>, dimension<E2...>> : std::type_identity<downcast_traits_t<merge_dimension_t<dimension<E1...>, dimension<E2...>>>> {};
template<Dimension D1, Dimension D2> template<Dimension D1, Dimension D2>
using dimension_multiply_t = dimension_multiply<typename D1::base_type, typename D2::base_type>::type; using dimension_multiply_t = dimension_multiply<typename D1::downcast_base_type, typename D2::downcast_base_type>::type;
// dimension_divide // dimension_divide
@@ -209,6 +209,6 @@ namespace units {
}; };
template<Dimension D1, Dimension D2> template<Dimension D1, Dimension D2>
using dimension_divide_t = dimension_divide<typename D1::base_type, typename D2::base_type>::type; using dimension_divide_t = dimension_divide<typename D1::downcast_base_type, typename D2::downcast_base_type>::type;
} // namespace units } // namespace units

View File

@@ -102,7 +102,7 @@ namespace units {
struct dim_invert<dimension<Es...>> : std::type_identity<downcast_traits_t<dimension<exp_invert_t<Es>...>>> {}; struct dim_invert<dimension<Es...>> : std::type_identity<downcast_traits_t<dimension<exp_invert_t<Es>...>>> {};
template<typename D> template<typename D>
using dim_invert_t = dim_invert<typename D::base_type>::type; using dim_invert_t = dim_invert<typename D::downcast_base_type>::type;
// make_dimension // make_dimension
@@ -167,7 +167,7 @@ namespace units {
struct dimension_multiply<dimension<E1...>, dimension<E2...>> : std::type_identity<downcast_traits_t<merge_dimension_t<dimension<E1...>, dimension<E2...>>>> {}; struct dimension_multiply<dimension<E1...>, dimension<E2...>> : std::type_identity<downcast_traits_t<merge_dimension_t<dimension<E1...>, dimension<E2...>>>> {};
template<typename D1, typename D2> template<typename D1, typename D2>
using dimension_multiply_t = dimension_multiply<typename D1::base_type, typename D2::base_type>::type; using dimension_multiply_t = dimension_multiply<typename D1::downcast_base_type, typename D2::downcast_base_type>::type;
// dimension_divide // dimension_divide
@@ -180,6 +180,6 @@ namespace units {
}; };
template<typename D1, typename D2> template<typename D1, typename D2>
using dimension_divide_t = dimension_divide<typename D1::base_type, typename D2::base_type>::type; using dimension_divide_t = dimension_divide<typename D1::downcast_base_type, typename D2::downcast_base_type>::type;
} // namespace units } // namespace units

View File

@@ -29,18 +29,18 @@ namespace units {
template<typename BaseType> template<typename BaseType>
struct downcast_base { struct downcast_base {
using base_type = BaseType; using downcast_base_type = BaseType;
}; };
template<typename T> template<typename T>
concept Downcastable = concept Downcastable =
requires { requires {
typename T::base_type; typename T::downcast_base_type;
} && } &&
std::derived_from<T, downcast_base<typename T::base_type>>; std::derived_from<T, downcast_base<typename T::downcast_base_type>>;
template<Downcastable T> template<Downcastable T>
using downcast_base_t = T::base_type; using downcast_base_t = T::downcast_base_type;
template<Downcastable T> template<Downcastable T>
struct downcast_traits : std::type_identity<T> {}; struct downcast_traits : std::type_identity<T> {};

View File

@@ -29,11 +29,11 @@ namespace units {
template<typename BaseType> template<typename BaseType>
struct downcast_base { struct downcast_base {
using base_type = BaseType; using downcast_base_type = BaseType;
}; };
template<typename T> template<typename T>
using downcast_base_t = T::base_type; using downcast_base_t = T::downcast_base_type;
template<typename T> template<typename T>
struct downcast_traits : std::type_identity<T> {}; struct downcast_traits : std::type_identity<T> {};

View File

@@ -109,10 +109,10 @@ using namespace units::si;
// class invariants // class invariants
// constexpr quantity<dim_length, metre, int> q(1); // should not compile // constexpr quantity<si::dim_length, second, int> error(0); // should not compile (unit of a different dimension)
// constexpr quantity<length, metre, quantity<metre, int>> error(0m); // should trigger a static_assert // constexpr quantity<si::dim_length, metre, quantity<si::dim_length, metre, int>> error(0); // should not compile (quantity used as Rep)
// constexpr quantity<int, int, double> error(0); // should trigger a static_assert // constexpr quantity<metre, si::dim_length, double> error(0); // should not compile (reordered arguments)
// constexpr quantity<length, unit<length, std::ratio<-1, 1>>, int> error(0); // should trigger a static_assert // constexpr quantity<si::dim_length, scaled_unit<metre, ratio<-1, 1>>, int> error(0); // should not compile (negative unit ratio)
// member types // member types
@@ -134,9 +134,9 @@ static_assert(length<metre, int>(km).count() == km.count());
static_assert(length<metre, int>(1).count() == 1); static_assert(length<metre, int>(1).count() == 1);
static_assert(length<metre, int>(my_value(1)).count() == 1); static_assert(length<metre, int>(my_value(1)).count() == 1);
static_assert(length<metre, my_int>(1).count() == my_int{1}); static_assert(length<metre, my_int>(1).count() == my_int{1});
// static_assert(length<metre, int>(1.0).count() == 1); // should not compile // static_assert(length<metre, int>(1.0).count() == 1); // should not compile (truncating conversion)
// static_assert(length<metre, int>(my_value(1.0)).count() == 1); // should not compile // static_assert(length<metre, int>(my_value(1.0)).count() == 1); // should not compile (truncating conversion)
// static_assert(length<metre, my_value>(1.0).count() == 1); // should not compile // static_assert(length<metre, my_int>(1.0).count() == my_int{1}); // should not compile (truncating conversion)
static_assert(length<metre, double>(1.0).count() == 1.0); static_assert(length<metre, double>(1.0).count() == 1.0);
static_assert(length<metre, double>(my_value(1.0)).count() == 1.0); static_assert(length<metre, double>(my_value(1.0)).count() == 1.0);
static_assert(length<metre, double>(1).count() == 1.0); static_assert(length<metre, double>(1).count() == 1.0);
@@ -147,27 +147,23 @@ static_assert(length<metre, my_double>(1).count() == my_double{1.0});
static_assert(length<metre, my_double>(3.14).count() == my_double{3.14}); static_assert(length<metre, my_double>(3.14).count() == my_double{3.14});
static_assert(length<metre, int>(km).count() == 1000); static_assert(length<metre, int>(km).count() == 1000);
// static_assert(length<metre, int>(length<metre, double>(3.14)).count() == 3); // should not compile // static_assert(length<metre, int>(length<metre, double>(3.14)).count() == 3); // should not compile (truncating conversion)
static_assert(length<metre, int>(quantity_cast<length<metre, my_int>>(3.14m)).count() == 3); static_assert(length<metre, int>(quantity_cast<length<metre, my_int>>(3.14m)).count() == 3);
// static_assert(length<metre, int>(length<metre, my_double>(1000.0)).count() == 1000); // should not compile // static_assert(length<metre, int>(length<metre, my_double>(1000.0)).count() == 1000); // should not compile (truncating conversion)
// static_assert(length<metre, my_value>(1000.0m).count() == 1000); // should not compile // static_assert(length<metre, my_int>(1000.0m).count() == my_int{1000}); // should not compile (truncating conversion)
static_assert(length<metre, double>(1000.0m).count() == 1000.0); static_assert(length<metre, double>(1000.0m).count() == 1000.0);
static_assert(length<metre, double>(length<metre, my_double>(1000.0)).count() == 1000.0); static_assert(length<metre, double>(length<metre, my_double>(1000.0)).count() == 1000.0);
static_assert(length<metre, my_double>(1000.0m).count() == my_double{1000.0}); static_assert(length<metre, my_double>(1000.0m).count() == my_double{1000.0});
static_assert(length<metre, double>(km).count() == 1000.0); static_assert(length<metre, double>(km).count() == 1000.0);
static_assert(length<metre, my_double>(km).count() == my_double{1000.0}); static_assert(length<metre, my_double>(km).count() == my_double{1000.0});
static_assert(length<metre, int>(1km).count() == 1000); static_assert(length<metre, int>(1km).count() == 1000);
// static_assert(length<metre, int>(1_s).count() == 1); // should not compile // static_assert(length<metre, int>(1s).count() == 1); // should not compile (different dimensions)
// static_assert(length<kilometre, int>(1010m).count() == 1); // should not compile //static_assert(length<kilometre, int>(1010m).count() == 1); // should not compile (truncating conversion)
static_assert(length<kilometre, int>(quantity_cast<length<kilometre, my_int>>(1010m)).count() == 1); static_assert(length<kilometre, int>(quantity_cast<length<kilometre, my_int>>(1010m)).count() == 1);
// assignment operator // assignment operator
static_assert([]() { static_assert([]() { length<metre, int> l1(1), l2(2); return l2 = l1; }().count() == 1);
length<metre, int> l1(1), l2(2);
return l2 = l1;
}()
.count() == 1);
// static member functions // static member functions
@@ -218,13 +214,13 @@ static_assert((1m *= 2).count() == 2);
static_assert((2m /= 2).count() == 1); static_assert((2m /= 2).count() == 1);
static_assert((7m %= 2).count() == 1); static_assert((7m %= 2).count() == 1);
static_assert((7m %= 2m).count() == 1); static_assert((7m %= 2m).count() == 1);
// static_assert((7.m %= 2.).count() == 1); // should not compile // static_assert((7.m %= 2.).count() == 1); // should not compile (operation not allowed for floating-point types)
// static_assert((7.m %= 2).count() == 1); // should not compile // static_assert((7.m %= 2).count() == 1); // should not compile (operation not allowed for floating-point types)
// static_assert((7m %= 2.).count() == 1); // should not compile // static_assert((7m %= 2.).count() == 1); // should not compile (operation not allowed for floating-point types)
static_assert((7m %= 2m).count() == 1); static_assert((7m %= 2m).count() == 1);
// static_assert((7.m %= 2.m).count() == 1); // should not compile // static_assert((7.m %= 2.m).count() == 1); // should not compile (operation not allowed for floating-point types)
// static_assert((7.m %= 2m).count() == 1); // should not compile // static_assert((7.m %= 2m).count() == 1); // should not compile (operation not allowed for floating-point types)
// static_assert((7m %= 2.m).count() == 1); // should not compile // static_assert((7m %= 2.m).count() == 1); // should not compile (operation not allowed for floating-point types)
// non-member arithmetic operators // non-member arithmetic operators
@@ -239,13 +235,27 @@ static_assert(std::is_same_v<decltype(length<metre, int>() * 1.0), length<metre,
static_assert(std::is_same_v<decltype(1.0 * length<metre, int>()), length<metre, double>>); static_assert(std::is_same_v<decltype(1.0 * length<metre, int>()), length<metre, double>>);
static_assert( static_assert(
std::is_same_v<decltype(velocity<metre_per_second, int>() * si::time<second, int>()), length<metre, int>>); std::is_same_v<decltype(velocity<metre_per_second, int>() * si::time<second, int>()), length<metre, int>>);
static_assert(
std::is_same_v<decltype(velocity<metre_per_second, int>() * si::time<hour, int>()), length<scaled_unit<metre, ratio<3600>>, int>>);
// TODO uncomment below when fixed in gcc
// static_assert(std::is_same_v<decltype(length<metre>() * si::time<minute>()),
// quantity<derived_dimension<exp<dim_length, 1>, exp<dim_time, 1>>, scaled_unit<unknown_unit, ratio<60>>>>);
static_assert(std::is_same_v<decltype(1 / si::time<second, int>()), frequency<hertz, int>>); static_assert(std::is_same_v<decltype(1 / si::time<second, int>()), frequency<hertz, int>>);
static_assert(std::is_same_v<decltype(1 / si::time<minute, int>()), frequency<scaled_unit<hertz, ratio<1, 60>>, int>>);
static_assert(std::is_same_v<decltype(1 / frequency<hertz, int>()), si::time<second, int>>); static_assert(std::is_same_v<decltype(1 / frequency<hertz, int>()), si::time<second, int>>);
// TODO uncomment below when fixed in gcc
// static_assert(std::is_same_v<decltype(1 / length<kilometre>()),
// quantity<derived_dimension<exp<dim_length, -1>>, scaled_unit<unknown_unit, ratio<1, 1000>>>>);
static_assert(std::is_same_v<decltype(length<metre, int>() / 1.0), length<metre, double>>); static_assert(std::is_same_v<decltype(length<metre, int>() / 1.0), length<metre, double>>);
static_assert(std::is_same_v<decltype(length<metre, int>() / length<metre, double>()), double>); static_assert(std::is_same_v<decltype(length<metre, int>() / length<metre, double>()), double>);
static_assert(std::is_same_v<decltype(length<kilometre, int>() / length<metre, double>()), double>); static_assert(std::is_same_v<decltype(length<kilometre, int>() / length<metre, double>()), double>);
static_assert( static_assert(
std::is_same_v<decltype(length<metre, int>() / si::time<second, int>()), velocity<metre_per_second, int>>); std::is_same_v<decltype(length<metre, int>() / si::time<second, int>()), velocity<metre_per_second, int>>);
static_assert(
std::is_same_v<decltype(length<metre>() / si::time<minute>()), velocity<scaled_unit<metre_per_second, ratio<1, 60>>>>);
// TODO uncomment below when fixed in gcc
// static_assert(std::is_same_v<decltype(si::time<minute>() / length<metre>()),
// quantity<derived_dimension<exp<dim_length, -1>, exp<dim_time, 1>>, scaled_unit<unknown_unit, ratio<60>>>>);
static_assert(std::is_same_v<decltype(length<metre, int>() % short(1)), length<metre, int>>); static_assert(std::is_same_v<decltype(length<metre, int>() % short(1)), length<metre, int>>);
static_assert(std::is_same_v<decltype(length<metre, int>() % length<metre, short>(1)), length<metre, int>>); static_assert(std::is_same_v<decltype(length<metre, int>() % length<metre, short>(1)), length<metre, int>>);
@@ -307,15 +317,18 @@ static_assert(std::is_same_v<common_quantity<length<kilometre, long long>, lengt
// quantity_cast // quantity_cast
static_assert(std::is_same_v<decltype(quantity_cast<detail::reference_unit<metre, ratio<1>>>(2km))::unit, metre>); static_assert(std::is_same_v<decltype(quantity_cast<scaled_unit<metre, ratio<1>>>(2km))::unit, metre>);
// static_assert(quantity_cast<int>(2km).count() == 2000); // should not compile
static_assert(quantity_cast<length<metre, int>>(2km).count() == 2000); static_assert(quantity_cast<length<metre, int>>(2km).count() == 2000);
static_assert(quantity_cast<length<kilometre, int>>(2000m).count() == 2); static_assert(quantity_cast<length<kilometre, int>>(2000m).count() == 2);
static_assert(quantity_cast<length<metre, int>>(1.23m).count() == 1);
static_assert(quantity_cast<metre>(2km).count() == 2000);
static_assert(quantity_cast<kilometre>(2000m).count() == 2);
static_assert(quantity_cast<int>(1.23m).count() == 1);
// time // time
// static_assert(1s == 1m); // should not compile // static_assert(1s == 1m); // should not compile (different dimensions)
static_assert(1h == 3600s); static_assert(1h == 3600s);
// length // length

View File

@@ -30,23 +30,23 @@ using namespace units;
struct metre : named_unit<metre, "m", si::prefix> {}; struct metre : named_unit<metre, "m", si::prefix> {};
struct centimetre : prefixed_unit<centimetre, si::centi, metre> {}; struct centimetre : prefixed_unit<centimetre, si::centi, metre> {};
struct kilometre : prefixed_unit<kilometre, si::kilo, metre> {}; struct kilometre : prefixed_unit<kilometre, si::kilo, metre> {};
struct yard : scaled_unit<yard, "yd", no_prefix, ratio<9'144, 10'000>, metre> {}; struct yard : named_scaled_unit<yard, "yd", no_prefix, ratio<9'144, 10'000>, metre> {};
struct foot : scaled_unit<foot, "ft", no_prefix, ratio<1, 3>, yard> {}; struct foot : named_scaled_unit<foot, "ft", no_prefix, ratio<1, 3>, yard> {};
struct dim_length : base_dimension<"length", metre> {}; struct dim_length : base_dimension<"length", metre> {};
struct second : named_unit<second, "s", si::prefix> {}; struct second : named_unit<second, "s", si::prefix> {};
struct hour : scaled_unit<hour, "h", no_prefix, ratio<3600>, second> {}; struct hour : named_scaled_unit<hour, "h", no_prefix, ratio<3600>, second> {};
struct dim_time : base_dimension<"time", second> {}; struct dim_time : base_dimension<"time", second> {};
struct metre_per_second : unit<metre_per_second> {}; struct metre_per_second : unit<metre_per_second> {};
struct dim_velocity : derived_dimension<dim_velocity, metre_per_second, exp<dim_length, 1>, exp<dim_time, -1>> {}; struct dim_velocity : derived_dimension<dim_velocity, metre_per_second, exp<dim_length, 1>, exp<dim_time, -1>> {};
struct kilometre_per_hour : deduced_unit<kilometre_per_hour, dim_velocity, kilometre, hour> {}; struct kilometre_per_hour : deduced_unit<kilometre_per_hour, dim_velocity, kilometre, hour> {};
static_assert(std::is_same_v<downcast<detail::reference_unit<metre, ratio<1>>>, metre>); static_assert(std::is_same_v<downcast<scaled_unit<metre, ratio<1>>>, metre>);
static_assert(std::is_same_v<downcast<detail::reference_unit<metre, ratio<1, 100>>>, centimetre>); static_assert(std::is_same_v<downcast<scaled_unit<metre, ratio<1, 100>>>, centimetre>);
static_assert(std::is_same_v<downcast<detail::reference_unit<metre, ratio<yard::ratio::num, yard::ratio::den>>>, yard>); static_assert(std::is_same_v<downcast<scaled_unit<metre, ratio<yard::ratio::num, yard::ratio::den>>>, yard>);
static_assert(std::is_same_v<downcast<detail::reference_unit<metre, ratio_multiply<typename yard::ratio, ratio<1, 3>>>>, foot>); static_assert(std::is_same_v<downcast<scaled_unit<metre, ratio_multiply<typename yard::ratio, ratio<1, 3>>>>, foot>);
static_assert(std::is_same_v<downcast<detail::reference_unit<metre_per_second, ratio_divide<typename kilometre::ratio, typename hour::ratio>>>, kilometre_per_hour>); static_assert(std::is_same_v<downcast<scaled_unit<metre_per_second, ratio_divide<typename kilometre::ratio, typename hour::ratio>>>, kilometre_per_hour>);
static_assert(centimetre::symbol == "cm"); static_assert(centimetre::symbol == "cm");
static_assert(kilometre::symbol == "km"); static_assert(kilometre::symbol == "km");