From ea69819098bc97824f2e742ace18a2976e5a7aa2 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 10 Dec 2020 02:58:21 +0700 Subject: [PATCH] simplify SFINAE with constexpr in ext --- headers/kari.hpp/kari_ext.hpp | 83 ++++++++++++++++------------------- 1 file changed, 38 insertions(+), 45 deletions(-) diff --git a/headers/kari.hpp/kari_ext.hpp b/headers/kari.hpp/kari_ext.hpp index 67c6394..8415a3e 100644 --- a/headers/kari.hpp/kari_ext.hpp +++ b/headers/kari.hpp/kari_ext.hpp @@ -32,7 +32,7 @@ namespace kari_hpp struct fconst_t { template < typename A, typename B > - constexpr auto operator()(A&& a, B&&) const { + constexpr auto operator()(A&& a, [[maybe_unused]] B&& b) const { return std::forward(a); } }; @@ -45,10 +45,7 @@ namespace kari_hpp struct fflip_t { template < typename F, typename A, typename B > constexpr auto operator()(F&& f, A&& a, B&& b) const { - return curry( - std::forward(f), - std::forward(b), - std::forward(a)); + return curry(std::forward(f), std::forward(b), std::forward(a)); } }; inline constexpr auto fflip = curry(fflip_t{}); @@ -62,9 +59,7 @@ namespace kari_hpp constexpr auto operator()(G&& g, F&& f, A&& a) const { return curry( std::forward(f), - curry( - std::forward(g), - std::forward(a))); + curry(std::forward(g), std::forward(a))); } }; inline constexpr auto fpipe = curry(fpipe_t{}); @@ -78,9 +73,7 @@ namespace kari_hpp constexpr auto operator()(G&& g, F&& f, A&& a) const { return curry( std::forward(g), - curry( - std::forward(f), - std::forward(a))); + curry(std::forward(f), std::forward(a))); } }; inline constexpr auto fcompose = curry(fcompose_t{}); @@ -90,26 +83,26 @@ namespace kari_hpp // template < typename G, typename F - , std::enable_if_t>, int> = 0 - , std::enable_if_t>, int> = 0 > + , std::enable_if_t>, + is_curried>>, int> = 0 > constexpr auto operator|(G&& g, F&& f) { - return fpipe( - std::forward(g), - std::forward(f)); - } + constexpr bool gc = is_curried_v>; + constexpr bool fc = is_curried_v>; - template < typename F, typename A - , std::enable_if_t< is_curried_v>, int> = 0 - , std::enable_if_t>, int> = 0 > - constexpr auto operator|(F&& f, A&& a) { - return std::forward(f)(std::forward(a)); - } + if constexpr ( gc && fc ) { + return fpipe(std::forward(g), std::forward(f)); + } - template < typename A, typename F - , std::enable_if_t>, int> = 0 - , std::enable_if_t< is_curried_v>, int> = 0 > - constexpr auto operator|(A&& a, F&& f) { - return std::forward(f)(std::forward(a)); + if constexpr ( gc && !fc) { + return std::forward(g)(std::forward(f)); + } + + if constexpr ( !gc && fc) { + return std::forward(f)(std::forward(g)); + } + + static_assert(gc || fc, "F or G or both arguments should be curried"); } // @@ -117,26 +110,26 @@ namespace kari_hpp // template < typename G, typename F - , std::enable_if_t>, int> = 0 - , std::enable_if_t>, int> = 0 > + , std::enable_if_t>, + is_curried>>, int> = 0 > constexpr auto operator*(G&& g, F&& f) { - return fcompose( - std::forward(g), - std::forward(f)); - } + constexpr bool gc = is_curried_v>; + constexpr bool fc = is_curried_v>; - template < typename F, typename A - , std::enable_if_t< is_curried_v>, int> = 0 - , std::enable_if_t>, int> = 0 > - constexpr auto operator*(F&& f, A&& a) { - return std::forward(f)(std::forward(a)); - } + if constexpr ( gc && fc ) { + return fcompose(std::forward(g), std::forward(f)); + } - template < typename A, typename F - , std::enable_if_t>, int> = 0 - , std::enable_if_t< is_curried_v>, int> = 0 > - constexpr auto operator*(A&& a, F&& f) { - return std::forward(f)(std::forward(a)); + if constexpr ( gc && !fc) { + return std::forward(g)(std::forward(f)); + } + + if constexpr ( !gc && fc) { + return std::forward(f)(std::forward(g)); + } + + static_assert(gc || fc, "F or G or both arguments should be curried"); } }