[llvm] [orc-rt] Allow move_only_function to capture by lvalue-reference. (PR #155716)

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 27 16:20:07 PDT 2025


https://github.com/lhames created https://github.com/llvm/llvm-project/pull/155716

Store std::decay_t<Callable> to ensure that we can initialize via lvalue references:

  auto NamedNoop = [](){};
   move_only_function<void()> Noop(NamedNoop); // <- no longer an error!

>From 3f162a0555b10dec85538bb046ae942f115c9f01 Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames at gmail.com>
Date: Wed, 27 Aug 2025 21:59:57 +1000
Subject: [PATCH] [orc-rt] Allow move_only_function to capture by
 lvalue-reference.

Store std::decay_t<Callable> to ensure that we can initialize via lvalue
references:

  auto NamedNoop = [](){};
   move_only_function<void()> Noop(NamedNoop); // <- no longer an error!
---
 orc-rt/include/orc-rt/move_only_function.h   | 3 ++-
 orc-rt/unittests/move_only_function-test.cpp | 5 +++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/orc-rt/include/orc-rt/move_only_function.h b/orc-rt/include/orc-rt/move_only_function.h
index c87ce50d4daed..4b8a3f80f6ef5 100644
--- a/orc-rt/include/orc-rt/move_only_function.h
+++ b/orc-rt/include/orc-rt/move_only_function.h
@@ -26,6 +26,7 @@
 #define ORC_RT_MOVE_ONLY_FUNCTION_H
 
 #include <memory>
+#include <type_traits>
 
 namespace orc_rt {
 
@@ -46,7 +47,7 @@ class CallableImpl : public Callable<RetT, ArgTs...> {
   }
 
 private:
-  CallableT Callable;
+  std::decay_t<CallableT> Callable;
 };
 
 } // namespace move_only_function_detail
diff --git a/orc-rt/unittests/move_only_function-test.cpp b/orc-rt/unittests/move_only_function-test.cpp
index 95194edc69f6d..c304bf111a392 100644
--- a/orc-rt/unittests/move_only_function-test.cpp
+++ b/orc-rt/unittests/move_only_function-test.cpp
@@ -98,6 +98,11 @@ TEST(MoveOnlyFunctionTest, Captures) {
   EXPECT_EQ(C5(), 15);
   Tmp = std::move(C5);
   EXPECT_EQ(Tmp(), 15);
+
+  // Test capture via lvalue.
+  auto Inc = [](int N) { return N + 1; };
+  move_only_function<int(int)> C6(Inc);
+  EXPECT_EQ(C6(1), 2);
 }
 
 TEST(MoveOnlyFunctionTest, MoveOnly) {



More information about the llvm-commits mailing list