Skip to content

Commit e119ada

Browse files
committed
Add fix for is any and a regression test, closes #5
1 parent 10c3de2 commit e119ada

5 files changed

+92
-2
lines changed

include/cpp2util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,12 +615,12 @@ auto as( std::variant<Ts...> const& x ) {
615615
// std::any is and as
616616
//
617617
template<typename T, typename X>
618-
requires std::is_same_v<X,std::any>
618+
requires (std::is_same_v<X,std::any> && !std::is_same_v<T,std::any>)
619619
constexpr auto is( X const& x ) -> bool
620620
{ return x.type() == typeid(T); }
621621

622622
template<typename T, typename X>
623-
requires (!std::is_reference_v<T> && std::is_same_v<X,std::any>)
623+
requires (!std::is_reference_v<T> && std::is_same_v<X,std::any> && !std::is_same_v<T,std::any>)
624624
constexpr auto as( X const& x ) -> T
625625
{ return std::any_cast<T>( x ); }
626626

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
main: () -> int = {
3+
v: std::variant<int, std::string> = "xyzzy" as std::string;
4+
a: std::any = "xyzzy" as std::string;
5+
o: std::optional<std::string> = "xyzzy" as std::string;
6+
7+
std::cout << "\nAll these cases satisfy \"matches std::string\"\n";
8+
9+
test_generic(v);
10+
test_generic(a);
11+
test_generic(o);
12+
}
13+
14+
test_generic: ( x: _ ) = {
15+
std::cout
16+
<< "\n" << typeid(x).name() << "\n ..."
17+
<< inspect x -> std::string {
18+
is std::string = " matches std::string";
19+
is std::variant<int, std::string> = " matches std::variant<int, std::string>";
20+
is std::any = " matches std::any";
21+
is std::optional<std::string> = " matches std::optional<std::string>";
22+
is _ = " no match";
23+
}
24+
<< "\n";
25+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// ----- Cpp2 support -----
2+
#define CPP2_USE_MODULES Yes
3+
#include "cpp2util.h"
4+
5+
6+
#line 2 "pure2-inspect-fallback-with-variant-any-optional.cpp2"
7+
[[nodiscard]] auto main() -> int;
8+
#line 14 "pure2-inspect-fallback-with-variant-any-optional.cpp2"
9+
auto test_generic(auto const& x) -> void;
10+
11+
//=== Cpp2 definitions ==========================================================
12+
13+
#line 1 "pure2-inspect-fallback-with-variant-any-optional.cpp2"
14+
15+
[[nodiscard]] auto main() -> int{
16+
std::variant<int,std::string> v { cpp2::as<std::string>("xyzzy") };
17+
std::any a { cpp2::as<std::string>("xyzzy") };
18+
std::optional<std::string> o { cpp2::as<std::string>("xyzzy") };
19+
20+
std::cout << "\nAll these cases satisfy \"matches std::string\"\n";
21+
22+
test_generic(v);
23+
test_generic(a);
24+
test_generic(o);
25+
}
26+
27+
auto test_generic(auto const& x) -> void{
28+
std::cout
29+
<< "\n" << typeid(x).name() << "\n ..."
30+
<< [&] () -> std::string { auto&& __expr = x;
31+
if (cpp2::is<std::string>(__expr)) { if constexpr( requires{" matches std::string";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(" matches std::string"),std::string> ) return " matches std::string"; else return std::string{}; else return std::string{}; }
32+
else if (cpp2::is<std::variant<int,std::string>>(__expr)) { if constexpr( requires{" matches std::variant<int, std::string>";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(" matches std::variant<int, std::string>"),std::string> ) return " matches std::variant<int, std::string>"; else return std::string{}; else return std::string{}; }
33+
else if (cpp2::is<std::any>(__expr)) { if constexpr( requires{" matches std::any";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(" matches std::any"),std::string> ) return " matches std::any"; else return std::string{}; else return std::string{}; }
34+
else if (cpp2::is<std::optional<std::string>>(__expr)) { if constexpr( requires{" matches std::optional<std::string>";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(" matches std::optional<std::string>"),std::string> ) return " matches std::optional<std::string>"; else return std::string{}; else return std::string{}; }
35+
else return " no match"; }
36+
()
37+
<< "\n";
38+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
main: () -> int = {
3+
v: std::variant<int, std::string> = "xyzzy" as std::string;
4+
a: std::any = "xyzzy" as std::string;
5+
o: std::optional<std::string> = "xyzzy" as std::string;
6+
7+
std::cout << "\nAll these cases satisfy \"matches std::string\"\n";
8+
9+
test_generic(v);
10+
test_generic(a);
11+
test_generic(o);
12+
}
13+
14+
test_generic: ( x: _ ) = {
15+
std::cout
16+
<< "\n" << typeid(x).name() << "\n ..."
17+
<< inspect x -> std::string {
18+
is std::string = " matches std::string";
19+
is std::variant<int, std::string> = " matches std::variant<int, std::string>";
20+
is std::any = " matches std::any";
21+
is std::optional<std::string> = " matches std::optional<std::string>";
22+
is _ = " no match";
23+
}
24+
<< "\n";
25+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-inspect-fallback-with-variant-any-optional.cpp2... ok (all Cpp2, passes safety checks)
2+

0 commit comments

Comments
 (0)