Common/Functional: Add InvokerOf template which can convert function pointers to functor types.

This commit is contained in:
Jordan Woyak 2025-11-17 18:35:38 -06:00
parent 1b63776f2d
commit 605cc579a4

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <concepts> #include <concepts>
#include <functional>
#include <memory> #include <memory>
#include <type_traits> #include <type_traits>
@ -51,4 +52,17 @@ private:
std::unique_ptr<FuncBase> m_ptr; std::unique_ptr<FuncBase> m_ptr;
}; };
// A functor type with an invocable non-type template parameter.
// e.g. Providing a function pointer will create a functor type that invokes said function.
// It allows using function pointers in contexts that expect a type, e.g. as a "deleter".
template <auto Invocable>
struct InvokerOf
{
template <typename... Args>
constexpr auto operator()(Args... args) const
{
return std::invoke(Invocable, std::forward<Args>(args)...);
}
};
} // namespace Common } // namespace Common