[libcxx-commits] [libcxx] [libc++] Partially implement P2846R6: `reserve_hint` (PR #206385)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Thu Aug 6 02:02:14 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() {
+  SizedSentinelRange b;
+  assert(std::ranges::reserve_hint(b) == 42);
+  ASSERT_SAME_TYPE(decltype(std::ranges::reserve_hint(b)), std::size_t);
----------------
Zingam wrote:

```suggestion
  std::same_as<std::size_t> decltype(auto) result = std::ranges::reserve_hint(b);
  static_assert(noexcept(std::ranges::reserve_hint(b));
  
  assert(result == 42);
```
FYI: For a while it is preferred to use the above concept-based pattern instead of the macro-based one. (no change requested... apply it on your own discretion)
But what I think is missing here are test cases for `noexcept`-ess .


https://github.com/llvm/llvm-project/pull/206385


More information about the libcxx-commits mailing list