|
1 | 1 | #include <iostream> |
| 2 | +#include <limits> |
2 | 3 |
|
3 | 4 | import mcpplibs.primitives; |
4 | 5 |
|
5 | 6 | int main() { |
6 | 7 | using namespace mcpplibs::primitives; |
7 | 8 | using namespace mcpplibs::primitives::policy; |
| 9 | + using namespace mcpplibs::primitives::operators; |
| 10 | + using namespace mcpplibs::primitives::types; |
8 | 11 |
|
9 | | - std::cout << "=== mcpplibs.primitives traits & policy example ===\n"; |
10 | | - std::cout << std::boolalpha; |
11 | | - std::cout << "int is std_integer: " << std_integer<int> << "\n"; |
12 | | - std::cout << "double is std_floating: " << std_floating<double> << "\n"; |
13 | | - std::cout << "int is underlying_type: " << underlying_type<int> << "\n"; |
14 | | - |
15 | | - std::cout |
16 | | - << "default value policy is unchecked_value: " |
17 | | - << std::is_same_v<default_value, checked_value> << "\n"; |
18 | | - |
19 | | - std::cout << "checked_value is a policy_type: " |
20 | | - << policy_type<checked_value> << "\n"; |
21 | | - std::cout << "checked_value category == value: " |
22 | | - << (policy::traits<checked_value>::kind == category::value) |
| 12 | + // Operators |
| 13 | + I32<> a{3}; |
| 14 | + I32<> b{4}; |
| 15 | + auto c = a + b; // primitive<int> |
| 16 | + std::cout << "3 + 4 = " << static_cast<int>(c) << "\n"; |
| 17 | + |
| 18 | + // Error handling |
| 19 | + I32<checked_value> lhs{1}; |
| 20 | + I32<checked_value> rhs{std::numeric_limits<std::int32_t>::max()}; |
| 21 | + |
| 22 | + try { |
| 23 | + auto res = lhs + rhs; |
| 24 | + } catch (const std::exception &e) { |
| 25 | + std::cout << "An exception occurred: " << e.what() << "\n"; |
| 26 | + } |
| 27 | + |
| 28 | + I64<checked_value, expected_error> lhs2{1}; |
| 29 | + I64<checked_value, expected_error> rhs2{std::numeric_limits<std::int64_t>::max()}; |
| 30 | + |
| 31 | + auto res2 = lhs2 + rhs2; |
| 32 | + |
| 33 | + // Saturating |
| 34 | + I32<saturating_value> s1{std::numeric_limits<std::int32_t>::max()}; |
| 35 | + I32<saturating_value> s2{1}; |
| 36 | + auto sat = s1 + s2; // saturating -> stays max |
| 37 | + std::cout << "saturating max + 1 = " << static_cast<std::int32_t>(sat) |
23 | 38 | << "\n"; |
24 | 39 |
|
| 40 | + // Mixed-type addition |
| 41 | + using expected_type = I64<category_compatible_type>; |
| 42 | + expected_type L1{5}; |
| 43 | + I32<category_compatible_type> L2{6}; |
| 44 | + auto mix = L1 + L2; // common_type -> I64 |
| 45 | + std::cout << "5 + 6 = " << mix.value() << "\n"; |
| 46 | + std::cout << std::boolalpha; |
| 47 | + std::cout << "Does type of mix is I64<category_compatible_type>: " << std::same_as<decltype(mix), expected_type> << std::endl; |
| 48 | + |
25 | 49 | return 0; |
26 | 50 | } |
0 commit comments