-
Notifications
You must be signed in to change notification settings - Fork 20
/
try.hpp
35 lines (31 loc) · 1.18 KB
/
try.hpp
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
#pragma once
#include <tl_expected/expected.hpp>
/** @file */
/**
* @brief Unwrap an expected into a stack variable or return the unexpected value
*
* This macro requires the use of non-standard C++. Compiler extensions must be enabled and
* -Wpedantic cannot be used. With GCC and Clang, you can disable -Wpedantic for an entire
* translation unit like this:
*
* ```cpp
* #pragma GCC diagnostic ignored "-Wpedantic"
* ```
* You can disable -Wpedantic for a specific segment of code like so:
*
* ```cpp
* #pragma GCC diagnostic push
* #pragma GCC diagnostic ignored "-Wpedantic"
* //...
* #pragma GCC diagnostic pop
* ```
*
* For more information on this particular compiler extension go to
* https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
*/
#define TRY(expected) \
({ \
auto const& _expected = (expected); /* Uglify name to prevent shadowing */ \
if (!_expected.has_value()) return tl::unexpected(_expected.error()); \
_expected.value(); \
})