From 5c3c841fe0b68398ae8bff707e9d84ae76411f63 Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Wed, 10 Sep 2025 13:34:58 +1000 Subject: [PATCH] [orc-rt] Use perfect forwarding for scope-exit initialization. Allows the use of move-only types with make_scope_exit. --- orc-rt/include/orc-rt/ScopeExit.h | 3 ++- orc-rt/unittests/ScopeExitTest.cpp | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/orc-rt/include/orc-rt/ScopeExit.h b/orc-rt/include/orc-rt/ScopeExit.h index e73ca187ddc74..afd20c193c394 100644 --- a/orc-rt/include/orc-rt/ScopeExit.h +++ b/orc-rt/include/orc-rt/ScopeExit.h @@ -21,7 +21,8 @@ namespace detail { template class ScopeExitRunner { public: - ScopeExitRunner(Fn &&F) : F(F) {} + template + ScopeExitRunner(FnInit &&F) : F(std::forward(F)) {} ScopeExitRunner(const ScopeExitRunner &) = delete; ScopeExitRunner &operator=(const ScopeExitRunner &) = delete; ScopeExitRunner(ScopeExitRunner &&) = delete; diff --git a/orc-rt/unittests/ScopeExitTest.cpp b/orc-rt/unittests/ScopeExitTest.cpp index bbd517d79793b..411bb4f97bc3d 100644 --- a/orc-rt/unittests/ScopeExitTest.cpp +++ b/orc-rt/unittests/ScopeExitTest.cpp @@ -37,3 +37,15 @@ TEST(ScopeExitTest, Release) { } EXPECT_FALSE(ScopeExitRun); } + +TEST(ScopeExitTest, MoveOnlyFunctionObject) { + struct MoveOnly { + MoveOnly() = default; + MoveOnly(MoveOnly &&) = default; + void operator()() {} + }; + + { + auto OnExit = make_scope_exit(MoveOnly()); + } +}