#pragma once #include "unique_ptr.hpp" namespace std { template struct function; template struct function { private: struct fn_interface { virtual ~fn_interface() = default; virtual Result operator()(Args ...) const = 0; }; template struct fn_implementation : fn_interface { fn_implementation(F&& f) : f_(std::forward(f)) { } Result operator()(Args ... a) const override { return f_(std::forward(a)...); } F f_; }; std::unique_ptr fn_{}; public: function() = default; template function(T&& t) : fn_(new fn_implementation(std::forward(t))) { } ~function() = default; function(function&&) noexcept = default; function& operator=(function&&) noexcept = default; function(const function&) = delete; function& operator=(const function&) = delete; Result operator()(Args ... args) const { return (*this->fn_)(std::forward(args)...); } operator bool() const { return this->fn_; } }; }