[libcxx-commits] [libcxx] [libc++] Partially implement P2846R6: `reserve_hint` (PR #206385)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Aug 7 03:55:09 PDT 2026
================
@@ -0,0 +1,105 @@
+//===----------------------------------------------------------------------===//
+//
+// 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++26
+
+// std::ranges::reserve_hint
+
+#include <cassert>
+#include <cstddef>
+#include <ranges>
+#include <type_traits>
+
+#include "test_iterators.h"
+#include "test_macros.h"
+
+using RangeReserveHintT = decltype(std::ranges::reserve_hint);
+
+struct Incomplete;
+static_assert(!std::is_invocable_v<RangeReserveHintT, Incomplete[]>);
+static_assert(!std::is_invocable_v<RangeReserveHintT, Incomplete (&)[]>);
+static_assert(!std::is_invocable_v<RangeReserveHintT, Incomplete (&&)[]>);
+
+extern int bounded_array[42];
+extern int unbounded_array[];
+
+struct SizedSentinelRange {
+ int data_[42] = {};
+ constexpr int* begin() { return data_; }
+ constexpr auto end() { return sized_sentinel<int*>(data_ + 42); }
+};
+
+struct HasSizeMember {
+ constexpr std::size_t size() { return 42; }
+};
+
+struct HasSizeFunction {
+ friend constexpr std::size_t size(HasSizeFunction) { return 42; }
+};
+
+struct HasReserveHintMember {
+ constexpr std::size_t reserve_hint() { return 42; }
+};
+
+struct HasReserveHintFunction {
+ friend constexpr std::size_t reserve_hint(HasReserveHintFunction) { return 42; }
+};
+
+struct HasReserveHintMemberBool {
+ constexpr bool reserve_hint() { return false; }
+};
+
+static_assert(!std::is_invocable_v<RangeReserveHintT, HasReserveHintMemberBool>);
+
+static_assert(std::ranges::reserve_hint(bounded_array) == 42);
+ASSERT_SAME_TYPE(decltype(std::ranges::reserve_hint(bounded_array)), std::size_t);
+
+static_assert(!std::is_invocable_v<RangeReserveHintT, decltype(unbounded_array)>);
+
+bool constexpr test_sized_sentinel_range() {
----------------
Zingam wrote:
This test bothers me a bit. Why not generalize this test function and do the runtime testing with the different range types? Something like:
```c++
template <typename T>
constexpr void test() {
....
}
constexpr bool test() {
test<HasSize>();
test<HasFunction>();
....
return true;
}
```
Does that make sense?
https://github.com/llvm/llvm-project/pull/206385
More information about the libcxx-commits
mailing list