Skip to content

Commit

Permalink
Switch from P3168R1 to P3168R2 after LWG feedback in Saint Louis
Browse files Browse the repository at this point in the history
  • Loading branch information
neatudarius committed Jun 27, 2024
1 parent 25568cb commit fc95223
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This repository implements `std::optional` extensions targeting C++26. The `Bema

**Implements**:

* [Give *std::optional* Range Support (P3168R1)](https://wg21.link/P3168R1)
* [Give *std::optional* Range Support (P3168R2)](https://wg21.link/P3168R2)
* [`std::optional<T&>` (P2988R5)](https://wg21.link/P2988R5)

## License
Expand All @@ -23,13 +23,13 @@ Full runable examples can be found in `examples/` - please check [./examples/REA

### range_loop

The next code snippet shows optional range support added in [Give *std::optional* Range Support (P3168R1)](https://wg21.link/P3168R1):
The next code snippet shows optional range support added in [Give *std::optional* Range Support (P3168R2)](https://wg21.link/P3168R2):

```cpp
#include <Beman/Optional26/optional.hpp>
...

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

beman::optional26::optional<int> empty_opt{};
for ([[maybe_unused]] const auto& i : empty_opt) {
Expand All @@ -44,7 +44,7 @@ for (const auto& i : opt) {
}
```

Full code can be found in [./examples/range_loop.cpp](./examples/range_loop.cpp). Check for local build and run instructions in [./examples/README.md](./examples/README.md) or [range_loop.cpp@Compiler Explorer](https://godbolt.org/z/f8dWaxsGo)/
Full code can be found in [./examples/range_loop.cpp](./examples/range_loop.cpp). Check for local build and run instructions in [./examples/README.md](./examples/README.md) or try [range_loop.cpp@Compiler Explorer](https://godbolt.org/z/f8dWaxsGo).

https://godbolt.org/z/f8dWaxsGo

Expand Down
4 changes: 2 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ std_vs_beman: .value() matches?: yes
$ .build/gcc-14/examples/RelWithDebInfo/concept_checks
```

## Range Support (P3168R1)
## Range Support (P3168R2)

Range support added in [*Give std::optional Range Support* (P3168R1)](https://wg21.link/P3168R1) examples:
Range support added in [*Give std::optional Range Support* (P3168R2)](https://wg21.link/P3168R2) examples:

* local [./range_loop.cpp](./range_loop.cpp) or [range_loop.cpp@Compiler Explorer](https://godbolt.org/z/f8dWaxsGo)
* local [./pythagorean_triples.cpp](./pythagorean_triples.cpp) or [pythagorean_triples.cpp@Compiler Explorer](https://godbolt.org/z/fGr8jYM6P)
Expand Down
2 changes: 1 addition & 1 deletion examples/pythagorean_triples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <ranges>

int main() {
// Example from P3168R1: generate an infinite sequence of Pythagorean triples.
// Example from P3168R2: generate an infinite sequence of Pythagorean triples.
// (x, y, z) is a Pythagorean triple if 1 <= x <= y <= z and x^2 + y^2 = z^2.
constexpr auto yield_if = []<class T>(bool b, T x) -> beman::optional26::optional<T> {
return b ? beman::optional26::optional<T>{std::move(x)} : beman::optional26::nullopt;
Expand Down
2 changes: 1 addition & 1 deletion examples/range_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <iostream>

int main() {
// Example from P3168R1: basic range loop over C++26 optional.
// Example from P3168R2: basic range loop over C++26 optional.

beman::optional26::optional<int> empty_opt{};
for ([[maybe_unused]] const auto& i : empty_opt) {
Expand Down
12 changes: 6 additions & 6 deletions include/Beman/Optional26/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,15 @@ class optional;
} // namespace beman::optional26

namespace std {
// Since P3168R1: Give std::optional Range Support.
// Since P3168R2: Give std::optional Range Support.
template <typename T>
inline constexpr bool ranges::enable_view<beman::optional26::optional<T>> = true;

// TODO: document why this is needed.
template <typename T>
inline constexpr bool ranges::enable_borrowed_range<beman::optional26::optional<T&>> = true;

// Since P3168R1: Give std::optional Range Support.
// Since P3168R2: Give std::optional Range Support.
#if defined(__cpp_lib_format_ranges)
template <class T>
inline constexpr auto format_kind<beman::optional26::optional<T>> = range_format::disabled;
Expand Down Expand Up @@ -318,7 +318,7 @@ class optional {

public:
using value_type = T;
// Since P3168R1: Give std::optional Range Support.
// Since P3168R2: Give std::optional Range Support.
using iterator = detail::contiguous_iterator<T, optional>; // see [optional.iterators]
using const_iterator = detail::contiguous_iterator<const T, optional>; // see [optional.iterators]

Expand Down Expand Up @@ -688,7 +688,7 @@ class optional {
swap(engaged, rhs.engaged);
}

// Since P3168R1: Give std::optional Range Support.
// Since P3168R2: Give std::optional Range Support.
// [optional.iterators], iterator support
constexpr iterator begin() noexcept { return iterator(has_value() ? std::addressof(value_) : nullptr); }
constexpr const_iterator begin() const noexcept {
Expand Down Expand Up @@ -941,7 +941,7 @@ class optional<T&> {
using value_type = T&;
// Since ${PAPER_NUMBER}: ${PAPER_TITLE}.
// Note: P3168 and P2988 may have different flows inside LEWG/LWG.
// Implementation of the range support for optional<T&> reflects P3168R1 for now.
// Implementation of the range support for optional<T&> reflects P3168R2 for now.
// [optional.iterators], iterator support
using iterator = detail::contiguous_iterator<T, optional>; // see [optional.iterators]
using const_iterator = detail::contiguous_iterator<const T, optional>; // see [optional.iterators]
Expand Down Expand Up @@ -1076,7 +1076,7 @@ class optional<T&> {

// Since ${PAPER_NUMBER}: ${PAPER_TITLE}.
// Note: P3168 and P2988 may have different flows inside LEWG/LWG.
// Implementation of the range support for optional<T&> reflects P3168R1 for now.
// Implementation of the range support for optional<T&> reflects P3168R2 for now.
// [optional.iterators], iterator support
constexpr iterator begin() noexcept { return iterator(has_value() ? value_ : nullptr); };
constexpr const_iterator begin() const noexcept { return const_iterator(has_value() ? value_ : nullptr); };
Expand Down
10 changes: 5 additions & 5 deletions src/Beman/Optional26/tests/optional_range_support.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

/**
* This file contains tests for the range support. Check P3168R1: "Give std::optional Range Support".
* This file contains tests for the range support. Check P3168R2: "Give std::optional Range Support".
*
* RangeSupportTest: test suite for the range support.
*
Expand Down Expand Up @@ -211,7 +211,7 @@ TEST(RangeSupportTest, LoopOverNonEmptyRange) {
}

TEST(RangeSupportTest, LoopOptionalAccess) {
// Example from P3168R1: should access the value from an optional object.
// Example from P3168R2: should access the value from an optional object.
const int expected_value = 0xCAFEBABE;
const auto get_optional = [&]() -> beman::optional26::optional<int> { return expected_value; };
ASSERT_TRUE(get_optional().has_value());
Expand All @@ -222,7 +222,7 @@ TEST(RangeSupportTest, LoopOptionalAccess) {
}

TEST(RangeSupportTest, LoopOptionalAssignment) {
// Example from P3168R1: should mutate the value from an optional object.
// Example from P3168R2: should mutate the value from an optional object.
const int initial_expected_value = 0xCAFEBABE;
const int expected_value = 0xDEADBEEF;
const auto get_optional = [&]() -> beman::optional26::optional<int> { return initial_expected_value; };
Expand All @@ -239,7 +239,7 @@ TEST(RangeSupportTest, LoopOptionalAssignment) {
}

TEST(RangeSupportTest, RangeChainExample) {
// Example from P3168R1: start from a set of values, apply multiple range operations involving optional values.
// Example from P3168R2: start from a set of values, apply multiple range operations involving optional values.
std::unordered_set<int> s{1, 3, 7, 9};
const auto flt = [&](int i) -> beman::optional26::optional<int> {
if (s.contains(i)) {
Expand All @@ -261,7 +261,7 @@ TEST(RangeSupportTest, RangeChainExample) {
}

TEST(RangeSupportTest, PythagoreanTriples) {
// Example from P3168R1: generate an infinite sequence of Pythagorean triples.
// Example from P3168R2: generate an infinite sequence of Pythagorean triples.
// (x, y, z) is a Pythagorean triple if 1 <= x <= y <= z and x^2 + y^2 = z^2.
constexpr auto yield_if = []<class T>(bool b, T x) -> beman::optional26::optional<T> {
return b ? beman::optional26::optional<T>{std::move(x)} : beman::optional26::nullopt;
Expand Down

0 comments on commit fc95223

Please sign in to comment.