@@ -1784,26 +1784,6 @@ constexpr auto is( X const& x ) -> auto {
1784
1784
{
1785
1785
return Dynamic_cast<C const *>(&x) != nullptr ;
1786
1786
}
1787
- else if constexpr (
1788
- specialization_of_template<X, std::variant>
1789
- )
1790
- {
1791
- if (x.valueless_by_exception ()) {
1792
- return std::is_same_v<C, empty>;
1793
- }
1794
- if constexpr (
1795
- std::is_same_v<C, empty>
1796
- )
1797
- {
1798
- if constexpr (requires { {variant_contains_type<std::monostate>(std::declval<X>())} -> std::same_as<std::true_type>; }) {
1799
- return std::get_if<std::monostate>(&x) != nullptr ;
1800
- }
1801
- }
1802
- return type_find_if (x, [&]<typename It>(It const &) -> bool {
1803
- if (x.index () == It::index ) { return std::is_same_v<C, std::variant_alternative_t <It::index , X>>;}
1804
- return false ;
1805
- }) != std::variant_npos;
1806
- }
1807
1787
else if constexpr (
1808
1788
(
1809
1789
std::is_same_v<X, std::nullptr_t >
@@ -1854,18 +1834,6 @@ inline constexpr auto is( auto const& x, auto&& value ) -> bool
1854
1834
else if constexpr (requires{ bool {x == value}; }) {
1855
1835
return x == value;
1856
1836
}
1857
- else if constexpr (specialization_of_template<decltype (x), std::variant> ) {
1858
- return type_find_if (x, [&]<typename It>(It const &) -> bool {
1859
- if (x.index () == It::index ) {
1860
- if constexpr (valid_predicate<decltype (value), decltype (std::get<It::index >(x))>) {
1861
- return value (std::get<It::index >(x));
1862
- } else if constexpr ( requires { bool {std::get<It::index >(x) == value}; } ) {
1863
- return std::get<It::index >(x) == value;
1864
- }
1865
- }
1866
- return false ;
1867
- }) != std::variant_npos;
1868
- }
1869
1837
return false ;
1870
1838
}
1871
1839
@@ -1970,7 +1938,6 @@ auto as(auto&& x CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT_AS) -> decltype(auto)
1970
1938
|| std::is_base_of_v<C, CPP2_TYPEOF(x)>
1971
1939
|| std::is_base_of_v<CPP2_TYPEOF(x), C>
1972
1940
|| requires { C{CPP2_FORWARD (x)}; }
1973
- || specialization_of_template<CPP2_TYPEOF(x), std::variant>
1974
1941
)
1975
1942
{
1976
1943
if constexpr (
@@ -2043,15 +2010,6 @@ auto as(auto&& x CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT_AS) -> decltype(auto)
2043
2010
}
2044
2011
return C{CPP2_FORWARD (x)};
2045
2012
}
2046
- else if constexpr (specialization_of_template<decltype (x), std::variant>) {
2047
- constness_like_t <C, decltype (x)>* ptr = nullptr ;
2048
- type_find_if (CPP2_FORWARD (x), [&]<typename It>(It const &) -> bool {
2049
- if constexpr (std::is_same_v< typename It::type, C >) { if (CPP2_FORWARD (x).index () == It::index ) { ptr = &std::get<It::index >(x); return true ; } };
2050
- return false ;
2051
- });
2052
- if (!ptr) { Throw ( std::bad_variant_access (), " 'as' cast failed for 'variant'" ); }
2053
- return cpp2::forward_like<decltype (x)>(*ptr);
2054
- }
2055
2013
else {
2056
2014
return nonesuch;
2057
2015
}
@@ -2062,9 +2020,61 @@ auto as(auto&& x CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT_AS) -> decltype(auto)
2062
2020
// std::variant is and as
2063
2021
//
2064
2022
2065
- // Common internal helper
2066
- //
2023
+ template < typename C, specialization_of_template<std::variant> X >
2024
+ constexpr auto is ( X const & x ) -> auto
2025
+ {
2026
+ if constexpr (
2027
+ std::is_same_v<C, X>
2028
+ || std::is_base_of_v<C, X>
2029
+ )
2030
+ {
2031
+ return std::true_type{};
2032
+ }
2033
+ else {
2034
+ if (x.valueless_by_exception ()) {
2035
+ return std::is_same_v<C, empty>;
2036
+ }
2037
+ if constexpr (
2038
+ std::is_same_v<C, empty>
2039
+ )
2040
+ {
2041
+ if constexpr (requires { {variant_contains_type<std::monostate>(std::declval<X>())} -> std::same_as<std::true_type>; }) {
2042
+ return std::get_if<std::monostate>(&x) != nullptr ;
2043
+ }
2044
+ }
2045
+ return type_find_if (x, [&]<typename It>(It const &) -> bool {
2046
+ if (x.index () == It::index ) { return std::is_same_v<C, std::variant_alternative_t <It::index , X>>;}
2047
+ return false ;
2048
+ }) != std::variant_npos;
2049
+ }
2050
+ }
2051
+
2052
+
2053
+ inline constexpr auto is ( specialization_of_template<std::variant> auto const & x, auto && value ) -> bool
2054
+ {
2055
+ return type_find_if (x, [&]<typename It>(It const &) -> bool {
2056
+ if (x.index () == It::index ) {
2057
+ if constexpr (valid_predicate<decltype (value), decltype (std::get<It::index >(x))>) {
2058
+ return value (std::get<It::index >(x));
2059
+ } else if constexpr ( requires { bool {std::get<It::index >(x) == value}; } ) {
2060
+ return std::get<It::index >(x) == value;
2061
+ }
2062
+ }
2063
+ return false ;
2064
+ }) != std::variant_npos;
2065
+ }
2067
2066
2067
+ template < typename C >
2068
+ auto as (specialization_of_template<std::variant> auto && x CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT_AS) -> decltype(auto )
2069
+ {
2070
+ constness_like_t <C, decltype (x)>* ptr = nullptr ;
2071
+ type_find_if (CPP2_FORWARD (x), [&]<typename It>(It const &) -> bool {
2072
+ if constexpr (std::is_same_v< typename It::type, C >) { if (CPP2_FORWARD (x).index () == It::index ) { ptr = &std::get<It::index >(x); return true ; } };
2073
+ return false ;
2074
+ });
2075
+ if (!ptr) { Throw ( std::bad_variant_access (), " 'as' cast failed for 'variant'" ); }
2076
+ return cpp2::forward_like<decltype (x)>(*ptr);
2077
+ }
2068
2078
2069
2079
// -------------------------------------------------------------------------------------------------------------
2070
2080
// std::any is and as
0 commit comments