Skip to content

Commit 8214d75

Browse files
committed
Add possibility to use lambda in is-statements
1 parent 50b7932 commit 8214d75

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

include/cpp2util.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,31 @@ constexpr auto is( X const& x ) -> bool {
641641
return false;
642642
}
643643

644+
template <typename F, typename Capture = std::monostate>
645+
requires (
646+
requires { &F::operator(); }
647+
)
648+
struct lambda_wrapper {
649+
F f;
650+
Capture capture;
651+
652+
constexpr lambda_wrapper(F f, Capture capture = {})
653+
: f{f}, capture{capture} {}
654+
655+
template <typename X>
656+
auto operator()(X&& x) const {
657+
if constexpr (std::is_same_v<Capture, std::monostate>)
658+
return f(std::forward<X>(x));
659+
else
660+
return f(std::forward<X>(x), capture);
661+
}
662+
};
663+
664+
template< lambda_wrapper value, typename X >
665+
constexpr auto is( X const& x ) -> bool {
666+
return value(x);
667+
}
668+
644669
//-------------------------------------------------------------------------------------------------------------
645670
// Built-in as (partial)
646671
//

0 commit comments

Comments
 (0)