Skip to content

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented Aug 28, 2025

No description provided.

Copy link
Contributor Author

arsenm commented Aug 28, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@arsenm arsenm marked this pull request as ready for review August 28, 2025 13:09
@llvmbot
Copy link
Member

llvmbot commented Aug 28, 2025

@llvm/pr-subscribers-llvm-adt

Author: Matt Arsenault (arsenm)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/155830.diff

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/STLFunctionalExtras.h (+8-8)
diff --git a/llvm/include/llvm/ADT/STLFunctionalExtras.h b/llvm/include/llvm/ADT/STLFunctionalExtras.h
index a4d50dc3648be..2c8d4e07fa5c0 100644
--- a/llvm/include/llvm/ADT/STLFunctionalExtras.h
+++ b/llvm/include/llvm/ADT/STLFunctionalExtras.h
@@ -41,18 +41,18 @@ class LLVM_GSL_POINTER function_ref<Ret(Params...)> {
   Ret (*callback)(intptr_t callable, Params ...params) = nullptr;
   intptr_t callable;
 
-  template<typename Callable>
-  static Ret callback_fn(intptr_t callable, Params ...params) {
+  template <typename Callable>
+  static constexpr Ret callback_fn(intptr_t callable, Params... params) {
     return (*reinterpret_cast<Callable*>(callable))(
         std::forward<Params>(params)...);
   }
 
 public:
-  function_ref() = default;
-  function_ref(std::nullptr_t) {}
+  constexpr function_ref() = default;
+  constexpr function_ref(std::nullptr_t) {}
 
   template <typename Callable>
-  function_ref(
+  constexpr function_ref(
       Callable &&callable LLVM_LIFETIME_BOUND,
       // This is not the copy-constructor.
       std::enable_if_t<!std::is_same<remove_cvref_t<Callable>,
@@ -65,13 +65,13 @@ class LLVM_GSL_POINTER function_ref<Ret(Params...)> {
       : callback(callback_fn<std::remove_reference_t<Callable>>),
         callable(reinterpret_cast<intptr_t>(&callable)) {}
 
-  Ret operator()(Params ...params) const {
+  constexpr Ret operator()(Params... params) const {
     return callback(callable, std::forward<Params>(params)...);
   }
 
-  explicit operator bool() const { return callback; }
+  explicit constexpr operator bool() const { return callback; }
 
-  bool operator==(const function_ref<Ret(Params...)> &Other) const {
+  constexpr bool operator==(const function_ref<Ret(Params...)> &Other) const {
     return callable == Other.callable;
   }
 };

Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a unit test that shows how this is supposed to be used? I'm surprised using reinterpret_cast is allowed...

@jurahul
Copy link
Contributor

jurahul commented Aug 28, 2025

Right, my earlier attempts to do the same ran into compiler errors, see https://discourse.llvm.org/t/constexpr-function-ref/87516

@arsenm
Copy link
Contributor Author

arsenm commented Aug 28, 2025

Don't care enough about this to put any effort into it

@arsenm arsenm closed this Aug 28, 2025
@arsenm arsenm deleted the users/arsenm/adt/make-function_ref-constexpr branch August 28, 2025 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants