//======================================================================= // Copyright Baptiste Wicht 2013-2018. // Distributed under the terms of the MIT License. // (See accompanying file LICENSE or copy at // http://www.opensource.org/licenses/MIT) //======================================================================= #ifndef STD_FUNCTION_HPP #define STD_FUNCTION_HPP #include #include namespace std { template struct function; template struct function { public: template function(T&& t){ typedef model::type> impl; static_assert(sizeof(impl) <= sizeof(storage), "Limited size in function"); static_assert(std::is_trivially_destructible::value, "Limited support of function"); auto* s = static_cast(&storage[0]); new (s) impl(std::forward(t)); } function(const function& rhs) = delete; function& operator=(const function& rhs) = delete; function(function&& rhs) = delete; function& operator=(function&& rhs) = delete; R operator()(Args... args) const { auto* s = static_cast(&storage[0]); auto* c = static_cast(s); return (*c)(std::forward(args)...); } private: size_t storage[2]; struct concept { virtual R operator()(Args...) const = 0; }; template struct model : concept { template model(U&& u) : t(std::forward(u)){ //Nothing to init } R operator()(Args... args) const override { return t(std::forward(args)...); } T t; }; }; } //end of namespace std #endif