-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[libc++] Implement P2988R12: std::optional<T&>
#155202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
72bb6e9
df2e9e7
322079d
2a014d8
294cb8e
762bb8c
223ce49
92137cb
c123551
9075442
0ca1d51
4ba0c8f
660716c
717c5f9
1612378
ea6ce10
089cb71
c602898
42637ff
3c9c51b
dfb3349
bc68310
4f87438
e586acd
18c1b96
1add5c5
85db654
65e92c4
5cf4603
3c2473d
4f58bf2
ba998d7
44d8d05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // REQUIRES: std-at-least-c++26 | ||
|
|
||
| // <optional> | ||
|
|
||
| // template <class U> T optional<T>::value_or(U&&); | ||
|
|
||
| #include <concepts> | ||
| #include <optional> | ||
|
|
||
| template <typename Opt, typename T> | ||
| concept has_value_or = requires(Opt opt, T&& t) { | ||
| { opt.value_or(t) } -> std::same_as<T>; | ||
| }; | ||
|
|
||
| static_assert(has_value_or<std::optional<int>, int>); | ||
| static_assert(has_value_or<std::optional<int&>, int&>); | ||
| static_assert(has_value_or<std::optional<const int&>, const int&>); | ||
| static_assert(!has_value_or<std::optional<int (&)[1]>&&, int (&)[1]>); | ||
| static_assert(!has_value_or<std::optional<int (&)()>&&, int (&)()>); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,8 @@ | |
|
|
||
| template <typename T> | ||
| constexpr bool test() { | ||
| std::optional<T> opt{T{}}; | ||
| std::remove_reference_t<T> t = std::remove_reference_t<T>{}; | ||
| std::optional<T> opt{t}; | ||
|
|
||
| { // begin() is marked noexcept | ||
| static_assert(noexcept(opt.begin())); | ||
|
|
@@ -53,6 +54,10 @@ constexpr bool tests() { | |
| assert(test<char>()); | ||
| assert(test<const int>()); | ||
| assert(test<const char>()); | ||
| assert(test<int&>()); | ||
| assert(test<char&>()); | ||
| assert(test<const int&>()); | ||
| assert(test<const char&>()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto, if you think it's better not to delay implementing LWG4308, cases for |
||
| return true; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,74 @@ | ||||||||||||||||||||||||||||||||
| //===----------------------------------------------------------------------===// | ||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why isn't this a |
||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||||||||||||||||||||||||||||||
| // See https://llvm.org/LICENSE.txt for license information. | ||||||||||||||||||||||||||||||||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||
| //===----------------------------------------------------------------------===// | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // REQUIRES: std-at-least-c++26 | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // <optional> | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // template <class T> class optional<T&>::iterator; | ||||||||||||||||||||||||||||||||
| // template <class T> class optional<T&>::const_iterator; | ||||||||||||||||||||||||||||||||
| // template <class T> | ||||||||||||||||||||||||||||||||
| // constexpr bool ranges::enable_borrowed_range<optional<T&>> = true; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| #include <cassert> | ||||||||||||||||||||||||||||||||
| #include <optional> | ||||||||||||||||||||||||||||||||
| #include <ranges> | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||
| constexpr bool enable_borrowed_range() { | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| assert(std::ranges::enable_borrowed_range<std::optional<T&>>); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+22
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Ditto below, I think we should only use |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| template <typename T> | ||||||||||||||||||||||||||||||||
| constexpr bool borrowed_range() { | ||||||||||||||||||||||||||||||||
| if (std::ranges::range<std::optional<T&>>) { | ||||||||||||||||||||||||||||||||
| assert(std::ranges::borrowed_range<std::optional<T&>>); | ||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| assert(!std::ranges::borrowed_range<std::optional<T&>>); | ||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+30
to
+40
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
So that we won't need to touch this file again when implementing LWG4308, as we've been testing range-ness in |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| constexpr bool test_enable_borrowed_range() { | ||||||||||||||||||||||||||||||||
| assert(enable_borrowed_range<int>()); | ||||||||||||||||||||||||||||||||
| assert(enable_borrowed_range<const int>()); | ||||||||||||||||||||||||||||||||
| assert(enable_borrowed_range<int[]>()); | ||||||||||||||||||||||||||||||||
| assert(enable_borrowed_range<int[10]>()); | ||||||||||||||||||||||||||||||||
| assert(enable_borrowed_range<int()>()); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| constexpr bool test_borrowed_range() { | ||||||||||||||||||||||||||||||||
| assert(borrowed_range<int>()); | ||||||||||||||||||||||||||||||||
| assert(borrowed_range<const int>()); | ||||||||||||||||||||||||||||||||
| assert(!borrowed_range<int[]>()); | ||||||||||||||||||||||||||||||||
| assert(!borrowed_range<int[10]>()); | ||||||||||||||||||||||||||||||||
| assert(!borrowed_range<int()>()); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| int main(int, char**) { | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| static_assert(test_enable_borrowed_range()); | ||||||||||||||||||||||||||||||||
| assert(test_enable_borrowed_range()); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| static_assert(test_borrowed_range()); | ||||||||||||||||||||||||||||||||
| assert(test_enable_borrowed_range()); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, conventional empty line at end of the file.
Suggested change
|
||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like that we should achieve the following results, per the current resolution of LWG4308.
The product code needs to be correspondingly adjusted. We can do this in a following-up PR as LWG4308 is not formally accepted yet (but it will be soon).