Skip to content

Commit 5eb3fdb

Browse files
committed
Add is overload for free function predicate
Current implementation of `is` does not handle generic functions as predicates. The following case works: ```cpp less_than_10_a: (i : int) -> bool = i < 10; main: () = { 12 is (less_than_10_a); } ``` It work also when function has generic return type ```cpp less_than_10_b: (i : int) -> _ = i < 10; main: () = { 12 is (less_than_10_b); } ``` It does not work when function has generic argument. The following case does not work: ```cpp less_than_10_c: (i) -> _ = i < 10; main: () = { 12 is (less_than_10_c); // error } ``` This change introduce additional overload for `is` making free function predicates acceptable by `is` (the function needs to return `bool` or generic type). That makes the following cases to work: ```cpp less_than_10_c: (i) -> _ = i < 10; less_than_10_d: (i) -> bool = i < 10; main: () = { 5 is (less_than_10_c); // works 2 is (less_than_10_d); // works } ``` Functions that return other type then `bool` or `auto` do not work.
1 parent c2e0778 commit 5eb3fdb

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

include/cpp2util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,12 @@ inline constexpr auto is( auto const& x, auto const& value ) -> bool
882882
return false;
883883
}
884884

885+
// Free function predicate case
886+
template< typename X >
887+
constexpr auto is( X const& x, bool (&value)(X const&) ) -> bool
888+
{
889+
return value(x);
890+
}
885891

886892
//-------------------------------------------------------------------------------------------------------------
887893
// Built-in as

0 commit comments

Comments
 (0)