Skip to content

[libc++]Implement LWG4001(iota_view should provide empty) #84669

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion libcxx/docs/Status/Cxx2cIssues.csv
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"`3974 <https://wg21.link/LWG3974>`__","``mdspan::operator[]`` should not copy ``OtherIndexTypes``","Kona November 2023","","",""
"`3987 <https://wg21.link/LWG3987>`__","Including ``<flat_foo>`` doesn't provide ``std::begin``/``end``","Kona November 2023","","","|flat_containers|"
"`3990 <https://wg21.link/LWG3990>`__","Program-defined specializations of ``std::tuple`` and ``std::variant`` can't be properly supported","Kona November 2023","","",""
"`4001 <https://wg21.link/LWG4001>`__","``iota_view`` should provide ``empty``","Kona November 2023","","","|ranges|"
"`4001 <https://wg21.link/LWG4001>`__","``iota_view`` should provide ``empty``","Kona November 2023","|Complete|","19.0","|ranges|"
"","","","","",""
"`3343 <https://wg21.link/LWG3343>`__","Ordering of calls to ``unlock()`` and ``notify_all()`` in Effects element of ``notify_all_at_thread_exit()`` should be reversed","Not Yet Adopted","|Complete|","16.0",""
"","","","","",""
5 changes: 5 additions & 0 deletions libcxx/include/__ranges/iota_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ class iota_view : public view_interface<iota_view<_Start, _BoundSentinel>> {
return __iterator{__bound_sentinel_};
}

[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const
noexcept(noexcept(__value_ == __bound_sentinel_)) /* strengthened */ {
return __value_ == __bound_sentinel_;
}

_LIBCPP_HIDE_FROM_ABI constexpr auto size() const
requires(same_as<_Start, _BoundSentinel> && __advanceable<_Start>) ||
(integral<_Start> && integral<_BoundSentinel>) || sized_sentinel_for<_BoundSentinel, _Start>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23

// constexpr bool empty() const;

#include <ranges>

void test() {
const std::ranges::iota_view<int, int> io(1, 10);
io.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23

// constexpr bool empty() const;

#include <cassert>
#include <concepts>
#include <limits>
#include <ranges>

#include "test_macros.h"
#include "types.h"

struct my_exception {};

struct ThrowBound {
int bound;
constexpr bool operator==(int x) const {
if (x > 0) {
#if !defined(TEST_HAS_NO_EXCEPTIONS)
throw my_exception{};
#else
assert(false && "Exceptions are disabled");
#endif
}
return x == bound;
}

constexpr bool operator==(SomeInt x) const {
if (x.value_ > 0) {
#if !defined(TEST_HAS_NO_EXCEPTIONS)
throw my_exception();
#else
assert(false && "Exceptions are disabled");
#endif
}
return x.value_ == bound;
}
};

// clang-format off
constexpr bool test() {
// Both are integer like and neither less than zero.
{
{
const std::ranges::iota_view<int, int> io(1, 10);
assert(!io.empty());
LIBCPP_ASSERT_NOEXCEPT(io.empty());
}
{
const std::ranges::iota_view<int, int> io(2, 2);
assert(io.empty());
LIBCPP_ASSERT_NOEXCEPT(io.empty());
}
}

// Both are integer like and both are less than zero.
{
{
const std::ranges::iota_view<int, int> io(-10, -5);
assert(!io.empty());
LIBCPP_ASSERT_NOEXCEPT(io.empty());
}
{
const std::ranges::iota_view<int, int> io(-10, -10);
assert(io.empty());
LIBCPP_ASSERT_NOEXCEPT(io.empty());
}
{
const std::ranges::iota_view<int, int> io(0, 0);
assert(io.empty());
LIBCPP_ASSERT_NOEXCEPT(io.empty());
}
}

// Both are integer like and "value_" is less than zero.
{
const std::ranges::iota_view<int, int> io(-10, 10);
assert(!io.empty());
LIBCPP_ASSERT_NOEXCEPT(io.empty());
}

// Neither are integer like.
{
const std::ranges::iota_view<SomeInt, SomeInt> io(SomeInt(-20), SomeInt(-10));
assert(!io.empty());
LIBCPP_ASSERT_NOEXCEPT(io.empty());
}
{
const std::ranges::iota_view<SomeInt, SomeInt> io(SomeInt(-10), SomeInt(-10));
assert(io.empty());
LIBCPP_ASSERT_NOEXCEPT(io.empty());
}
{
const std::ranges::iota_view<SomeInt, SomeInt> io(SomeInt(0), SomeInt(0));
assert(io.empty());
LIBCPP_ASSERT_NOEXCEPT(io.empty());
}
{
const std::ranges::iota_view<SomeInt, SomeInt> io(SomeInt(10), SomeInt(20));
assert(!io.empty());
LIBCPP_ASSERT_NOEXCEPT(io.empty());
}
{
const std::ranges::iota_view<SomeInt, SomeInt> io(SomeInt(10), SomeInt(10));
assert(io.empty());
LIBCPP_ASSERT_NOEXCEPT(io.empty());
}

return true;
}
// clang-format on

void test_throw() {
#if !defined(TEST_HAS_NO_EXCEPTIONS)
try {
// Both are integer like and !noexcept(value_ == bound_)
{
const std::ranges::iota_view<int, ThrowBound> io(10, ThrowBound{10});
assert(io.empty());
LIBCPP_ASSERT_NOT_NOEXCEPT(io.empty());
}

// Neither are integer like and !noexcept(value_ == bound_)
{
const std::ranges::iota_view<SomeInt, ThrowBound> io(SomeInt{10}, ThrowBound{10});
assert(io.empty());
LIBCPP_ASSERT_NOT_NOEXCEPT(io.empty());
}
} catch (...) {
}
#endif // TEST_HAS_NO_EXCEPTIONS
}

int main() {
test();
test_throw();
static_assert(test());
return 0;
}