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

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Thu Aug 13 06:40:17 PDT 2026


================
@@ -0,0 +1,131 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// constexpr auto reserve_hint()
+//     requires approximately_sized_range<InnerView>;
+// constexpr auto reserve_hint() const
+//     requires approximately_sized_range<const InnerView>;
+
+#include <cassert>
+#include <cstddef>
+#include <ranges>
+#include <utility>
+
+#include "helpers.h"
+#include "test_iterators.h"
+#include "test_macros.h"
+
+int buffer[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
+
+struct NonApproximatelySizedView : std::ranges::view_base {
+  using iterator = forward_iterator<int*>;
+  iterator begin() const;
+  iterator end() const;
+};
+
+struct ApproximatelySizedView : std::ranges::view_base {
+  unsigned int size_;
+  constexpr explicit ApproximatelySizedView(unsigned int size) : size_(size) {}
+  constexpr auto begin() const { return forward_iterator<int*>(buffer); }
+  constexpr auto end() const { return forward_iterator<int*>(buffer + size_); }
+  constexpr unsigned int reserve_hint() const { return size_; }
+};
+
+struct ApproximatelySizedNotConstView : std::ranges::view_base {
+  unsigned int size_;
+  constexpr explicit ApproximatelySizedNotConstView(unsigned int size) : size_(size) {}
+  constexpr auto begin() const { return forward_iterator<int*>(buffer); }
+  constexpr auto end() const { return forward_iterator<int*>(buffer + size_); }
+  constexpr unsigned int reserve_hint() { return size_; }
+};
----------------
philnik777 wrote:

Can we put these commonly used structs into a header instead?

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


More information about the libcxx-commits mailing list