Skip to content

Commit 27c9abe

Browse files
committed
Add proper example for optional ref in root README
1 parent 033d393 commit 27c9abe

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

README.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,24 @@ The next code snippet shows optional reference support added in [`std::optional<
6969
#include <beman/optional/optional.hpp>
7070
...
7171

72-
{
73-
// Empty optional example.
74-
std::optional<int> std_empty_opt;
75-
beman::optional::optional<int> beman_empty_opt;
76-
assert(!std_empty_opt.has_value() &&
77-
!beman_empty_opt.has_value()); // or assert(!std_empty_opt && !beman_empty_opt);
78-
std::cout << "std_vs_beman: .has_value() matches?: "
79-
<< (std_empty_opt.has_value() == beman_empty_opt.has_value() ? "yes" : "no") << "\n";
80-
}
72+
// Example from P2988R5: optional reference.
73+
74+
struct Cat { ... };
75+
76+
beman::optional::optional<Cat&> find_cat(std::string) { return {}; }
77+
beman::optional::optional<Cat&> do_it(Cat& cat) { return { cat }; }
8178

82-
{
83-
// Optional with value example.
84-
std::optional<int> std_opt = 26;
85-
beman::optional::optional<int> beman_opt = 26;
86-
assert(std_opt.has_value() && beman_opt.has_value()); // or assert(std_opt && beman_opt);
87-
assert(std_opt.value() == beman_opt.value()); // or assert(*std_opt == *beman_opt);
88-
std::cout << "std_vs_beman: .value() matches?: " << (std_opt.value() == beman_opt.value() ? "yes" : "no")
89-
<< "\n";
79+
beman::optional::optional<Cat&> api() {
80+
beman::optional::optional<Cat&> cat = find_cat("Fido");
81+
return cat.and_then(do_it);
9082
}
83+
84+
beman::optional::optional<Cat&> cat = api();
85+
9186
```
9287
93-
Full code can be found in [./examples/optional_ref.cpp](./examples/optional_ref.cpp). Build and run instructions in [./examples/README.md](./examples/README.md).
88+
Full code can be found in [./examples/optional_ref.cpp](./examples/optional_ref.cpp). Build and run instructions in [./examples/README.md](./examples/README.md). Or try it on Compiler Explorer: [optional_ref.cpp@Compiler Explorer](https://godbolt.org/z/MxjdvTTov)
89+
9490
9591
## How to Build
9692

0 commit comments

Comments
 (0)