Skip to content

Commit cd03b8e

Browse files
committed
Add possibility to use lambda in is-statements
1 parent b7668e9 commit cd03b8e

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
@@ -704,6 +704,31 @@ constexpr auto is( X const& x ) -> bool {
704704
return false;
705705
}
706706

707+
template <typename F, typename Capture = std::monostate>
708+
requires (
709+
requires { &F::operator(); }
710+
)
711+
struct lambda_wrapper {
712+
F f;
713+
Capture capture;
714+
715+
constexpr lambda_wrapper(F f, Capture capture = {})
716+
: f{f}, capture{capture} {}
717+
718+
template <typename X>
719+
auto operator()(X&& x) const {
720+
if constexpr (std::is_same_v<Capture, std::monostate>)
721+
return f(std::forward<X>(x));
722+
else
723+
return f(std::forward<X>(x), capture);
724+
}
725+
};
726+
727+
template< lambda_wrapper value, typename X >
728+
constexpr auto is( X const& x ) -> bool {
729+
return value(x);
730+
}
731+
707732
//-------------------------------------------------------------------------------------------------------------
708733
// Built-in as (partial)
709734
//

0 commit comments

Comments
 (0)