Skip to content

Commit 148d1ea

Browse files
committed
Add possibility to use lambda in is-statements
1 parent a0e99b0 commit 148d1ea

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
@@ -637,6 +637,31 @@ constexpr auto is( X const& x ) -> bool {
637637
return false;
638638
}
639639

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

0 commit comments

Comments
 (0)