Skip to content

Commit 58b8e15

Browse files
committed
make get nodiscard, add nodiscard test, clean up tests
1 parent 3e3ab8d commit 58b8e15

File tree

5 files changed

+48
-28
lines changed

5 files changed

+48
-28
lines changed

libcxx/include/__utility/integer_sequence.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr void __for_each_index_sequence(index_sequence<_I
7070
# endif // _LIBCPP_STD_VER >= 20
7171

7272
# if _LIBCPP_STD_VER >= 26
73-
// structured binding support for integer_sequence
73+
// [intseq.binding], structured binding support
7474
template <class _Tp, _Tp... _Indices>
7575
struct tuple_size<integer_sequence<_Tp, _Indices...>> : integral_constant<size_t, sizeof...(_Indices)> {};
7676

@@ -87,7 +87,7 @@ struct tuple_element<_Ip, const integer_sequence<_Tp, _Indices...>> {
8787
};
8888

8989
template <size_t _Ip, class _Tp, _Tp... _Indices>
90-
_LIBCPP_HIDE_FROM_ABI constexpr _Tp get(integer_sequence<_Tp, _Indices...>) noexcept {
90+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp get(integer_sequence<_Tp, _Indices...>) noexcept {
9191
static_assert(_Ip < sizeof...(_Indices), "Index out of bounds in std::get<> (std::integer_sequence)");
9292
return _Indices...[_Ip];
9393
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
// REQUIRES: std-at-least-c++26
10+
11+
// <utility>
12+
13+
// template<size_t I, class T, T... Values>
14+
// [[nodiscard]] constexpr T get(integer_sequence<T, Values...>) noexcept;
15+
16+
// check that get is marked [[nodiscard]]
17+
18+
#include <utility>
19+
20+
void f() {
21+
std::index_sequence<1> seq;
22+
get<0>(seq); // expected-warning {{ignoring return value of function}}
23+
}

libcxx/test/std/utilities/intseq/intseq.binding/structured_binding.pass.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,34 @@
2020
#include <cassert>
2121
#include <utility>
2222

23-
void test_structured_bindings() {
23+
constexpr bool test_structured_bindings() {
2424
auto [elt0, elt1, elt2, elt3] = std::make_index_sequence<4>();
25-
25+
2626
assert(elt0 == 0);
2727
assert(elt1 == 1);
2828
assert(elt2 == 2);
2929
assert(elt3 == 3);
30-
}
3130

3231
#if __cpp_structured_bindings >= 202411L
33-
template <typename...>
34-
void test_p1061_structured_bindings() {
35-
auto [... empty] = std::make_index_sequence<0>();
36-
static_assert(sizeof...(empty) == 0);
37-
38-
auto [... size4] = std::make_index_sequence<4>();
39-
static_assert(sizeof...(size4) == 4);
40-
41-
assert(size4...[0] == 0);
42-
assert(size4...[1] == 1);
43-
assert(size4...[2] == 2);
44-
assert(size4...[3] == 3);
45-
}
32+
[]<typename...> {
33+
auto [... empty] = std::make_index_sequence<0>();
34+
static_assert(sizeof...(empty) == 0);
35+
36+
auto [... size4] = std::make_index_sequence<4>();
37+
static_assert(sizeof...(size4) == 4);
38+
39+
assert(size4...[0] == 0);
40+
assert(size4...[1] == 1);
41+
assert(size4...[2] == 2);
42+
assert(size4...[3] == 3);
43+
}();
4644
#endif
4745

46+
return true;
47+
}
48+
4849
int main(int, char**) {
4950
test_structured_bindings();
50-
#if __cpp_structured_bindings >= 202411L
51-
test_p1061_structured_bindings();
52-
#endif
51+
static_assert(test_structured_bindings());
5352
return 0;
5453
}

libcxx/test/std/utilities/intseq/intseq.binding/tuple_interface.compile.pass.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include <type_traits>
2626
#include <utility>
2727

28-
constexpr void test() {
28+
void test() {
2929
// std::tuple_size_v
3030
using empty = std::integer_sequence<int>;
3131
static_assert(std::tuple_size_v<empty> == 0);
@@ -47,14 +47,12 @@ constexpr void test() {
4747
static_assert(std::is_same_v<std::tuple_element_t<3, const size4>, int>);
4848

4949
// std::get
50-
constexpr static size4 seq4{};
51-
static_assert(get<0>(seq4) == 9);
50+
constexpr static size4 seq4;
51+
constexpr std::same_as<int> decltype(auto) elt0 = get<0>(seq4);
52+
static_assert(elt0 == 9);
5253
static_assert(get<1>(seq4) == 8);
5354
static_assert(get<2>(seq4) == 7);
5455
static_assert(get<3>(seq4) == 2);
5556

5657
static_assert(noexcept(get<0>(seq4)));
57-
58-
constexpr std::same_as<int> decltype(auto) r = get<0>(seq4);
59-
static_assert(r == 9);
6058
}

libcxx/test/std/utilities/intseq/intseq.binding/tuple_interface.verify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void f() {
2727
// expected-error-re@*:* {{static assertion failed{{.*}}Index out of bounds in std::tuple_element<> (const std::integer_sequence)}}
2828
using test2 = std::tuple_element_t<0, const std::integer_sequence<int>>;
2929

30-
auto empty = std::integer_sequence<int>();
30+
std::integer_sequence<int> empty;
3131
// expected-error-re@*:* {{static assertion failed{{.*}}Index out of bounds in std::get<> (std::integer_sequence)}}
3232
// expected-error-re@*:* {{invalid index 0 for pack {{.*}} of size 0}}
3333
(void)std::get<0>(empty);

0 commit comments

Comments
 (0)