[libcxx-commits] [libcxx] [libc++][ranges] Applied [[nodiscard]] to `elements_view` (PR #206589)
Lucas Mellone via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Jul 4 13:36:16 PDT 2026
================
@@ -0,0 +1,100 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++20
+
+// Test the libc++ extension that std::ranges::elements_view and std::views::elements are marked as [[nodiscard]].
+
+#include <functional>
+#include <map>
+#include <ranges>
+#include <utility>
+
+struct View : std::ranges::view_interface<View> {
+ std::tuple<int, int>* begin();
+ const std::tuple<int, int>* begin() const;
+ volatile std::tuple<int, int>* end();
+ const volatile std::tuple<int, int>* end() const;
+};
+static_assert(!std::ranges::common_range<View>);
+static_assert(!std::same_as<std::ranges::iterator_t<View>, std::ranges::iterator_t<const View>>);
+static_assert(!std::same_as<std::ranges::sentinel_t<View>, std::ranges::sentinel_t<const View>>);
+
+void test() {
+ auto v = View{} | std::views::elements<1>;
+
+ // [range.elements.view]
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::as_const(v).base();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::move(v).base();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ v.begin();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::as_const(v).begin();
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ v.end();
----------------
lknknm wrote:
I think it should be fine now, but I'm not quite sure if I'm correctly testing the `end() const` for common range in:
```cpp
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
requires common_range<const _View>
{
return __iterator</*_Const=*/true>{ranges::end(__base_)};
}
```
Could you please double check? Thanks!
https://github.com/llvm/llvm-project/pull/206589
More information about the libcxx-commits
mailing list