[libcxx-commits] [libcxx] [libc++][ranges] LWG4035: `single_view` should provide `empty` (PR #87366)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Fri Apr 5 13:21:24 PDT 2024


https://github.com/H-G-Hristov updated https://github.com/llvm/llvm-project/pull/87366

>From adf17d00442312fdc58326b1dc29515c275a97d5 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Tue, 2 Apr 2024 19:27:29 +0300
Subject: [PATCH 1/4] [libc++][ranges] LWG4035: `single_view` should provide
 `empty`

Implements: https://wg21.link/LWG4035
---
 libcxx/include/__ranges/single_view.h         |  2 +
 .../range.single.view/empty.pass.cpp          | 49 +++++++++++++++++++
 2 files changed, 51 insertions(+)
 create mode 100644 libcxx/test/std/ranges/range.factories/range.single.view/empty.pass.cpp

diff --git a/libcxx/include/__ranges/single_view.h b/libcxx/include/__ranges/single_view.h
index f91c7c35263676..45244f34994d74 100644
--- a/libcxx/include/__ranges/single_view.h
+++ b/libcxx/include/__ranges/single_view.h
@@ -70,6 +70,8 @@ class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS single_view : public view_interface<s
 
   _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* end() const noexcept { return data() + 1; }
 
+  _LIBCPP_HIDE_FROM_ABI static constexpr bool empty() noexcept { return false; }
+
   _LIBCPP_HIDE_FROM_ABI static constexpr size_t size() noexcept { return 1; }
 
   _LIBCPP_HIDE_FROM_ABI constexpr _Tp* data() noexcept { return __value_.operator->(); }
diff --git a/libcxx/test/std/ranges/range.factories/range.single.view/empty.pass.cpp b/libcxx/test/std/ranges/range.factories/range.single.view/empty.pass.cpp
new file mode 100644
index 00000000000000..fe73b5ef2b3409
--- /dev/null
+++ b/libcxx/test/std/ranges/range.factories/range.single.view/empty.pass.cpp
@@ -0,0 +1,49 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// static constexpr bool empty() noexcept;
+
+#include <cassert>
+#include <concepts>
+#include <ranges>
+#include <utility>
+
+#include "test_macros.h"
+
+struct Empty {};
+struct BigType {
+  char buffer[64] = {10};
+};
+
+template <typename T>
+constexpr void test_empty(T value) {
+  using SingleView = std::ranges::single_view<T>;
+  SingleView sv{value};
+
+  assert(!SingleView::empty());
+  static_assert(noexcept(SingleView::empty()));
+  static_assert(noexcept(std::ranges::empty(sv)));
+  static_assert(noexcept(std::ranges::empty(std::as_const(sv))));
+}
+
+constexpr bool test() {
+  test_empty<int>(92);
+  test_empty<Empty>(Empty{});
+  test_empty<BigType>(BigType{});
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}

>From 49862b435f41a741e45458aafa8d7a7b6059de80 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Tue, 2 Apr 2024 19:36:44 +0300
Subject: [PATCH 2/4] Minor tweak

---
 .../ranges/range.factories/range.single.view/empty.pass.cpp    | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libcxx/test/std/ranges/range.factories/range.single.view/empty.pass.cpp b/libcxx/test/std/ranges/range.factories/range.single.view/empty.pass.cpp
index fe73b5ef2b3409..ea462e820ec43b 100644
--- a/libcxx/test/std/ranges/range.factories/range.single.view/empty.pass.cpp
+++ b/libcxx/test/std/ranges/range.factories/range.single.view/empty.pass.cpp
@@ -27,7 +27,8 @@ constexpr void test_empty(T value) {
   using SingleView = std::ranges::single_view<T>;
   SingleView sv{value};
 
-  assert(!SingleView::empty());
+  std::same_as<bool> decltype(auto) result = SingleView::empty();
+  assert(result == false);
   static_assert(noexcept(SingleView::empty()));
   static_assert(noexcept(std::ranges::empty(sv)));
   static_assert(noexcept(std::ranges::empty(std::as_const(sv))));

>From 9b6d7befd953a6e13dd8bec227af6aa88778b932 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Tue, 2 Apr 2024 20:15:19 +0300
Subject: [PATCH 3/4] Minor tweak

---
 .../range.single.view/empty.pass.cpp          | 26 ++++++++++++++-----
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/libcxx/test/std/ranges/range.factories/range.single.view/empty.pass.cpp b/libcxx/test/std/ranges/range.factories/range.single.view/empty.pass.cpp
index ea462e820ec43b..7e6ff015ea9a41 100644
--- a/libcxx/test/std/ranges/range.factories/range.single.view/empty.pass.cpp
+++ b/libcxx/test/std/ranges/range.factories/range.single.view/empty.pass.cpp
@@ -25,13 +25,27 @@ struct BigType {
 template <typename T>
 constexpr void test_empty(T value) {
   using SingleView = std::ranges::single_view<T>;
-  SingleView sv{value};
 
-  std::same_as<bool> decltype(auto) result = SingleView::empty();
-  assert(result == false);
-  static_assert(noexcept(SingleView::empty()));
-  static_assert(noexcept(std::ranges::empty(sv)));
-  static_assert(noexcept(std::ranges::empty(std::as_const(sv))));
+  {
+    std::same_as<bool> decltype(auto) result = SingleView::empty();
+    assert(result == false);
+    static_assert(noexcept(SingleView::empty()));
+  }
+
+  {
+    SingleView sv{value};
+
+    std::same_as<bool> decltype(auto) result = std::ranges::empty(sv);
+    assert(result == false);
+    static_assert(noexcept(std::ranges::empty(sv)));
+  }
+  {
+    const SingleView sv{value};
+
+    std::same_as<bool> decltype(auto) result = std::ranges::empty(sv);
+    assert(result == false);
+    static_assert(noexcept(std::ranges::empty(std::as_const(sv))));
+  }
 }
 
 constexpr bool test() {

>From 341d85bb33e86be9899a29a940f14dd89c06974e Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Thu, 4 Apr 2024 23:16:39 +0300
Subject: [PATCH 4/4] Updated status page

---
 libcxx/docs/Status/Cxx2cIssues.csv | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/docs/Status/Cxx2cIssues.csv b/libcxx/docs/Status/Cxx2cIssues.csv
index 8a4bf2ef62162a..dc6d1c79bc157f 100644
--- a/libcxx/docs/Status/Cxx2cIssues.csv
+++ b/libcxx/docs/Status/Cxx2cIssues.csv
@@ -53,7 +53,7 @@
 "`4025 <https://wg21.link/LWG4025>`__","Move assignment operator of ``std::expected<cv void, E>`` should not be conditionally deleted","Tokyo March 2024","","",""
 "`4030 <https://wg21.link/LWG4030>`__","Clarify whether arithmetic expressions in ``[numeric.sat.func]`` are mathematical or C++","Tokyo March 2024","|Nothing To Do|","",""
 "`4031 <https://wg21.link/LWG4031>`__","``bad_expected_access<void>`` member functions should be ``noexcept``","Tokyo March 2024","","",""
-"`4035 <https://wg21.link/LWG4035>`__","``single_view`` should provide ``empty``","Tokyo March 2024","","","|ranges|"
+"`4035 <https://wg21.link/LWG4035>`__","``single_view`` should provide ``empty``","Tokyo March 2024","|Complete|","19.0","|ranges|"
 "`4036 <https://wg21.link/LWG4036>`__","``__alignof_is_defined`` is only implicitly specified in C++ and not yet deprecated","Tokyo March 2024","","",""
 "`4037 <https://wg21.link/LWG4037>`__","Static data members of ``ctype_base`` are not yet required to be usable in constant expressions","Tokyo March 2024","","",""
 "`4038 <https://wg21.link/LWG4038>`__","``std::text_encoding::aliases_view`` should have constexpr iterators","Tokyo March 2024","","",""



More information about the libcxx-commits mailing list