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

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Apr 2 09:38:01 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Hristo Hristov (H-G-Hristov)

<details>
<summary>Changes</summary>

Implements: https://wg21.link/LWG4035

---
Full diff: https://github.com/llvm/llvm-project/pull/87366.diff


2 Files Affected:

- (modified) libcxx/include/__ranges/single_view.h (+2) 
- (added) libcxx/test/std/ranges/range.factories/range.single.view/empty.pass.cpp (+50) 


``````````diff
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..ea462e820ec43b
--- /dev/null
+++ b/libcxx/test/std/ranges/range.factories/range.single.view/empty.pass.cpp
@@ -0,0 +1,50 @@
+//===----------------------------------------------------------------------===//
+//
+// 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};
+
+  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))));
+}
+
+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;
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/87366


More information about the libcxx-commits mailing list