[libcxx-commits] [libcxx] [libc++][ranges] Applied `[[nodiscard]]` to `views::zip_transform` (PR #207120)
Lucas Mellone via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Jul 4 14:55:30 PDT 2026
================
@@ -0,0 +1,88 @@
+//===----------------------------------------------------------------------===//
+//
+// 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++23
+
+// Check that functions are marked [[nodiscard]]
+
+#include <ranges>
+#include <utility>
+#include <vector>
+
+struct View : std::ranges::view_interface<View> {
+ int* begin();
+ const int* begin() const;
+ volatile int* end();
+ const volatile int* end() const;
+
+ int size() 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>>);
+
+template <class... Args>
+struct Invocable {
+ int operator()(Args...) const { return 5; }
+};
+
+void test() {
+ View range;
+ std::ranges::zip_transform_view ztv{[](int x) { return x; }, range};
+
+ // [range.zip_transform.view]
+
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ ztv.begin();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ std::as_const(ztv).begin();
+ // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+ ztv.end();
----------------
lknknm wrote:
I'm not sure if we should model also a CommonRange to test the `if constexpr (common_range<_InnerView>)` in:
```cpp
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() {
if constexpr (common_range<_InnerView>) {
return __iterator<false>(*this, __zip_.end());
} else {
return __sentinel<false>(__zip_.end());
}
}
```
Does that make sense?
https://github.com/llvm/llvm-project/pull/207120
More information about the libcxx-commits
mailing list