[RFC] libstdc++: Use !enable_view in viewable_range concept.
Tomasz Kamiński
tkaminsk@redhat.com
Mon Nov 9 15:03:40 GMT 2026
This implements P4463R0, "enable_view shouldn't be ignored".
Use !enable_view for non-view branch, so it does not apply for the
types for which it is true, but do not satisfy moveable (thus they do
not satsify view), like optional<const int>. In consequence we no longer
silently form an reference to such "view" type.
libstdc++-v3/ChangeLog:
* include/bits/ranges_base.h (ranges::viewable_range): Replace
!view<remove_cvref_t<_Tp> with !enable_view<remove_cvref_t<_Tp>>.
* testsuite/20_util/optional/range.cc: Disable tests for optionals
of non-movable types, are corresponding assert.
* testsuite/20_util/optional/range_neg.cc: New test.
---
Implementation experience for above paper, not targetting trunk.
Tested on powerpc64le and x86_64-linux.
libstdc++-v3/include/bits/ranges_base.h | 2 +-
.../testsuite/20_util/optional/range.cc | 30 +++++++-------
.../testsuite/20_util/optional/range_neg.cc | 39 +++++++++++++++++++
3 files changed, 54 insertions(+), 17 deletions(-)
create mode 100644 libstdc++-v3/testsuite/20_util/optional/range_neg.cc
diff --git a/libstdc++-v3/include/bits/ranges_base.h b/libstdc++-v3/include/bits/ranges_base.h
index 7f4abbec341..7062ae7434a 100644
--- a/libstdc++-v3/include/bits/ranges_base.h
+++ b/libstdc++-v3/include/bits/ranges_base.h
@@ -873,7 +873,7 @@ namespace ranges
template<typename _Tp>
concept viewable_range = range<_Tp>
&& ((view<remove_cvref_t<_Tp>> && constructible_from<remove_cvref_t<_Tp>, _Tp>)
- || (!view<remove_cvref_t<_Tp>>
+ || (!enable_view<remove_cvref_t<_Tp>>
&& (is_lvalue_reference_v<_Tp>
|| (movable<remove_reference_t<_Tp>>
&& !__detail::__is_initializer_list<remove_cvref_t<_Tp>>))));
diff --git a/libstdc++-v3/testsuite/20_util/optional/range.cc b/libstdc++-v3/testsuite/20_util/optional/range.cc
index 4238c9cf28e..942c3ea522f 100644
--- a/libstdc++-v3/testsuite/20_util/optional/range.cc
+++ b/libstdc++-v3/testsuite/20_util/optional/range.cc
@@ -291,12 +291,12 @@ all_tests()
int arr[10]{};
NonMovable nm;
NonAssignable na;
+
+ test(i);
test(&i);
test(std::string_view("test"));
test(std::vector<int>{1, 2, 3, 4});
test(std::optional<int>(42));
- test<const int>(42);
-
test<int&>(i);
test<const int&>(i);
test<int(&)[10]>(arr);
@@ -313,36 +313,34 @@ all_tests()
range_chain_example();
test_as_const<false, int>(i);
- test_as_const<false, const int>(i);
test_as_const<true, int&>(i);
test_as_const<true, const int&>(i);
- test_as_const<false, NonMovable, int>(10);
- test_as_const<false, const NonMovable, int>(10);
- test_as_const<true, NonMovable&>(nm);
- test_as_const<true, const NonMovable&>(nm);
- test_as_const<false, NonAssignable>({});
- test_as_const<false, const NonAssignable>({});
test_as_const<true, NonAssignable&>(na);
test_as_const<true, const NonAssignable&>(na);
+ test_as_const<true, NonMovable&>(nm);
+ test_as_const<true, const NonMovable&>(nm);
#define TEST_ADAPTOR(name) \
test_##name<true, int>(i); \
- test_##name<false, const int>(i); \
test_##name<true, int&>(i); \
test_##name<true, const int&>(i); \
- test_##name<false, NonMovable, int>(10); \
- test_##name<false, const NonMovable, int>(10); \
+ test_##name<true, NonAssignable&>(na); \
+ test_##name<true, const NonAssignable&>(na); \
test_##name<true, NonMovable&>(nm); \
test_##name<true, const NonMovable&>(nm); \
- test_##name<false, NonAssignable>({}); \
- test_##name<false, const NonAssignable>({}); \
- test_##name<true, NonAssignable&>(na); \
- test_##name<true, const NonAssignable&>(na)
TEST_ADAPTOR(reverse);
TEST_ADAPTOR(take);
TEST_ADAPTOR(drop);
#undef TEST_ADAPTOR
+
+ static_assert( !std::ranges::viewable_range<std::optional<const int>> );
+ static_assert( !std::ranges::viewable_range<std::optional<const int>&> );
+ static_assert( !std::ranges::viewable_range<std::optional<NonAssignable>> );
+ static_assert( !std::ranges::viewable_range<std::optional<NonAssignable>&> );
+ static_assert( !std::ranges::viewable_range<std::optional<NonMovable>> );
+ static_assert( !std::ranges::viewable_range<std::optional<NonMovable>&> );
+
return true;
}
diff --git a/libstdc++-v3/testsuite/20_util/optional/range_neg.cc b/libstdc++-v3/testsuite/20_util/optional/range_neg.cc
new file mode 100644
index 00000000000..8cbcf8fa36b
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/optional/range_neg.cc
@@ -0,0 +1,39 @@
+// { dg-do compile { target c++26 } }
+
+#include <map>
+#include <string>
+#include <optional>
+#include <ranges>
+
+int twice(int x)
+{ return x * 2; }
+
+constexpr auto
+int_rvalue()
+{
+ return std::optional<const int>(10) | std::views::transform(&twice); // { dg-error "here" }
+}
+
+constexpr auto
+int_lvalue()
+{
+ std::optional<const int> opt(10);
+ return opt | std::views::transform(&twice); // { dg-error "here" }
+}
+
+constexpr auto
+pair_rvalue()
+{
+ std::pair<const std::string, int> p("test", 20);
+ return std::optional(p) | std::views::keys; // { dg-error "here" }
+}
+
+constexpr auto
+pair_lvalue()
+{
+ std::pair<const std::string, int> p("test", 20);
+ std::optional opt(p);
+ return opt | std::views::keys; // { dg-error "here" }
+}
+
+// { dg-prune-output "no match for 'operator" }
--
2.56.0
More information about the Libstdc++
mailing list