From 1d481a395ae18546f9bfd224f196a0271a526691 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Tue, 25 Feb 2025 03:59:37 -0600 Subject: [PATCH] VariantUtil: Introduce WithVariantAlternative to dynamically construct and visit a variant alternative. --- Source/Core/Common/VariantUtil.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Source/Core/Common/VariantUtil.h b/Source/Core/Common/VariantUtil.h index 69f886c167..54ac6ba2cf 100644 --- a/Source/Core/Common/VariantUtil.h +++ b/Source/Core/Common/VariantUtil.h @@ -34,3 +34,18 @@ struct overloaded : Ts... template overloaded(Ts...) -> overloaded; + +// Visits a functor with a variant_alternative of the given index. +// e.g. WithVariantAlternative>(1, func) calls func() +// An out-of-bounds index causes no visitation. +template +void WithVariantAlternative(std::size_t index, Func&& func) +{ + if constexpr (I < std::variant_size_v) + { + if (index == 0) + func.template operator()>(); + else + WithVariantAlternative(index - 1, std::forward(func)); + } +}