You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With C++-26 reflection, fixed-size C arrays and inheritance are supported out of the box (no
777
+
`-DREFLECT_CPP_C_ARRAYS_OR_INHERITANCE` flag needed), and there are no range restrictions for
778
+
enums. Refer to the [documentation](https://rfl.getml.com/cpp26_reflection) for details.
779
+
732
780
## The team behind reflect-cpp
733
781
734
782
reflect-cpp has been developed by [getML (Code17 GmbH)](https://getml.com), a company specializing in software engineering and machine learning for enterprise applications. reflect-cpp is currently maintained by Patrick Urbanke and Manuel Bellersen, with major contributions coming from the community.
C++-23 introduced `std::expected` as the standard way of expressing the result of an operation that might fail. Unlike `std::optional`, it holds both the value on success (of type `T`) and the error on failure (of type `E`). reflect-cpp supports `std::expected` out of the box: you can use it as a top-level type, as a field of a struct, or inside containers.
4
+
5
+
## Availability
6
+
7
+
`std::expected` is a C++-23 feature. reflect-cpp detects it via the feature-test macro `__cpp_lib_expected`. Note that some standard libraries only expose `<expected>` when compiled in C++-23 mode — for instance, GCC's libstdc++ requires `-std=c++23`. If your standard library does not provide `std::expected`, reflect-cpp will not recognize the type, and attempting to serialize one will result in a compile-time error.
8
+
9
+
## How `std::expected` is serialized
10
+
11
+
A `std::expected<T, E>` is serialized as the two alternatives of an untagged variant (an `rfl::Variant`):
12
+
13
+
- If it holds a value, the value is written as-is, i.e. exactly the same way as it would be written if it were of type `T`.
14
+
- If it holds an error, an object with a single field named `error` is written, containing the value of type `E`.
15
+
16
+
Wrapping the error in an object makes sure that the error is always recognized as an object, even if `T` itself is a struct.
17
+
18
+
```cpp
19
+
#include<expected>
20
+
#include<rfl/json.hpp>
21
+
22
+
const std::expected<int, std::string> ok = 42;
23
+
const std::string ok_json = rfl::json::write(ok);
24
+
// -> 42
25
+
26
+
const std::expected<int, std::string> err = std::unexpected("Something went wrong.");
- Formats that do not support variants (CSV and Parquet) do not support `std::expected` either, since it is serialized as a variant under the hood.
103
+
104
+
## Relation to `rfl::Result`
105
+
106
+
reflect-cpp's own result type, [`rfl::Result`](result.md), is what `rfl::json::read` and `rfl::json::write` return and operate on. Supporting `std::expected` is a separate concern: it means that you can use `std::expected<T, E>` as a *data type* in your structs, which is what this section is about.
107
+
108
+
Note that there is a CMake option `REFLECTCPP_USE_STD_EXPECTED` that makes `rfl::Result<T>` an alias for `std::expected<T, rfl::Error>`. This is a separate feature from the one described in this section, but the two can be combined.
Copy file name to clipboardExpand all lines: docs/standard_containers.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,6 +51,8 @@ This will also be represented as follows:
51
51
```
52
52
53
53
All other supported standard containers
54
-
(other than `std::variant`, `std::optional`, `std::unique_ptr`and `std::shared_ptr`)
54
+
(other than `std::variant`, `std::optional`, `std::unique_ptr`, `std::shared_ptr`and `std::expected`)
55
55
will be represented as arrays. Containers for which the `value_type`
56
56
is a key-value-pair will be represented as arrays of pairs.
57
+
58
+
`std::expected` is an exception to this: it is serialized as its value type, or as an object with a single `error` field. Refer to the [std::expected](expected.md) section for details.
0 commit comments