Move only function helper

This commit is contained in:
Piotr Wójcik 2024-12-29 22:10:55 +01:00
commit 61ad41a28b

19
nano/lib/function.hpp Normal file
View file

@ -0,0 +1,19 @@
#pragma once
#include <functional>
#include <memory>
#include <utility>
namespace nano
{
// TODO: Replace with std::move_only_function in C++23
template <typename F>
auto wrap_move_only (F && f)
{
using fn_type = decltype (std::function{ std::declval<F> () });
auto ptr = std::make_shared<std::decay_t<F>> (std::forward<F> (f));
return fn_type ([ptr] (auto &&... args) {
return (*ptr) (std::forward<decltype (args)> (args)...);
});
}
}