From a4dfd8d6c6f499b9016157b9411653923eace215 Mon Sep 17 00:00:00 2001 From: reuk Date: Wed, 20 Sep 2023 16:13:02 +0100 Subject: [PATCH] FixedSizeFunction: Allow sinking of rvalue arguments for functions returning void --- .../containers/juce_FixedSizeFunction.h | 2 +- .../containers/juce_FixedSizeFunction_test.cpp | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/modules/juce_core/containers/juce_FixedSizeFunction.h b/modules/juce_core/containers/juce_FixedSizeFunction.h index 3919ccb430..dc96b9f548 100644 --- a/modules/juce_core/containers/juce_FixedSizeFunction.h +++ b/modules/juce_core/containers/juce_FixedSizeFunction.h @@ -56,7 +56,7 @@ namespace detail template std::enable_if_t, Ret> call (void* s, Args... args) { - (*reinterpret_cast (s)) (args...); + (*reinterpret_cast (s)) (std::forward (args)...); } template diff --git a/modules/juce_core/containers/juce_FixedSizeFunction_test.cpp b/modules/juce_core/containers/juce_FixedSizeFunction_test.cpp index a5542d5e88..86d9b3bb41 100644 --- a/modules/juce_core/containers/juce_FixedSizeFunction_test.cpp +++ b/modules/juce_core/containers/juce_FixedSizeFunction_test.cpp @@ -312,14 +312,26 @@ public: { JUCE_FAIL_ON_ALLOCATION_IN_SCOPE; - using Fn = FixedSizeFunction<64, int (std::unique_ptr)>; + using FnA = FixedSizeFunction<64, int (std::unique_ptr)>; auto value = 5; auto ptr = std::make_unique (value); - Fn fn = [] (std::unique_ptr p) { return *p; }; + FnA fnA = [] (std::unique_ptr p) { return *p; }; - expect (value == fn (std::move (ptr))); + expect (value == fnA (std::move (ptr))); + + using FnB = FixedSizeFunction<64, void (std::unique_ptr&&)>; + + FnB fnB = [&value] (std::unique_ptr&& p) + { + auto x = std::move (p); + value = *x; + }; + + const auto newValue = 10; + fnB (std::make_unique (newValue)); + expect (value == newValue); } beginTest ("Functions be converted from smaller functions");