From 3f162a0555b10dec85538bb046ae942f115c9f01 Mon Sep 17 00:00:00 2001 From: Lang Hames 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 to ensure that we can initialize via lvalue references: auto NamedNoop = [](){}; move_only_function 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 +#include namespace orc_rt { @@ -46,7 +47,7 @@ class CallableImpl : public Callable { } private: - CallableT Callable; + std::decay_t 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 C6(Inc); + EXPECT_EQ(C6(1), 2); } TEST(MoveOnlyFunctionTest, MoveOnly) {