@@ -60,56 +60,26 @@ Full code can be found in [./examples/range_loop.cpp](https://github.com/bemanpr
6060[ ./examples/README.md] ( https://github.com/bemanproject/optional/blob/main/examples/README.md ) .
6161
6262``` cpp { "compiler": "clang_trunk", "libs": ["beman_optional@trunk"], "options": "-std=c++26" }
63- // examples/optional_ref .cpp -*-C++-*-
63+ // examples/range_loop .cpp -*-C++-*-
6464// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6565
6666#include < beman/optional/optional.hpp>
67+ #include < iostream>
6768
68- #include < string>
69-
70- struct Cat {
71- int catalog_index{0};
72- };
73-
74- namespace std17 {
75-
76- // Prior to C++26, the code would look like this.
77- // Using raw pointers to represent optional references.
78- // Note: Using smart pointers would also be a choice, but it involves ownership semantics.
79-
80- Cat* find_cat(std::string) { return nullptr; }
81-
82- Cat* do_it(Cat& cat) { return &cat; }
69+ int main () {
70+ // Example from P3168R1: basic range loop over C++26 optional.
8371
84- Cat * api() {
85- Cat * cat = find_cat("Fido");
86- if (cat != nullptr) {
87- return do_it( * cat) ;
72+ beman::optional::optional<int> empty_opt{};
73+ for ([[maybe_unused]] const auto& i : empty_opt) {
74+ // Should not see this in console.
75+ std::cout << "\"for each loop\" over C++26 optional: empty_opt\n" ;
8876 }
89- return nullptr;
90- }
91-
92- } // namespace std17
93-
94- namespace std26 {
95- // After C++26 with P2988R5, the code would look like this.
96- // Using directly optional to represent optional references.
97-
98- beman::optional::optional<Cat&> find_cat(std::string) { return {}; }
99-
100- beman::optional::optional<Cat&> do_it(Cat& cat) { return {cat}; }
101-
102- beman::optional::optional<Cat&> api() {
103- beman::optional::optional<Cat&> cat = find_cat("Fido");
104- return cat.and_then(do_it);
105- }
106-
107- } // namespace std26
10877
109- int main() {
110- // Example from P2988R5: optional reference.
111- [[ maybe_unused]] Cat* old_cat = std17::api();
112- [[ maybe_unused]] beman::optional::optional<Cat&> new_cat = std26::api();
78+ beman::optional::optional<int> opt{__cplusplus};
79+ for (const auto& i : opt) {
80+ // Should see this in console.
81+ std::cout << "\"for each loop\" over C++26 optional: opt = " << i << "\n";
82+ }
11383
11484 return 0;
11585}
@@ -118,7 +88,9 @@ int main() {
11888// $ cmake --workflow --preset gcc-14
11989//
12090// # run example:
121- // $ .build/gcc-14/examples/RelWithDebInfo/optional_ref
91+ // $ .build/gcc-14/examples/RelWithDebInfo/range_loop
92+ // # note: 1st log (empty_opt) is missing from console!
93+ // "for each loop" over C++26 optional: opt = 26 # 2nd log (non empty opt)
12294```
12395
12496### optional_ref
@@ -350,4 +322,4 @@ Latest revision(s) of the papers can be built / found at:
350322 * issue: [ #1661 ] ( https://github.com/cplusplus/papers/issues/1661 )
351323 * LEWG:
352324 * Reviewed in Tokyo 2024.
353- * Forwarded by LEWG in 2025 in Hagenberg.
325+ * Forwarded by LEWG in 2025 in Hagenberg.
0 commit comments