[libcxx-commits] [libcxx] [libc++] Implement `std::function_ref` (PR #186692)
via libcxx-commits
libcxx-commits at lists.llvm.org
Wed May 13 03:09:00 PDT 2026
================
@@ -0,0 +1,115 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// template<class T> function_ref& operator=(T) = delete;
+
+#include <cassert>
+#include <functional>
+#include <utility>
+#include <type_traits>
+
+#include "test_macros.h"
+
+// Constraints:
+// - T is not the same type as function_ref,
+// - is_pointer_v<T> is false, and
+// - T is not a specialization of constant_wrapper.
+
+// non const noexcept(false)
+static_assert(std::is_assignable_v<std::function_ref<void()>, std::function_ref<void()>>);
+static_assert(std::is_assignable_v<std::function_ref<void()>, std::function_ref<void() const>>);
+static_assert(std::is_assignable_v<std::function_ref<void()>, std::function_ref<void() noexcept>>);
+static_assert(std::is_assignable_v<std::function_ref<void()>, std::function_ref<void() const noexcept>>);
+
+static_assert(std::is_assignable_v<std::function_ref<void()>, void (*)()>);
+static_assert(!std::is_assignable_v<std::function_ref<void()>, void (*)(int)>);
+
+static_assert(std::is_assignable_v<std::function_ref<void()>, std::constant_wrapper<[] {}>>);
+static_assert(!std::is_assignable_v<std::function_ref<void()>, std::constant_wrapper<[](int) {}>>);
+
+// const noexcept(false)
+static_assert(std::is_assignable_v<std::function_ref<void() const>, std::function_ref<void()>>);
----------------
Kim-J-Smith wrote:
The implementation is missing the second constraint from [func.wrap.ref.ctor]/2:
`is_convertible_v<int cv&, int cv2&>`. As a result, we incorrectly allow
conversion from `function_ref<void()>` to `function_ref<void() const>`,
which should be rejected because `int const&` cannot convert to `int&`.
https://github.com/llvm/llvm-project/pull/186692
More information about the libcxx-commits
mailing list