diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-11-02 19:54:50 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-12-20 15:35:58 +0100 |
commit | c09323804c6c6d42c052b1c60061134261e515fc (patch) | |
tree | 1c5dd6962e82ee35bc1ae662e6f268826511ada9 /asl/functional.hpp | |
parent | be34f768e093cb6752ab81fde3ab529861a8a6a8 (diff) |
Add function with invoke and result_of_t
Diffstat (limited to 'asl/functional.hpp')
-rw-r--r-- | asl/functional.hpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/asl/functional.hpp b/asl/functional.hpp new file mode 100644 index 0000000..967a2a2 --- /dev/null +++ b/asl/functional.hpp @@ -0,0 +1,65 @@ +#pragma once
+
+#include "asl/meta.hpp"
+#include "asl/utility.hpp"
+
+namespace asl {
+
+template<typename... Args, typename C>
+constexpr auto invoke(is_func auto C::* f, auto&& self, Args&&... args)
+ requires requires {
+ (self.*f)(ASL_FWD(args)...);
+ }
+{
+ return (ASL_FWD(self).*f)(ASL_FWD(args)...);
+}
+
+template<typename... Args, typename C>
+constexpr auto invoke(is_func auto C::* f, auto* self, Args&&... args)
+ requires requires {
+ (self->*f)(ASL_FWD(args)...);
+ }
+{
+ return (self->*f)(ASL_FWD(args)...);
+}
+
+template<typename... Args, typename C>
+constexpr auto invoke(is_object auto C::* m, auto&& self, Args&&...)
+ requires (
+ sizeof...(Args) == 0 &&
+ requires { self.*m; }
+ )
+{
+ return ASL_FWD(self).*m;
+}
+
+template<typename... Args, typename C>
+constexpr auto invoke(is_object auto C::* m, auto* self, Args&&...)
+ requires (
+ sizeof...(Args) == 0 &&
+ requires { self->*m; }
+ )
+{
+ return self->*m;
+}
+
+template<typename... Args>
+constexpr auto invoke(auto&& f, Args&&... args)
+ requires requires {
+ f(ASL_FWD(args)...);
+ }
+{
+ return ASL_FWD(f)(ASL_FWD(args)...);
+}
+
+template<typename F> struct _result_of_helper;
+
+template<typename R, typename... Args>
+struct _result_of_helper<R(Args...)>
+{
+ using type = decltype(invoke(declval<R>(), declval<Args>()...));
+};
+
+template<typename F> using result_of_t = _result_of_helper<F>::type;
+
+} // namespace asl
|