[libcxx-commits] [libcxx] [libc++] Implement `std::function_ref` (PR #186692)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jun 19 11:18:45 PDT 2026


================
@@ -0,0 +1,158 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 function_ref& operator=(const function_ref&) noexcept = default;
+
+#include <cassert>
+#include <functional>
+#include <utility>
+#include <type_traits>
+
+#include "test_macros.h"
+
+static_assert(std::is_copy_assignable_v<std::function_ref<void()>>);
+static_assert(std::is_copy_assignable_v<std::function_ref<void() const>>);
+static_assert(std::is_copy_assignable_v<std::function_ref<void() noexcept>>);
+static_assert(std::is_copy_assignable_v<std::function_ref<void() const noexcept>>);
+
+double plus(int x, double y) noexcept { return x + y; }
+double minus(int x, double y) noexcept { return x - y; }
+
+struct Int {
+  int i;
+  constexpr Int(int ii) noexcept : i(ii) {}
+};
+
+struct NeedsConversion {
+  int operator()(Int x, Int y, Int z) const noexcept { return x.i + y.i + z.i; }
+};
+
+int needs_conversion(Int x, Int y, Int z) noexcept { return x.i + y.i + z.i; }
+int zero(Int, Int, Int) noexcept { return 0; }
+
+constexpr bool test() {
+  {
+    std::function_ref<void()> f(std::cw<[] {}>);
+    std::function_ref<void()> f2(std::cw<[] {}>);
+    f2 = f;
----------------
ldionne wrote:

Same comment here about testing return type + value.

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


More information about the libcxx-commits mailing list