-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeters_and_inches.cpp
39 lines (34 loc) · 1.12 KB
/
meters_and_inches.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "../../CtUnits/CtUnits.hpp"
#include <iostream>
#include <type_traits>
int main()
{
struct Meter
{
};
struct Inch
{
};
using value_type = double;
using DistanceInMeters =
ctu::Quantity<value_type, ctu::UdMap<ctu::UnitDimension<Meter, 1>>>;
using DistanceInInches =
ctu::Quantity<value_type, ctu::UdMap<ctu::UnitDimension<Inch, 1>>>;
const auto meter_to_inch = ctu::Quantity<
value_type,
ctu::UdMap<ctu::UnitDimension<Inch, -1>, ctu::UnitDimension<Meter, 1>>>(
0.0254);
const auto distance_in_meters = DistanceInMeters(123.0);
const DistanceInInches distance_in_inches =
distance_in_meters / meter_to_inch;
// note that
static_assert(
!std::is_same_v<
DistanceInInches, decltype(distance_in_meters * meter_to_inch)>);
// therefore
// const DistanceInInches distance_in_inches =
// distance_in_meters*meter_to_inch; would lead to a compile-time error
std::cout << distance_in_meters.get_value() << " meters is "
<< distance_in_inches.get_value() << " inches\n";
return 0;
}