[libcxx-commits] [libcxx] [libc++] P0792R14: `std::function_ref` (PR #94894)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Mar 17 07:11:12 PDT 2026
================
@@ -0,0 +1,186 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// This header is unguarded on purpose. This header is an implementation detail of function_ref.h
+// and generates multiple versions of std::function_ref
+
+#include <__assert>
+#include <__config>
+#include <__functional/invoke.h>
+#include <__memory/addressof.h>
+#include <__type_traits/conditional.h>
+#include <__type_traits/invoke.h>
+#include <__type_traits/is_const.h>
+#include <__type_traits/is_function.h>
+#include <__type_traits/is_member_pointer.h>
+#include <__type_traits/is_object.h>
+#include <__type_traits/is_pointer.h>
+#include <__type_traits/is_reference.h>
+#include <__type_traits/is_same.h>
+#include <__type_traits/is_void.h>
+#include <__type_traits/remove_cvref.h>
+#include <__type_traits/remove_pointer.h>
+#include <__type_traits/remove_reference.h>
+#include <__utility/constant_arg.h>
+#include <__utility/forward.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+#ifndef _LIBCPP___FUNCTIONAL_FUNCTION_REF_H
+# error This header should only be included from function_ref.h
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 26
+
+template <class...>
+class function_ref;
+
+template <class _Rp, class... _ArgTypes, bool __is_noexcept>
+class function_ref<_Rp(_ArgTypes...) _LIBCPP_FUNCTION_REF_CV noexcept(__is_noexcept)> {
+private:
+ template <class... _Tp>
+ static constexpr bool __is_invocable_using =
+ _If<__is_noexcept, is_nothrow_invocable_r<_Rp, _Tp..., _ArgTypes...>, is_invocable_r<_Rp, _Tp..., _ArgTypes...>>::
+ value;
+
+ // use a union instead of a plain `void*` to avoid dropping const qualifiers and casting function pointers to data
+ // pointers
+ // todo: libstdc++ does not support volatile objects. shall we support it? the standard does not say it should not be
+ // supported
+ union __storage_t {
+ void* __obj_ptr_;
+ void const* __obj_const_ptr_;
+
+ _LIBCPP_HIDE_FROM_ABI constexpr explicit __storage_t() noexcept : __obj_ptr_(nullptr) {}
+
+ template <class _Tp>
+ _LIBCPP_HIDE_FROM_ABI constexpr explicit __storage_t(_Tp* __ptr) noexcept {
+ if constexpr (is_object_v<_Tp>) {
+ if constexpr (is_const_v<_Tp>) {
+ __obj_const_ptr_ = __ptr;
+ } else {
+ __obj_ptr_ = __ptr;
+ }
+ } else {
+ static_assert(is_function_v<_Tp>);
+ __obj_ptr_ = reinterpret_cast<void*>(__ptr);
+ }
+ }
+ };
+
+ template <class _Tp>
+ _LIBCPP_HIDE_FROM_ABI static constexpr auto __get(__storage_t __storage) {
+ if constexpr (is_object_v<_Tp>) {
+ if constexpr (is_const_v<_Tp>) {
+ return static_cast<_Tp*>(__storage.__obj_const_ptr_);
+ } else {
+ return static_cast<_Tp*>(__storage.__obj_ptr_);
+ }
+ } else {
+ static_assert(is_function_v<_Tp>);
+ return reinterpret_cast<_Tp*>(__storage.__obj_ptr_);
+ }
+ }
+
+ __storage_t __storage_;
+
+ using __call_t _LIBCPP_NODEBUG = _Rp (*)(__storage_t, _ArgTypes&&...) noexcept(__is_noexcept);
+ __call_t __call_;
+
+public:
+ template <class _Fp>
+ requires is_function_v<_Fp> && __is_invocable_using<_Fp>
+ _LIBCPP_HIDE_FROM_ABI function_ref(_Fp* __fn_ptr) noexcept
+ : __storage_(__fn_ptr),
+ __call_([](__storage_t __storage, _ArgTypes&&... __args) static noexcept(__is_noexcept) -> _Rp {
+ return __get<_Fp>(__storage)(std::forward<_ArgTypes>(__args)...);
+ }) {
+ _LIBCPP_ASSERT_NON_NULL(__fn_ptr != nullptr, "the function pointer should not be a nullptr");
+ }
+
+ template <class _Fn, class _Tp = remove_reference_t<_Fn>>
+ requires(!is_same_v<remove_cvref_t<_Fn>, function_ref> && !is_member_pointer_v<_Tp> &&
+ __is_invocable_using<_LIBCPP_FUNCTION_REF_CV _Tp&>)
+ _LIBCPP_HIDE_FROM_ABI constexpr function_ref(_Fn&& __obj) noexcept
----------------
huixie90 wrote:
TODO:
If `__obj` is another `function_ref`, we should unwrap it and copy the pointers
https://github.com/llvm/llvm-project/pull/94894
More information about the libcxx-commits
mailing list