Skip to content

Commit 7cab021

Browse files
committed
Address comments
1 parent 97f3a7b commit 7cab021

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

libcxx/test/std/utilities/optional/optional.iterator/begin.pass.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717
#include <iterator>
1818
#include <optional>
1919
#include <type_traits>
20+
#include <utility>
2021

2122
template <typename T>
2223
constexpr bool test() {
23-
constexpr std::optional<T> opt{T{}};
24+
const std::optional<T> opt{T{}};
2425
std::optional<T> nonconst_opt{T{}};
2526

2627
{ // begin() is marked noexcept
27-
assert(noexcept(opt.begin()));
28-
assert(noexcept(nonconst_opt.begin()));
28+
static_assert(noexcept(opt.begin()));
29+
static_assert(noexcept(nonconst_opt.begin()));
2930
}
3031

3132
{ // Dereferencing an iterator at the beginning == indexing the 0th element, and that calling begin() again return the same iterator.
@@ -37,6 +38,14 @@ constexpr bool test() {
3738
assert(iter2 == nonconst_opt.begin());
3839
}
3940

41+
{ // Calling begin() multiple times on a disengaged optional returns the same iterator.
42+
std::optional<T> disengaged{std::nullopt};
43+
auto iter1 = disengaged.begin();
44+
auto iter2 = std::as_const(disengaged).begin();
45+
assert(iter1 == disengaged.begin());
46+
assert(iter2 == std::as_const(disengaged).begin());
47+
}
48+
4049
return true;
4150
}
4251

@@ -48,7 +57,7 @@ constexpr bool tests() {
4857
return true;
4958
}
5059

51-
int main() {
60+
int main(int, char**) {
5261
assert(tests());
5362
static_assert(tests());
5463

libcxx/test/std/utilities/optional/optional.iterator/end.pass.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
template <typename T>
2222
constexpr bool test() {
2323
std::optional<T> unengaged{std::nullopt};
24-
constexpr std::optional<T> unengaged2{std::nullopt};
24+
const std::optional<T> unengaged2{std::nullopt};
2525

2626
{ // end() is marked noexcept
27-
assert(noexcept(unengaged.end()));
28-
assert(noexcept(unengaged2.end()));
27+
static_assert(noexcept(unengaged.end()));
28+
static_assert(noexcept(unengaged2.end()));
2929
}
3030

3131
{ // end() == begin() and end() == end() if the optional is unengaged
@@ -42,7 +42,7 @@ constexpr bool test() {
4242
}
4343

4444
std::optional<T> engaged{T{}};
45-
constexpr std::optional<T> engaged2{T{}};
45+
const std::optional<T> engaged2{T{}};
4646

4747
{ // end() != begin() if the optional is engaged
4848
auto it = engaged.end();
@@ -67,7 +67,9 @@ constexpr bool tests() {
6767
return true;
6868
}
6969

70-
int main() {
70+
int main(int, char**) {
7171
assert(tests());
7272
static_assert(tests());
73+
74+
return 0;
7375
}

libcxx/test/std/utilities/optional/optional.iterator/iterator.pass.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
template <typename T, T __val>
2323
constexpr bool test() {
24-
constexpr std::optional<T> opt{__val};
24+
const std::optional<T> opt{__val};
2525
std::optional<T> nonconst_opt{__val};
2626

2727
{ // Dereferencing an iterator of an engaged optional will return the same value that the optional holds.
@@ -50,22 +50,23 @@ constexpr bool test() {
5050
assert((std::is_same_v<typename decltype(it2)::reference, T&>));
5151
}
5252

53-
{ // std::ranges for an engaged optional<T> == 1, unengaged optional<T> == 0
54-
constexpr std::optional<T> unengaged{std::nullopt};
55-
std::optional<T> unengaged2{std::nullopt};
53+
{ // std::ranges::size for an engaged optional<T> == 1, disengaged optional<T> == 0
54+
const std::optional<T> disengaged{std::nullopt};
55+
std::optional<T> disengaged2{std::nullopt};
5656
assert(std::ranges::size(opt) == 1);
5757
assert(std::ranges::size(nonconst_opt) == 1);
5858

59-
assert(std::ranges::size(unengaged) == 0);
60-
assert(std::ranges::size(unengaged2) == 0);
59+
assert(std::ranges::size(disengaged) == 0);
60+
assert(std::ranges::size(disengaged2) == 0);
6161
}
6262

6363
{ // std::ranges::enable_view<optional<T>> == true, and std::format_kind<optional<T>> == true
64-
assert(std::ranges::enable_view<std::optional<T>> == true);
65-
assert(std::format_kind<std::optional<T>> == std::range_format::disabled);
64+
static_assert(std::ranges::enable_view<std::optional<T>> == true);
65+
static_assert(std::format_kind<std::optional<T>> == std::range_format::disabled);
6666
}
6767

68-
// 8: An optional with value that is reset will have a begin() == end(), then when it is reassigned a value, begin() != end(), and *begin() will contain the new value.
68+
// An optional with value that is reset will have a begin() == end(), then when it is reassigned a value,
69+
// begin() != end(), and *begin() will contain the new value.
6970
{
7071
std::optional<T> val{__val};
7172
assert(val.begin() != val.end());
@@ -89,7 +90,9 @@ constexpr bool tests() {
8990
return true;
9091
}
9192

92-
int main() {
93+
int main(int, char**) {
9394
assert(tests());
9495
static_assert(tests());
96+
97+
return 0;
9598
}

0 commit comments

Comments
 (0)