Skip to content

Commit 8ba9ed6

Browse files
authored
[libc++][ranges] LWG4035: single_view should provide empty (#98371)
Implements: https://wg21.link/LWG4035 - https://eel.is/c++draft/range.single.view
1 parent 1cd6359 commit 8ba9ed6

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

libcxx/docs/Status/Cxx2cIssues.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"`4025 <https://wg21.link/LWG4025>`__","Move assignment operator of ``std::expected<cv void, E>`` should not be conditionally deleted","Tokyo March 2024","","",""
5454
"`4030 <https://wg21.link/LWG4030>`__","Clarify whether arithmetic expressions in ``[numeric.sat.func]`` are mathematical or C++","Tokyo March 2024","|Nothing To Do|","",""
5555
"`4031 <https://wg21.link/LWG4031>`__","``bad_expected_access<void>`` member functions should be ``noexcept``","Tokyo March 2024","|Complete|","16.0",""
56-
"`4035 <https://wg21.link/LWG4035>`__","``single_view`` should provide ``empty``","Tokyo March 2024","","","|ranges|"
56+
"`4035 <https://wg21.link/LWG4035>`__","``single_view`` should provide ``empty``","Tokyo March 2024","|Complete|","19.0","|ranges|"
5757
"`4036 <https://wg21.link/LWG4036>`__","``__alignof_is_defined`` is only implicitly specified in C++ and not yet deprecated","Tokyo March 2024","","",""
5858
"`4037 <https://wg21.link/LWG4037>`__","Static data members of ``ctype_base`` are not yet required to be usable in constant expressions","Tokyo March 2024","","",""
5959
"`4038 <https://wg21.link/LWG4038>`__","``std::text_encoding::aliases_view`` should have constexpr iterators","Tokyo March 2024","","",""

libcxx/include/__ranges/single_view.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS single_view : public view_interface<s
7070

7171
_LIBCPP_HIDE_FROM_ABI constexpr const _Tp* end() const noexcept { return data() + 1; }
7272

73+
_LIBCPP_HIDE_FROM_ABI static constexpr bool empty() noexcept { return false; }
74+
7375
_LIBCPP_HIDE_FROM_ABI static constexpr size_t size() noexcept { return 1; }
7476

7577
_LIBCPP_HIDE_FROM_ABI constexpr _Tp* data() noexcept { return __value_.operator->(); }
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
11+
// static constexpr bool empty() noexcept;
12+
13+
#include <cassert>
14+
#include <concepts>
15+
#include <ranges>
16+
#include <utility>
17+
18+
#include "test_macros.h"
19+
20+
struct Empty {};
21+
struct BigType {
22+
char buffer[64] = {10};
23+
};
24+
25+
template <typename T>
26+
constexpr void test_empty(T value) {
27+
using SingleView = std::ranges::single_view<T>;
28+
29+
{
30+
std::same_as<bool> decltype(auto) result = SingleView::empty();
31+
assert(result == false);
32+
static_assert(noexcept(SingleView::empty()));
33+
}
34+
35+
{
36+
SingleView sv{value};
37+
38+
std::same_as<bool> decltype(auto) result = std::ranges::empty(sv);
39+
assert(result == false);
40+
static_assert(noexcept(std::ranges::empty(sv)));
41+
}
42+
{
43+
const SingleView sv{value};
44+
45+
std::same_as<bool> decltype(auto) result = std::ranges::empty(sv);
46+
assert(result == false);
47+
static_assert(noexcept(std::ranges::empty(std::as_const(sv))));
48+
}
49+
}
50+
51+
constexpr bool test() {
52+
test_empty<int>(92);
53+
test_empty<Empty>(Empty{});
54+
test_empty<BigType>(BigType{});
55+
56+
return true;
57+
}
58+
59+
int main(int, char**) {
60+
test();
61+
static_assert(test());
62+
63+
return 0;
64+
}

0 commit comments

Comments
 (0)