Skip to content

Commit adf17d0

Browse files
committed
[libc++][ranges] LWG4035: single_view should provide empty
Implements: https://wg21.link/LWG4035
1 parent 4abb722 commit adf17d0

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
SingleView sv{value};
29+
30+
assert(!SingleView::empty());
31+
static_assert(noexcept(SingleView::empty()));
32+
static_assert(noexcept(std::ranges::empty(sv)));
33+
static_assert(noexcept(std::ranges::empty(std::as_const(sv))));
34+
}
35+
36+
constexpr bool test() {
37+
test_empty<int>(92);
38+
test_empty<Empty>(Empty{});
39+
test_empty<BigType>(BigType{});
40+
41+
return true;
42+
}
43+
44+
int main(int, char**) {
45+
test();
46+
static_assert(test());
47+
48+
return 0;
49+
}

0 commit comments

Comments
 (0)