Skip to content

Commit 3bb5699

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

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

README.md

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ The next code snippet shows optional range support added in [Give *std::optional
4242
#include <beman/optional/optional.hpp>
4343
...
4444

45-
// Example from P3168R2: basic range loop over C++26 optional.
46-
4745
beman::optional::optional<int> empty_opt{};
4846
for ([[maybe_unused]] const auto& i : empty_opt) {
4947
// Should not see this in console.
@@ -69,28 +67,21 @@ The next code snippet shows optional reference support added in [`std::optional<
6967
#include <beman/optional/optional.hpp>
7068
...
7169

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-
}
70+
struct Cat { ... };
71+
72+
beman::optional::optional<Cat&> find_cat(std::string) { return {}; }
73+
beman::optional::optional<Cat&> do_it(Cat& cat) { return { cat }; }
8174

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";
75+
beman::optional::optional<Cat&> api() {
76+
beman::optional::optional<Cat&> cat = find_cat("Fido");
77+
return cat.and_then(do_it);
9078
}
79+
80+
beman::optional::optional<Cat&> cat = api();
81+
9182
```
9283
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).
84+
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).
9485
9586
## How to Build
9687

0 commit comments

Comments
 (0)