-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.cpp
54 lines (49 loc) · 1.66 KB
/
runtime.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <benri/si/base.h>
#include <iostream>
#include <string>
#include <variant>
// Define a variant holding the quantities.
using si_unit = std::variant<benri::quantity<benri::si::kilogram_t>,
benri::quantity<benri::si::metre_t>,
benri::quantity<benri::si::second_t>>;
// Define a runtime function.
auto parse(const std::string& input)
{
if (input == "kilogram")
return si_unit(benri::quantity<benri::si::kilogram_t>{1});
else if (input == "metre")
return si_unit(benri::quantity<benri::si::metre_t>{1});
else if (input == "second")
return si_unit(benri::quantity<benri::si::second_t>{1});
else
throw;
}
// Define another runtime function.
auto print(si_unit value)
{
std::cout << "You entered: ";
std::visit(
[](auto&& arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same<T, benri::quantity<benri::si::kilogram_t>>::value)
std::cout << "kilogram\n" << std::flush;
else if constexpr (std::is_same<T,
benri::quantity<benri::si::metre_t>>::value)
std::cout << "metre\n" << std::flush;
else if constexpr (std::is_same<T,
benri::quantity<benri::si::second_t>>::value)
std::cout << "second\n" << std::flush;
else
throw;
},
value);
}
int main()
{
// Get runtime quantity from user input.
std::string input;
std::cin >> input;
auto value = parse(input);
// Print unit of the quantity.
print(value);
}