diff options
| author | Jordan Woyak <jordan.woyak@gmail.com> | 2025-10-22 04:05:03 -0500 |
|---|---|---|
| committer | Jordan Woyak <jordan.woyak@gmail.com> | 2025-10-22 05:06:41 -0500 |
| commit | ba5edce2af5acffedd6f67363aedf6f9dc0ab52d (patch) | |
| tree | 503e3a7e8c82af168e094bb120944f8c930970f3 | |
| parent | df809959d05267430e24f81574521cac888e2e6f (diff) | |
Common/Functional: Fix MoveOnlyFunction from inadvertently creating references from lvalues.
| -rw-r--r-- | Source/Core/Common/Functional.h | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/Source/Core/Common/Functional.h b/Source/Core/Common/Functional.h index 49d81d5a24..cdb44ebb0c 100644 --- a/Source/Core/Common/Functional.h +++ b/Source/Core/Common/Functional.h @@ -3,7 +3,9 @@ #pragma once +#include <concepts> #include <memory> +#include <type_traits> // TODO C++23: Replace with std::move_only_function. @@ -21,13 +23,14 @@ public: MoveOnlyFunction() = default; - template <typename F> + template <std::invocable<Args...> F> + requires(!std::same_as<std::decay_t<F>, MoveOnlyFunction>) MoveOnlyFunction(F&& f) : m_ptr{std::make_unique<Func<F>>(std::forward<F>(f))} { } result_type operator()(Args... args) const { return m_ptr->Invoke(std::forward<Args>(args)...); } - explicit operator bool() const { return m_ptr != nullptr; }; + explicit operator bool() const { return m_ptr != nullptr; } void swap(MoveOnlyFunction& other) { m_ptr.swap(other.m_ptr); } private: @@ -40,9 +43,9 @@ private: template <typename F> struct Func : FuncBase { - Func(F&& f) : func{std::forward<F>(f)} {} + explicit Func(F&& f) : func{std::forward<F>(f)} {} result_type Invoke(Args... args) override { return func(std::forward<Args>(args)...); } - F func; + std::decay_t<F> func; }; std::unique_ptr<FuncBase> m_ptr; |
