| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -615,12 +615,12 @@ auto as( std::variant<Ts...> const& x ) { | |||
| 615 | 615 | // std::any is and as | |
| 616 | 616 | // | |
| 617 | 617 | 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>) | ||
| 619 | 619 | constexpr auto is( X const& x ) -> bool | |
| 620 | 620 | { return x.type() == typeid(T); } | |
| 621 | 621 | ||
| 622 | 622 | 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>) | ||
| 624 | 624 | constexpr auto as( X const& x ) -> T | |
| 625 | 625 | { return std::any_cast<T>( x ); } | |
| 626 | 626 | ||
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + pure2-inspect-fallback-with-variant-any-optional.cpp2... ok (all Cpp2, passes safety checks) | ||
| 2 | + | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments