Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah_cpp_compat_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 https://github.com/lrleon/Aleph-w
6
7 This file is part of Aleph-w library
8
9 Copyright (c) 2002-2026 Leandro Rabindranath Leon
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28*/
29
45#include <iostream>
46#include <limits>
47#include <string>
48
49#include <ah-cpp-compat.H>
50
51using Aleph::expected;
52// Not `using Aleph::unexpected`: the Windows UCRT declares a global
53// ::unexpected() (the C++98 std::unexpected), which that declaration would
54// conflict with. The uses below are qualified instead.
55
56namespace
57{
58
59// A parse-like operation: turn a string into a positive int, or explain why
60// it could not. No exceptions cross the boundary — the error is a value.
61expected<int, std::string> parse_positive(const std::string &text)
62{
63 if (text.empty())
64 return Aleph::unexpected<std::string>("empty input");
65
66 int value = 0;
67 for (char c : text)
68 {
69 if (c < '0' or c > '9')
70 return Aleph::unexpected<std::string>("not a number: '" + text + "'");
71
72 int digit = c - '0';
73 constexpr int max_val = std::numeric_limits<int>::max();
74 if (value > (max_val - digit) / 10)
75 return Aleph::unexpected<std::string>("overflow: '" + text + "'");
76
77 value = value * 10 + digit;
78 }
79
80 if (value == 0)
81 return Aleph::unexpected<std::string>("must be > 0");
82
83 return value;
84}
85
86void report(const std::string &input)
87{
88 auto r = parse_positive(input);
89 std::cout << " parse_positive(\"" << input << "\") -> ";
90 if (r) // explicit operator bool
91 std::cout << "value " << *r << '\n';
92 else
93 std::cout << "error: " << r.error() << '\n';
94}
95
96} // namespace
97
98int main()
99{
100 std::cout << "C++23 library facilities detected on this toolchain:\n"
101 << " std::expected = " << ALEPH_HAS_STD_EXPECTED << '\n'
102 << " std::generator = " << ALEPH_HAS_STD_GENERATOR << '\n'
103 << " std::flat_map = " << ALEPH_HAS_STD_FLAT_MAP << '\n'
104 << " std::mdspan = " << ALEPH_HAS_STD_MDSPAN << "\n\n";
105
106 std::cout << "Basic value / error reporting:\n";
107 report("42");
108 report("7x");
109 report("0");
110 report("");
111
112 std::cout << "\nvalue_or (supply a fallback instead of branching):\n";
113 std::cout << " parse_positive(\"100\").value_or(-1) = "
114 << parse_positive("100").value_or(-1) << '\n';
115 std::cout << " parse_positive(\"bad\").value_or(-1) = "
116 << parse_positive("bad").value_or(-1) << '\n';
117
118 std::cout << "\nMonadic chaining (transform / and_then / or_else):\n";
119
120 // transform: apply a pure function to the value, propagate errors untouched.
121 auto squared = parse_positive("9").transform([](int v) { return v * v; });
122 std::cout << " \"9\" |> transform(square) = "
123 << (squared ? std::to_string(*squared) : squared.error()) << '\n';
124
125 // and_then: chain another fallible step.
126 auto chained = parse_positive("5").and_then(
127 [](int v) -> expected<int, std::string>
128 {
129 if (v > 1000)
130 return Aleph::unexpected<std::string>("too large");
131 return v + 100;
132 });
133 std::cout << " \"5\" |> and_then(+100 if <=1000) = "
134 << (chained ? std::to_string(*chained) : chained.error()) << '\n';
135
136 // or_else: recover from an error with a default.
137 auto recovered = parse_positive("oops").or_else(
138 [](const std::string &) -> expected<int, std::string> { return 0; });
139 std::cout << " \"oops\" |> or_else(0) = "
140 << (recovered ? std::to_string(*recovered) : recovered.error())
141 << '\n';
142
143 return 0;
144}
C++20/23 compatibility layer: feature detection + Aleph:: polyfills.
#define ALEPH_HAS_STD_FLAT_MAP
#define ALEPH_HAS_STD_EXPECTED
#define ALEPH_HAS_STD_GENERATOR
#define ALEPH_HAS_STD_MDSPAN
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Minimal std::expected-style result type for C++20.
Wrapper for the error value used to initialize an expected.
Gsl_Compatible_Rng r