[libcxx-commits] [libcxx] WIP [libc++][ranges] Applied [[nodiscard]] to adjacent_transform_view (PR #205900)
Lucas Mellone via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Jun 27 17:18:24 PDT 2026
https://github.com/lknknm updated https://github.com/llvm/llvm-project/pull/205900
>From 65046f1524740c0e4a7829b61cd6a7601c34cfa5 Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Thu, 25 Jun 2026 21:47:55 +0200
Subject: [PATCH 1/6] initial: nodiscard adjacent_transform_view
---
.../include/__ranges/adjacent_transform_view.h | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/libcxx/include/__ranges/adjacent_transform_view.h b/libcxx/include/__ranges/adjacent_transform_view.h
index 4863d074482dc..bdae098623977 100644
--- a/libcxx/include/__ranges/adjacent_transform_view.h
+++ b/libcxx/include/__ranges/adjacent_transform_view.h
@@ -105,22 +105,23 @@ class adjacent_transform_view : public view_interface<adjacent_transform_view<_V
_LIBCPP_HIDE_FROM_ABI constexpr explicit adjacent_transform_view(_View __base, _Fn __fun)
: __inner_(std::move(__base)), __fun_(std::in_place, std::move(__fun)) {}
- _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
requires copy_constructible<_View>
{
return __inner_.base();
}
- _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__inner_).base(); }
- _LIBCPP_HIDE_FROM_ABI constexpr auto begin() { return __iterator<false>(*this, __inner_.begin()); }
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__inner_).base(); }
- _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() { return __iterator<false>(*this, __inner_.begin()); }
+
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
requires range<const _InnerView> && regular_invocable<__apply_n<const _Fn&, _Np>, range_reference_t<const _View>>
{
return __iterator<true>(*this, __inner_.begin());
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto end() {
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() {
if constexpr (common_range<_InnerView>) {
return __iterator<false>(*this, __inner_.end());
} else {
@@ -128,7 +129,7 @@ class adjacent_transform_view : public view_interface<adjacent_transform_view<_V
}
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
requires range<const _InnerView> && regular_invocable<__apply_n<const _Fn&, _Np>, range_reference_t<const _View>>
{
if constexpr (common_range<const _InnerView>) {
@@ -138,13 +139,13 @@ class adjacent_transform_view : public view_interface<adjacent_transform_view<_V
}
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto size()
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size()
requires sized_range<_InnerView>
{
return __inner_.size();
}
- _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
requires sized_range<const _InnerView>
{
return __inner_.size();
>From 0b582345030b80efba493370df6595f56abdaf64 Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Thu, 25 Jun 2026 23:51:42 +0200
Subject: [PATCH 2/6] Update libcxx/include/__ranges/adjacent_transform_view.h
Co-authored-by: Hristo Hristov <zingam at outlook.com>
---
libcxx/include/__ranges/adjacent_transform_view.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/libcxx/include/__ranges/adjacent_transform_view.h b/libcxx/include/__ranges/adjacent_transform_view.h
index bdae098623977..5d22042e9c78f 100644
--- a/libcxx/include/__ranges/adjacent_transform_view.h
+++ b/libcxx/include/__ranges/adjacent_transform_view.h
@@ -114,7 +114,6 @@ class adjacent_transform_view : public view_interface<adjacent_transform_view<_V
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__inner_).base(); }
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() { return __iterator<false>(*this, __inner_.begin()); }
-
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
requires range<const _InnerView> && regular_invocable<__apply_n<const _Fn&, _Np>, range_reference_t<const _View>>
{
>From acc5961454c76807c11297fc8ac970135d47f69f Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Sat, 27 Jun 2026 17:50:55 +0200
Subject: [PATCH 3/6] complete: nodiscard to adjacent_transform_view.h
---
.../include/__ranges/adjacent_transform_view.h | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/libcxx/include/__ranges/adjacent_transform_view.h b/libcxx/include/__ranges/adjacent_transform_view.h
index 5d22042e9c78f..f8c4cd716a8d5 100644
--- a/libcxx/include/__ranges/adjacent_transform_view.h
+++ b/libcxx/include/__ranges/adjacent_transform_view.h
@@ -202,7 +202,7 @@ class adjacent_transform_view<_View, _Fn, _Np>::__iterator {
requires _Const && convertible_to<__inner_iterator<false>, __inner_iterator<true>>
: __parent_(__i.__parent_), __inner_(std::move(__i.__inner_)) {}
- _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const
noexcept(__noexcept_dereference(make_index_sequence<_Np>{})) {
return std::apply(
[&](const auto&... __iters) -> decltype(auto) { return std::invoke(*__parent_->__fun_, *__iters...); },
@@ -249,7 +249,7 @@ class adjacent_transform_view<_View, _Fn, _Np>::__iterator {
return *this;
}
- _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator[](difference_type __n) const
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator[](difference_type __n) const
requires random_access_range<_Base>
{
return std::apply(
@@ -291,25 +291,25 @@ class adjacent_transform_view<_View, _Fn, _Np>::__iterator {
return __x.__inner_ <=> __y.__inner_;
}
- _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(const __iterator& __i, difference_type __n)
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(const __iterator& __i, difference_type __n)
requires random_access_range<_Base>
{
return __iterator(*__i.__parent_, __i.__inner_ + __n);
}
- _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(difference_type __n, const __iterator& __i)
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(difference_type __n, const __iterator& __i)
requires random_access_range<_Base>
{
return __iterator(*__i.__parent_, __i.__inner_ + __n);
}
- _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator-(const __iterator& __i, difference_type __n)
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator-(const __iterator& __i, difference_type __n)
requires random_access_range<_Base>
{
return __iterator(*__i.__parent_, __i.__inner_ - __n);
}
- _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y)
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y)
requires sized_sentinel_for<__inner_iterator<_Const>, __inner_iterator<_Const>>
{
return __x.__inner_ - __y.__inner_;
@@ -344,14 +344,14 @@ class adjacent_transform_view<_View, _Fn, _Np>::__sentinel {
template <bool _OtherConst>
requires sized_sentinel_for<__inner_sentinel<_Const>, __inner_iterator<_OtherConst>>
- _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _InnerView>>
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _InnerView>>
operator-(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
return __x.__inner_ - __y.__inner_;
}
template <bool _OtherConst>
requires sized_sentinel_for<__inner_sentinel<_Const>, __inner_iterator<_OtherConst>>
- _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _InnerView>>
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _InnerView>>
operator-(const __sentinel& __x, const __iterator<_OtherConst>& __y) {
return __x.__inner_ - __y.__inner_;
}
@@ -371,7 +371,7 @@ struct __fn : __range_adaptor_closure<__fn<_Np>> {
}
template <class _Range, class _Fn>
- _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Range&& __range, _Fn&& __fn) noexcept(
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Range&& __range, _Fn&& __fn) noexcept(
noexcept(adjacent_transform_view<views::all_t<_Range&&>, decay_t<_Fn>, _Np>(
std::forward<_Range>(__range), std::forward<_Fn>(__fn))))
-> decltype(adjacent_transform_view<views::all_t<_Range&&>, decay_t<_Fn>, _Np>(
>From 15e15ee15ad0e6580dfff6f8fd3da6fbb9255854 Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Sat, 27 Jun 2026 17:51:04 +0200
Subject: [PATCH 4/6] complete: nodiscard to adjacent_transform_view.h
---
libcxx/include/__ranges/adjacent_transform_view.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/libcxx/include/__ranges/adjacent_transform_view.h b/libcxx/include/__ranges/adjacent_transform_view.h
index f8c4cd716a8d5..10bf812607b3e 100644
--- a/libcxx/include/__ranges/adjacent_transform_view.h
+++ b/libcxx/include/__ranges/adjacent_transform_view.h
@@ -309,7 +309,8 @@ class adjacent_transform_view<_View, _Fn, _Np>::__iterator {
return __iterator(*__i.__parent_, __i.__inner_ - __n);
}
- [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y)
+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type
+ operator-(const __iterator& __x, const __iterator& __y)
requires sized_sentinel_for<__inner_iterator<_Const>, __inner_iterator<_Const>>
{
return __x.__inner_ - __y.__inner_;
>From 287a7d06ae3ca47191a279c987f60137bd0e5761 Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Sat, 27 Jun 2026 18:24:55 +0200
Subject: [PATCH 5/6] add: initial test file
---
.../nodiscard.verify.cpp | 20 +++++++++++++++++++
1 file changed, 20 insertions(+)
create mode 100644 libcxx/test/libcxx/ranges/range.adaptors/range.adjacent.transform/nodiscard.verify.cpp
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.adjacent.transform/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.adjacent.transform/nodiscard.verify.cpp
new file mode 100644
index 0000000000000..6a26fa8bfda85
--- /dev/null
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.adjacent.transform/nodiscard.verify.cpp
@@ -0,0 +1,20 @@
+
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// Test the libc++ extension that std::ranges::adjacent_transform_view and std::views::adjacent_transform are marked as [[nodiscard]].
+
+#include <ranges>
+#include <utility>
+#include <functional>
+
+void test(){
+
+}
>From 3aee8e6f2e9c2c3f291da0f79993c161c7c3189d Mon Sep 17 00:00:00 2001
From: Lucas Mellone <github.snugness349 at passinbox.com>
Date: Sun, 28 Jun 2026 02:18:05 +0200
Subject: [PATCH 6/6] add: test view
---
.../range.adjacent.transform/nodiscard.verify.cpp | 10 ++++++++++
1 file changed, 10 insertions(+)
diff --git a/libcxx/test/libcxx/ranges/range.adaptors/range.adjacent.transform/nodiscard.verify.cpp b/libcxx/test/libcxx/ranges/range.adaptors/range.adjacent.transform/nodiscard.verify.cpp
index 6a26fa8bfda85..459b132766ae0 100644
--- a/libcxx/test/libcxx/ranges/range.adaptors/range.adjacent.transform/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/ranges/range.adaptors/range.adjacent.transform/nodiscard.verify.cpp
@@ -15,6 +15,16 @@
#include <utility>
#include <functional>
+struct View : std::ranges::view_interface<View> {
+ int* begin();
+ const int* begin() const;
+ volatile int* end();
+ const volatile 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(){
}
More information about the libcxx-commits
mailing list