From 907f41bae46b5d2a48bf7cf31addbc0ac71a61c3 Mon Sep 17 00:00:00 2001 From: Johannes Schmidt Date: Thu, 12 Mar 2026 14:12:00 +0100 Subject: [PATCH] Use perfect-forwarding references to prevent passing temporaries Without this change, it is easy to pass a temporary, for example a vector returned by a function to ParallelFor(). With this commit a type-constraint is added to disable the use of this function for rvalue sequences. An alternative would have been to capture rvalue-references in the function, but that would have made the function unnecessarily complex, involving tuple-capture and `shared_ptr`s, while it is usually easy to control lifetime at the call-site. --- lib/base/workqueue.hpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/base/workqueue.hpp b/lib/base/workqueue.hpp index a6cfb7216..fc3711555 100644 --- a/lib/base/workqueue.hpp +++ b/lib/base/workqueue.hpp @@ -67,15 +67,14 @@ public: void Join(bool stop = false); template - void ParallelFor(const VectorType& items, const FuncType& func) + void ParallelFor(VectorType&& items, const FuncType& func) { - ParallelFor(items, true, func); + ParallelFor(std::forward(items), true, func); } - template - void ParallelFor(const VectorType& items, bool preChunk, const FuncType& func) + template>> + void ParallelFor(VectorType&& items, bool preChunk, const FuncType& func) { - const auto totalCount = std::size(items); using SizeType = std::remove_const_t; const auto chunks = preChunk ? m_ThreadCount : totalCount;