@@ -2060,28 +2060,22 @@ auto as( X&& x CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT_AS) -> decltype(auto)
2060
2060
2061
2061
// is Type
2062
2062
//
2063
- template <typename T, typename X>
2064
- requires (std::is_same_v<X,std::any> && !std::is_same_v<T,std::any> && !std::is_same_v<T,empty>)
2065
- constexpr auto is ( X const & x ) -> bool
2066
- { return x.type () == Typeid<T>(); }
2067
-
2068
- template <typename T, typename X>
2069
- requires (std::is_same_v<X,std::any> && std::is_same_v<T,empty>)
2070
- constexpr auto is ( X const & x ) -> bool
2071
- { return !x.has_value (); }
2072
-
2063
+ template <typename T, std::same_as<std::any> X>
2064
+ constexpr auto is ( X const & x ) -> bool{
2065
+ if (!x.has_value ()) {
2066
+ return std::is_same_v<T,empty>;
2067
+ }
2068
+ return x.type () == Typeid<T>();
2069
+ }
2073
2070
2074
2071
// is Value
2075
2072
//
2076
2073
inline constexpr auto is ( std::any const & x, auto && value ) -> bool
2077
2074
{
2078
2075
// Predicate case
2079
- if constexpr (requires{ bool { value (x) }; } ) {
2076
+ if constexpr (valid_predicate< decltype ( value), decltype (x)> ) {
2080
2077
return value (x);
2081
2078
}
2082
- else if constexpr (std::is_function_v<decltype (value)> || requires{ &value.operator (); }) {
2083
- return false ;
2084
- }
2085
2079
2086
2080
// Value case
2087
2081
else if constexpr (requires{ bool { *std::any_cast<CPP2_TYPEOF (value)>(&x) == value }; }) {
@@ -2095,10 +2089,12 @@ inline constexpr auto is( std::any const& x, auto&& value ) -> bool
2095
2089
2096
2090
// as
2097
2091
//
2098
- template <typename T, typename X>
2099
- requires (!std::is_reference_v<T> && std::is_same_v<X,std::any> && !std::is_same_v<T,std::any>)
2100
- constexpr auto as ( X const & x ) -> T
2101
- { return std::any_cast<T>( x ); }
2092
+ template <typename T, same_type_as<std::any> X>
2093
+ constexpr auto as ( X && x ) -> decltype(auto ) {
2094
+ constness_like_t <T, X>* ptr = std::any_cast<T>( &x );
2095
+ if (!ptr) { Throw ( std::bad_any_cast (), " 'as' cast failed for 'std::any'" ); }
2096
+ return cpp2::forward_like<X>(*ptr);
2097
+ }
2102
2098
2103
2099
2104
2100
// -------------------------------------------------------------------------------------------------------------
0 commit comments