-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptional_types.cpp
More file actions
31 lines (26 loc) · 1.06 KB
/
Copy pathoptional_types.cpp
File metadata and controls
31 lines (26 loc) · 1.06 KB
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
import std;
import serial_xml;
// std::optional members are omitted entirely when they
// contain std::nullopt. When they hold a value, they
// behave like their inner type.
struct OptionalChild {
std::optional<int> value;
};
struct OptionalAttribute {
[[= serial_xml::attribute]] std::optional<int> value;
};
struct MixedOptional {
std::string always_present;
std::optional<std::string> maybe_present;
};
int main() {
std::print("Optional with value:\n {}\n", serial_xml::to_xml(OptionalChild{{42}}));
std::print("Optional without value (nullopt):\n {}\n",
serial_xml::to_xml(OptionalChild{std::nullopt}));
std::print("Optional attribute with value:\n {}\n", serial_xml::to_xml(OptionalAttribute{{42}}));
std::print("Optional attribute without value:\n {}\n",
serial_xml::to_xml(OptionalAttribute{std::nullopt}));
std::print("Mixed optional:\n {}\n", serial_xml::to_xml(MixedOptional{"hello", std::nullopt}));
std::print("Mixed optional (both present):\n {}\n",
serial_xml::to_xml(MixedOptional{"hello", "world"}));
}