[orc-rt] Use perfect forwarding for scope-exit initialization. (#157786)

Allows the use of move-only types with make_scope_exit.
This commit is contained in:
Lang Hames
2025-09-10 13:47:01 +10:00
committed by GitHub
parent d077c840c9
commit 224cad63da
2 changed files with 14 additions and 1 deletions

View File

@@ -21,7 +21,8 @@ namespace detail {
template <typename Fn> class ScopeExitRunner {
public:
ScopeExitRunner(Fn &&F) : F(F) {}
template <typename FnInit>
ScopeExitRunner(FnInit &&F) : F(std::forward<FnInit>(F)) {}
ScopeExitRunner(const ScopeExitRunner &) = delete;
ScopeExitRunner &operator=(const ScopeExitRunner &) = delete;
ScopeExitRunner(ScopeExitRunner &&) = delete;

View File

@@ -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());
}
}