From 92f256160f316c1d48fa12c56e6f2608b2b33811 Mon Sep 17 00:00:00 2001 From: dyknon Date: Thu, 10 Sep 2026 00:52:11 +0900 Subject: better syntax, more protection, std::function compatibility --- thinfunc.h | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'thinfunc.h') diff --git a/thinfunc.h b/thinfunc.h index c75c33a..dcfb94a 100644 --- a/thinfunc.h +++ b/thinfunc.h @@ -33,7 +33,7 @@ template struct func; template struct func{ - virtual R operator()(Args...) = 0; + virtual R operator()(Args...) const = 0; }; template @@ -43,6 +43,7 @@ class bound_anchor; template class anchor{ + friend T; public: template (T::*Self), R (T::*Method)(Args...)> using bound_type = bound_anchor) char body[sizeof(bound_type)]; -public: +protected: template (T::*Self), R (T::*Method)(Args...)> auto& bind(){ - return *(new (this->body) bound_type()); + static_assert(sizeof(bound_type) == sizeof(body)); + static_assert( + (func *)(bound_type *)nullptr + == (func *)(bound_type *)nullptr); + return *(new (body) bound_type()); } +public: template (T::*Self), R (T::*Method)(Args...)> - auto& assume_bound() const{ - return *(bound_type *)this->body; + auto& bound(){ + return *(bound_type *)body; + } + func& bound(){ + return *(func *)(bound_type *)body; } + anchor(const anchor&) = delete; + anchor(const anchor&&) = delete; + anchor& operator=(const anchor&) = delete; + anchor& operator=(const anchor&&) = delete; + anchor() = default; }; template< @@ -75,7 +89,7 @@ public: bound_anchor(const bound_anchor&&) = delete; bound_anchor& operator=(const bound_anchor&) = delete; bound_anchor& operator=(const bound_anchor&&) = delete; - virtual R operator()(Args... args){ + virtual R operator()(Args... args) const{ auto offset = (char *)&(((T *)0)->*Self) - (char *)0; T *container = (T *)((char *)this - offset); return (container->*Method)(args...); -- cgit v1.2.3