Skip to content

Commit aef3217

Browse files
committed
Add possibility to use lambda in is-statements
1 parent f4866cf commit aef3217

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

include/cpp2util.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,28 @@ auto is( X const& x ) -> bool {
637637
return false;
638638
}
639639

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

0 commit comments

Comments
 (0)