[libcxx-commits] [libcxx] [libc++] Implement ranges::iota (PR #68494)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Aug 5 12:53:36 PDT 2024
================
@@ -0,0 +1,215 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// Testing std::ranges::iota
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+#include <algorithm>
+#include <array>
+#include <cassert>
+#include <numeric>
+#include <utility>
+
+#include "almost_satisfies_types.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+
+//
+// Testing constraints
+//
+
+// Concepts to check different overloads of std::ranges::iota
+template <class Iter = int*, class Sent = int*, class Value = int>
+concept HasIotaIter = requires(Iter&& iter, Sent&& sent, Value&& val) {
+ std::ranges::iota(std::forward<Iter>(iter), std::forward<Sent>(sent), std::forward<Value>(val));
+};
+
+template <class Range, class Value = int>
+concept HasIotaRange =
+ requires(Range&& range, Value&& val) { std::ranges::iota(std::forward<Range>(range), std::forward<Value>(val)); };
+
+// Test constraints of the iterator/sentinel overload
+// ==================================================
+static_assert(HasIotaIter<int*, int*, int>);
+
+// !input_or_output_iterator<O>
+static_assert(!HasIotaIter<InputIteratorNotInputOrOutputIterator>);
+
+// !sentinel_for<S, O>
+static_assert(!HasIotaIter<int*, SentinelForNotSemiregular>);
+static_assert(!HasIotaIter<int*, SentinelForNotWeaklyEqualityComparableWith>);
+
+// !weakly_incrementable<T>
+static_assert(!HasIotaIter<int*, int*, WeaklyIncrementableNotMovable>);
+
+// !indirectly writable <O, T>
+static_assert(!HasIotaIter<OutputIteratorNotIndirectlyWritable, int*, int>);
+
+// Test constraints for the range overload
+// =======================================
+static_assert(HasIotaRange<UncheckedRange<int*>, int>);
+
+// !weakly_incrementable<T>
+static_assert(!HasIotaRange<UncheckedRange<int*>, WeaklyIncrementableNotMovable>);
+
+// !ranges::output_range<const _Tp&>
+static_assert(!HasIotaRange<UncheckedRange<int*>, OutputIteratorNotIndirectlyWritable>);
----------------
philnik777 wrote:
> How frequently do we (libc++) need to update our existing verification tests?
I'm not exactly sure, but there is probably something every 3-4 months on average.
> > Anyways, this would require discussion beyond the current patch, since that's how we've done it for all the other ranges algorithms.
>
> We should be committed to doing things right the first time, not to remaining consistent with a status quo in need of change. If that means deviating from what's currently done, that's fine. We can have the discussion afterwards, using this PR as the pilot commit.
I absolutely disagree here. "Just put it in sneakily" is _much_ worse than having imperfect code commited. I've seen before that you suggest stuff like `_LIBCPP_ALWAYS_INLINE` or `[[nodiscard]]`. I don't think it's OK for this kind of stuff to be added and then ask for forgiveness. This has significantly eroded my confidence that you give reasonable reviews, especially since people pushed back on these things before.
Just to make it clear: What I _am_ OK with is having a suggestion and asking for other people to give their thoughts on it. If there is no disagreement, fine.
> I'd really prefer to see this PR landed with maximum confidence in correctness, and also not block @jamesETsmith on a policy discussion.
I agree that we should not block this on a policy discussion. That's why I think we should move on with it as-is and not add a `.verify.cpp`. As I said, I don't think it's a good idea and I would block based on this.
https://github.com/llvm/llvm-project/pull/68494
More information about the libcxx-commits
mailing list