Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ The next code snippet shows optional range support added in [Give *std::optional
#include <beman/optional/optional.hpp>
...

// Example from P3168R2: basic range loop over C++26 optional.

beman::optional::optional<int> empty_opt{};
for ([[maybe_unused]] const auto& i : empty_opt) {
// Should not see this in console.
Expand All @@ -69,28 +67,21 @@ The next code snippet shows optional reference support added in [`std::optional<
#include <beman/optional/optional.hpp>
...

{
// Empty optional example.
std::optional<int> std_empty_opt;
beman::optional::optional<int> beman_empty_opt;
assert(!std_empty_opt.has_value() &&
!beman_empty_opt.has_value()); // or assert(!std_empty_opt && !beman_empty_opt);
std::cout << "std_vs_beman: .has_value() matches?: "
<< (std_empty_opt.has_value() == beman_empty_opt.has_value() ? "yes" : "no") << "\n";
}
struct Cat { ... };

beman::optional::optional<Cat&> find_cat(std::string) { return {}; }
beman::optional::optional<Cat&> do_it(Cat& cat) { return { cat }; }

{
// Optional with value example.
std::optional<int> std_opt = 26;
beman::optional::optional<int> beman_opt = 26;
assert(std_opt.has_value() && beman_opt.has_value()); // or assert(std_opt && beman_opt);
assert(std_opt.value() == beman_opt.value()); // or assert(*std_opt == *beman_opt);
std::cout << "std_vs_beman: .value() matches?: " << (std_opt.value() == beman_opt.value() ? "yes" : "no")
<< "\n";
beman::optional::optional<Cat&> api() {
beman::optional::optional<Cat&> cat = find_cat("Fido");
return cat.and_then(do_it);
}

beman::optional::optional<Cat&> cat = api();

```

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).
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).

## How to Build

Expand Down