diff --git a/src/vt/runnable/runnable.h b/src/vt/runnable/runnable.h index 2bfe5425a1..3151a4fe43 100644 --- a/src/vt/runnable/runnable.h +++ b/src/vt/runnable/runnable.h @@ -235,16 +235,19 @@ struct RunnableNew { decltype(auto) runLambda(Callable&& c, Args&&... args) { auto start_time = timing::getCurrentTime(); start(start_time); - if constexpr(std::is_void_v>) { - std::invoke(std::forward(c), std::forward(args)...); - auto finish_time = timing::getCurrentTime(); - finish(finish_time); - } else { - decltype(auto) r{std::invoke(std::forward(c), std::forward(args)...)}; - auto finish_time = timing::getCurrentTime(); - finish(finish_time); - return r; - } + + // Arrange a scope guard to call finish() without any sort of dynamic allocation + struct finisher { + RunnableNew* r; + finisher(RunnableNew* in_r) : r(in_r){}; + ~finisher() { + auto finish_time = timing::getCurrentTime(); + r->finish(finish_time); + } + }; + finisher f(this); + + return std::invoke(std::forward(c), std::forward(args)...); } #if vt_check_enabled(fcontext)