Skip to content

Commit 64f7198

Browse files
committed
Add possibility to use lambda in is-statements
1 parent 921ed9f commit 64f7198

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
@@ -686,6 +686,31 @@ constexpr auto is( X const& x ) -> bool {
686686
return false;
687687
}
688688

689+
template <typename F, typename Capture = std::monostate>
690+
requires (
691+
requires { &F::operator(); }
692+
)
693+
struct lambda_wrapper {
694+
F f;
695+
Capture capture;
696+
697+
constexpr lambda_wrapper(F f, Capture capture = {})
698+
: f{f}, capture{capture} {}
699+
700+
template <typename X>
701+
auto operator()(X&& x) const {
702+
if constexpr (std::is_same_v<Capture, std::monostate>)
703+
return f(std::forward<X>(x));
704+
else
705+
return f(std::forward<X>(x), capture);
706+
}
707+
};
708+
709+
template< lambda_wrapper value, typename X >
710+
constexpr auto is( X const& x ) -> bool {
711+
return value(x);
712+
}
713+
689714
//-------------------------------------------------------------------------------------------------------------
690715
// Built-in as (partial)
691716
//

0 commit comments

Comments
 (0)