Skip to content

Commit 02db7b4

Browse files
committed
Fix not saving the readme.md
1 parent 767a790 commit 02db7b4

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,42 +160,43 @@ A range adaptor that takes a range of `Result<T, E>` and returns a view containi
160160

161161
## Exception-Based Error Handling
162162

163-
When exceptions are enabled (i.e. `_CPPUNWIND`, `__EXCEPTIONS`, or `__cpp_exceptions` are defined), cppmatch also provides exception-based alternatives to handle error propagation and pattern matching.
163+
When exceptions are enabled cppmatch also provides exception-based alternatives to handle error propagation and pattern matching, which supports MSVC
164164

165-
### `expect_e(expr)`
166-
The function `expect_e` is an alternative to the `expect` macro. It evaluates an expression that returns a `Result<T, E>`. If the result is an error, it uses pattern matching to throw the contained error as an exception. Otherwise, it returns the success value.
165+
### `expect_e(expr)`
166+
The **function** `expect_e` is an alternative to the **macro** `expect`. It evaluates an expression that returns a `Result<T, E>`. If the result is an error, it uses pattern matching to throw the contained error as an exception. Otherwise, it returns the success value.
167+
168+
The try...catch will be generated dynamically with the macro **match_e**, surrounding the expression between try/catch is not necessary
167169

168170
- **Example:**
169171
```cpp
170-
cppmatch::Result<int, std::string> parse_int(std::string_view input) {
172+
cppmatch::Result<int, Error<std::string>> parse_int(std::string_view input) {
171173
// Returns either a valid int or an error string.
172174
}
175+
176+
int value = expect_e(parse_int("123"));
173177

174-
try {
175-
int value = cppmatch::expect_e(parse_int("123"));
176-
// Proceed with the valid value.
177-
} catch (const std::string &err) {
178-
// Handle the error.
179-
std::cerr << "Parsing failed: " << err << '\n';
180-
}
181178
```
182179
183-
### `match_e(EXPR, ...)`
180+
### `match_e(EXPR, Lambdas...)`
184181
The `match_e` macro allows you to perform pattern matching on the result of a function call that returns a `Result`. Unlike `match`, it supports exceptions: if the function call throws an exception (via `expect_e` or otherwise), `match_e` catches it and applies the provided lambdas to handle the exception.
185182
183+
match_e will **AUTOMATICALLY** surround the expression EXPR in a try...catch block that handles all the Error types returned by EXPR.
184+
185+
If an error is thrown that was not specified in match_e, it will be rethrown.
186+
If only using cpp-match, this is minimized because in match_e you have to exhaustively pass a callable to all possible variant returns of parse_int.
187+
186188
- **Usage Details:**
187189
- **EXPR:** Must be a function call expression (not an lvalue), as enforced by a static assertion.
188190
- **Lambdas:** A set of lambdas to match the success or error cases.
189191
- **Note:** This macro is only available when exceptions are enabled.
190192
191193
- **Example:**
192194
```cpp
193-
auto result_message = cppmatch::match_e(parse_int("abc"),
195+
std::string result_message = match_e(parse_int("abc"),
194196
[](int value) { return "Parsed value: " + std::to_string(value); },
195-
[](const std::string &err) { return "Error: " + err; }
197+
[](auto&&) { return "An error has occured." }
196198
);
197-
// If parse_int("abc") returns an error, the error lambda is executed.
198-
// Otherwise, the success lambda is executed.
199+
199200
```
200201

201202
Under the hood, `match_e` wraps the function call in a lambda, executes it, and if an exception is thrown, it attempts to match the exception against the provided lambdas by iterating through the possible error types. If no match is found, it rethrows the exception.

0 commit comments

Comments
 (0)