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

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Aug 14 05:56:43 PDT 2026


================
@@ -0,0 +1,63 @@
+//===----------------------------------------------------------------------===//
+//
+// 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<V>;
+// constexpr auto reserve_hint() const
+//     requires approximately_sized_range<const V>;
+
+#include <cassert>
+#include <ranges>
+
+#include "test_iterators.h"
+#include "types.h"
+
+template <class View>
+concept ReserveHintEnabled = requires(View v) { v.reserve_hint(); };
+
+constexpr bool test() {
+  int buf[8] = {1, 2, 3, 4, 5, 6, 7, 8};
+
+  {
+    static_assert(ReserveHintEnabled<std::ranges::common_view<ApproximatelySizedView>&>);
+    static_assert(ReserveHintEnabled<std::ranges::common_view<ApproximatelySizedView> const&>);
+    static_assert(!ReserveHintEnabled<std::ranges::common_view<CopyableView>&>);
+    static_assert(!ReserveHintEnabled<std::ranges::common_view<CopyableView> const&>);
+    static_assert(ReserveHintEnabled<std::ranges::common_view<NonConstApproximatelySizedView>&>);
+    static_assert(!ReserveHintEnabled<std::ranges::common_view<NonConstApproximatelySizedView> const&>);
+  }
----------------
inquisitivecrystal wrote:

I don't think that's how static_assert works. As I understand it, it's called only once when the function is parsed, since it's not a template function. If it were a template function it would be called for each instantiation. I'm not an expert though. 

But on the subject of whether this should actually be here... it's exactly paralleling the sibling size test, which is why I did it, but it seems a bit messy to me too. I can remove it.

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


More information about the libcxx-commits mailing list