[libcxx-commits] [libcxx] [libc++][c++23] P2374: `views::cartesian_product` (PR #111215)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Apr 17 23:09:17 PDT 2026
================
@@ -0,0 +1,106 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// constexpr iterator<false> begin() requires (!simple-view<First> || ... || !simple-view <Vs>);
+// constexpr iterator<true > begin() const requires (range<const First> && ... && range<const Vs>);
+
+#include <ranges>
+#include <cassert>
+#include <concepts>
+#include <tuple>
+#include <utility>
+
+#include "../range.zip/types.h"
+
+template <class T>
+concept HasConstBegin = requires(const T& ct) { ct.begin(); };
+
+template <class T>
+concept HasNonConstBegin = requires(T& t) { t.begin(); };
+
+template <class T>
+concept HasConstAndNonConstBegin = HasConstBegin<T> && HasNonConstBegin<T> && requires(T& t, const T& ct) {
+ requires !std::same_as<decltype(t.begin()), decltype(ct.begin())>;
+};
+
+template <class T>
+concept HasOnlyNonConstBegin = HasNonConstBegin<T> && !HasConstBegin<T>;
+
+template <class T>
+concept HasOnlyConstBegin = HasConstBegin<T> && !HasConstAndNonConstBegin<T>;
+
+struct NoConstBeginView : std::ranges::view_base {
+ int* begin();
+ int* end();
+};
+
+constexpr bool test() {
+ int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+ { // all underlying iterators should be at the begin position
+ std::ranges::cartesian_product_view v(
+ SizedRandomAccessView{buffer}, std::views::iota(0), std::ranges::single_view(2.0));
+ std::same_as<std::tuple<int&, int, double&>> decltype(auto) mutVal = *v.begin();
+ std::same_as<std::tuple<int&, int, const double&>> decltype(auto) constVal = *std::as_const(v).begin();
+ assert(mutVal == std::make_tuple(1, 0, 2.0));
+ assert(constVal == std::make_tuple(1, 0, 2.0));
+ assert(&(std::get<0>(mutVal)) == &buffer[0]);
+ assert(&(std::get<0>(constVal)) == &buffer[0]);
----------------
H-G-Hristov wrote:
IMO. Not separating const and non-const tests would be clearer. And I think it's the usual style.
https://github.com/llvm/llvm-project/pull/111215
More information about the libcxx-commits
mailing list